Skip to content

Commit

Permalink
fix: fast shutdown (#4)
Browse files Browse the repository at this point in the history
Break the poller wait by using shutdown event to let it know its time to wake up.
  • Loading branch information
ankush committed Jan 29, 2025
1 parent 2f5b492 commit 4f74f4a
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions gunicorn/workers/gthread.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ def __init__(self, *args, **kwargs):
# initialise the pool
self.tpool = None
self.poller = None
self.shutdown_event = os.eventfd(1)
self._lock = None
self.futures = deque()
self._keep = deque()
Expand All @@ -97,6 +98,10 @@ def get_thread_pool(self):
"""Override this method to customize how the thread pool is created"""
return futures.ThreadPoolExecutor(max_workers=self.cfg.threads)

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

def handle_quit(self, sig, frame):
self.alive = False
# worker_int callback
Expand Down Expand Up @@ -201,6 +206,9 @@ def run(self):
acceptor = partial(self.accept, server)
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)

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

0 comments on commit 4f74f4a

Please sign in to comment.