Skip to content

Commit

Permalink
arbiter: Do basic rate limiting of signals
Browse files Browse the repository at this point in the history
In the theoretical case that there are many signals directed to the
arbiter process for whatever reason, this change should keep the
backlog of signals fairly short to make the arbiter able to catch up.
  • Loading branch information
sylt committed Sep 3, 2024
1 parent 7ecea2d commit 4ff9e9b
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions gunicorn/arbiter.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,14 +202,10 @@ def run(self):
while True:
self.maybe_promote_master()

try:
sig = self.SIG_QUEUE.get(timeout=1)
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
for sig in self.wait_for_signals(timeout=1):
if sig != signal.SIGCHLD:
self.log.info("Handling signal: %s", signal.Signals(sig).name)
self.SIG_HANDLERS[sig]()

self.murder_workers()
self.manage_workers()
Expand All @@ -227,6 +223,16 @@ def run(self):
self.pidfile.unlink()
sys.exit(-1)

def wait_for_signals(self, timeout=1):
signals = {} # Use dict() instead of set() to keep the insertion order
try:
signals.setdefault(self.SIG_QUEUE.get(timeout))
while True:
signals.setdefault(self.SIG_QUEUE.get_nowait())
except queue.Empty:
signals.pop(self.WAKEUP_REQUEST, None)
return signals.keys()

def handle_chld(self):
"SIGCHLD handling"
self.reap_workers()
Expand Down

0 comments on commit 4ff9e9b

Please sign in to comment.