Skip to content

Commit

Permalink
Support dumping gevent stacks from uWSGI workers
Browse files Browse the repository at this point in the history
To help debug cases where a worker process "hangs", add a SIGCONT signal handler
which dumps both the native and greenlet threads.
  • Loading branch information
robertknight committed Sep 17, 2024
1 parent 0ce6bb5 commit aa1f344
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions viahtml/wsgi.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
"""The application providing a WSGI entry-point."""

import os
import signal

# Our job here is to leave this `application` attribute laying around as
# it's what uWSGI expects to find.
from viahtml.app import Application


def _dump_stacks(_signal, frame): # pylint:disable=unused-argument, # pragma: no cover
# Print both Python thread and gevent "greenlet" stats and backtraces.
#
# See https://www.gevent.org/monitoring.html#visibility
import gevent # pylint:disable=import-outside-toplevel
gevent.util.print_run_info()


application = Application()

if os.environ.get("SENTRY_DSN"): # pragma: no cover
Expand All @@ -17,3 +27,11 @@
# pylint: disable=redefined-variable-type
sentry_sdk.init(dsn=os.environ["SENTRY_DSN"])
application = SentryWsgiMiddleware(application)

# Add a way to dump stacks so we can see what a uWSGI worker is doing if it
# hangs.
#
# We use `SIGCONT` for this because this handler isn't used for anything
# important, and uWSGI defines its own `SIGUSR1` and `SIGUSR2` handlers after
# this code runs.
signal.signal(signal.SIGCONT, _dump_stacks)

0 comments on commit aa1f344

Please # to comment.