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 fefc8b9 commit 11c6970
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 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,17 +198,10 @@ def run(self):

try:
sig = self.SIG_QUEUE.get(timeout=1)
self.log.info("Handling signal: %s", signal.Signals(sig).name)
self.SIG_HANDLERS[sig]()
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
self.log.info("Handling signal: %s", signame)
handler()
pass

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

0 comments on commit 11c6970

Please sign in to comment.