Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SIGQUIT handler on UvicornWorker #1424

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions uvicorn/workers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging
import signal
import sys
import threading
from typing import Any

from gunicorn.arbiter import Arbiter
Expand Down Expand Up @@ -72,9 +73,24 @@ def init_signals(self) -> None:
for s in self.SIGNALS:
signal.signal(s, signal.SIG_DFL)

def _install_sigquit_handler(self, server: Server) -> None:
"""Workaround to install a SIGQUIT handler on workers.

Ref.:
- https://github.com/encode/uvicorn/issues/1116
- https://github.com/benoitc/gunicorn/issues/2604
"""
if threading.current_thread() is not threading.main_thread():
# Signals can only be listened to from the main thread.
return

loop = asyncio.get_running_loop()
loop.add_signal_handler(signal.SIGQUIT, self.handle_exit, signal.SIGQUIT, None)

async def _serve(self) -> None:
self.config.app = self.wsgi
server = Server(config=self.config)
self._install_sigquit_handler(server)
await server.serve(sockets=self.sockets)
if not server.started:
sys.exit(Arbiter.WORKER_BOOT_ERROR)
Expand Down