Skip to content

Commit

Permalink
arbiter: Add Arbiter:wakeup() method
Browse files Browse the repository at this point in the history
It accepts an optional "due_to_signal" argument which can be used to
tell if the wakeup was made because a signal handler needs to be
executed or not.
  • Loading branch information
sylt committed Aug 18, 2024
1 parent 64387d1 commit 7ecea2d
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions gunicorn/arbiter.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class Arbiter:
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()]
WAKEUP_REQUEST = signal.NSIG

def __init__(self, app):
os.environ["SERVER_SOFTWARE"] = SERVER_SOFTWARE
Expand Down Expand Up @@ -178,12 +179,18 @@ def init_signals(self):

def signal(self, sig, frame):
""" Note: Signal handler! No logging allowed. """
self.SIG_QUEUE.put(sig)
self.wakeup(due_to_signal=sig)

# Some UNIXes require SIGCHLD to be reinstalled, see python signal docs
if sig == signal.SIGCHLD:
signal.signal(sig, self.signal)

def wakeup(self, due_to_signal=None):
"""\
Wake up the main master loop.
"""
self.SIG_QUEUE.put(due_to_signal or self.WAKEUP_REQUEST)

def run(self):
"Main master loop."
self.start()
Expand All @@ -197,9 +204,10 @@ def run(self):

try:
sig = self.SIG_QUEUE.get(timeout=1)
if sig != signal.SIGCHLD:
self.log.info("Handling signal: %s", signal.Signals(sig).name)
self.SIG_HANDLERS[sig]()
if sig != self.WAKEUP_REQUEST:
if sig != signal.SIGCHLD:
self.log.info("Handling signal: %s", signal.Signals(sig).name)
self.SIG_HANDLERS[sig]()
except queue.Empty:
pass

Expand Down

0 comments on commit 7ecea2d

Please sign in to comment.