Opened 3 years ago

Closed 3 years ago

#18925 closed Bug (wontfix)

Exception information should be localized and passed to got_request_exception signal handlers

Reported by: obeattie Owned by: nobody
Component: HTTP handling Version: 1.4
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

It is possible, when Django is running under Gevent (ie. via Gunicorn), for traceback information for exceptions raised while processing a request to be lost. This is because Gunicorn will throw away traceback information (deliberately, though it doesn't necessarily sound as if this is "by design"), if a switch to another greenlet occurs during processing an exception.

The specific case where this has happened for me (and subsequently destroyed traceback information about errors happening in production), is when using a patched version of psycopg2 and a signal handler (like Sentry's) which accesses the database — causing a greenlet switch. It can be replicated by using the process described in a ticket on Gunicorn's tracker.

It seems the only way to solve this is for Django to localize sys.exc_info() before calling signal handlers (which may do who knows what with regard to greenlet switching), and to pass it explicitly to signal handlers. This shouldn't cause any issues with existing signal handlers since they all must accept **kwargs anyway. I've prepared a patch, which I'll submit via a Pull Request on GitHub referencing this ticket.

On my install, before applying the patch, Django error pages would appear without traceback information; after the change, they have a proper stack trace, as expected.

Change History (5)

comment:1 Changed 3 years ago by obeattie

  • Has patch set
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

I've sent a pull request on GitHub.

comment:2 Changed 3 years ago by obeattie

Should also add that this should not cause cyclical reference issues as mentioned that it may in the Python documentation, since the local reference is held in the handler, not the exception handlers.

comment:3 Changed 3 years ago by claudep

  • Needs tests set

comment:4 Changed 3 years ago by obeattie

  • Needs tests unset

I've now added a test for this, that doesn't require a dependency on Gunicorn to replicate

comment:5 Changed 3 years ago by obeattie

  • Resolution set to wontfix
  • Status changed from new to closed

In response to my posts on the mailing list, this has actually been fixed in gevent, so closing this.

Note: See TracTickets for help on using tickets.
Back to Top