Skip to content

Commit

Permalink
fix: Drain shutdown event (#5)
Browse files Browse the repository at this point in the history
Otherwise it goes in a spinny loop
  • Loading branch information
ankush authored Feb 3, 2025
1 parent 4f74f4a commit b9a8570
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions gunicorn/workers/gthread.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def __init__(self, *args, **kwargs):
# initialise the pool
self.tpool = None
self.poller = None
self.shutdown_event = os.eventfd(1)
self.shutdown_event = os.eventfd(0)
self._lock = None
self.futures = deque()
self._keep = deque()
Expand All @@ -100,7 +100,7 @@ def get_thread_pool(self):

def handle_exit(self, sig, frame):
self.alive = False
os.eventfd_write(self.shutdown_event, 0)
os.eventfd_write(self.shutdown_event, 1)

def handle_quit(self, sig, frame):
self.alive = False
Expand Down Expand Up @@ -154,6 +154,10 @@ def on_client_socket_readable(self, conn, client):
# submit the connection to a worker
self.enqueue_req(conn)

def on_shutdown_event(self, *args):
# Drain any readable input to avoid getting polled again
_ = os.eventfd_read(self.shutdown_event)

def murder_keepalived(self):
now = time.time()
while True:
Expand Down Expand Up @@ -207,7 +211,7 @@ def run(self):
self.poller.register(sock, selectors.EVENT_READ, acceptor)

# This is just used to wake up the poller, nothing else needs to be done.
self.poller.register(self.shutdown_event, selectors.EVENT_READ, lambda *args: None)
self.poller.register(self.shutdown_event, selectors.EVENT_READ, self.on_shutdown_event)

while self.alive:
# notify the arbiter we are alive
Expand Down

0 comments on commit b9a8570

Please sign in to comment.