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 committed Feb 7, 2024
1 parent b7df92e commit 5848bc4
Showing 1 changed file with 9 additions and 12 deletions.
21 changes: 9 additions & 12 deletions gunicorn/arbiter.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ class Arbiter(object):
SIG_QUEUE = queue.SimpleQueue()
SIGNALS = [getattr(signal.Signals, "SIG%s" % x)
for x in "CHLD HUP QUIT INT TERM TTIN TTOU USR1 USR2 WINCH".split()]
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 @@ -72,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 @@ -194,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 5848bc4

Please sign in to comment.