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 6fc1f85
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions viahtml/wsgi.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
"""The application providing a WSGI entry-point."""

import faulthandler
import os
import signal
import sys

# 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(*args): # pragma: no cover
# Print both Python thread and gevent "greenlet" stats and backtraces.
#
# See https://www.gevent.org/monitoring.html#visibility
import gevent

gevent.util.print_run_info()


application = Application()

if os.environ.get("SENTRY_DSN"): # pragma: no cover
Expand All @@ -17,3 +30,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 6fc1f85

Please # to comment.