Skip to content

Commit

Permalink
arbiter: clean up main loop
Browse files Browse the repository at this point in the history
* Look up handlers in __init__() to induce run-time error early on if
  something is wrong.

* Since we now know that all handlers exist, we can simplify the main
  loop in arbiter, in such a way that we don't need to call wakeup().

So after this commit, the pipe in arbiter is only used to deliver
which signal was sent.
  • Loading branch information
sylt authored and pajod committed Aug 15, 2024
1 parent d74394b commit 717fbc2
Showing 1 changed file with 10 additions and 15 deletions.
25 changes: 10 additions & 15 deletions gunicorn/arbiter.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
#
# This file is part of gunicorn released under the MIT license.
# See the NOTICE for more information.
import queue
import errno
import os
import random
import signal
import sys
import time
import traceback
import queue
import socket

from gunicorn.errors import HaltServer, AppImportError
from gunicorn.pidfile import Pidfile
Expand Down Expand Up @@ -43,8 +42,6 @@ class Arbiter:
# pylint: disable=used-before-assignment
SIGNALS = [getattr(signal.Signals, "SIG%s" % x)
for x in "CHLD HUP QUIT INT TERM TTIN TTOU USR1 USR2 WINCH".split()]
# pylint: disable=used-before-assignment
SIG_NAMES = dict((sig, sig.name[3:].lower()) for sig in SIGNALS)

def __init__(self, app):
os.environ["SERVER_SOFTWARE"] = SERVER_SOFTWARE
Expand Down Expand Up @@ -74,6 +71,11 @@ def __init__(self, app):
0: sys.executable
}

self.SIG_HANDLERS = dict(
(sig, getattr(self, "handle_%s" % sig.name[3:].lower()))
for sig in self.SIGNALS
)

def _get_num_workers(self):
return self._num_workers

Expand Down Expand Up @@ -196,18 +198,11 @@ def run(self):

try:
sig = self.SIG_QUEUE.get(timeout=1)
except queue.Empty:
sig = None

if sig:
signame = self.SIG_NAMES.get(sig)
handler = getattr(self, "handle_%s" % signame, None)
if not handler:
self.log.error("Unhandled signal: %s", signame)
continue
if sig != signal.SIGCHLD:
self.log.info("Handling signal: %s", signame)
handler()
self.log.info("Handling signal: %s", signal.Signals(sig).name)
self.SIG_HANDLERS[sig]()
except queue.Empty:
pass

self.murder_workers()
self.manage_workers()
Expand Down

0 comments on commit 717fbc2

Please sign in to comment.