Skip to content

Commit

Permalink
unblock the wait loop under python 3.5
Browse files Browse the repository at this point in the history
in python 3.5 the select is blocking when waiting for it which prevent quick
exit on SIGTERM.

The problem is described:
https://www.python.org/dev/peps/pep-0475/#backward-compatibility

This change fix it by listening for signal event on the worker pipe. Once an
event is triggered it will forcefully wake up the select and return.

fix benoitc#1256
  • Loading branch information
benoitc committed May 10, 2016
1 parent 84fb0d6 commit d37d763
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
5 changes: 5 additions & 0 deletions gunicorn/workers/base.py
Expand Up @@ -113,6 +113,8 @@ def changed(fname):
[util.close_on_exec(s) for s in self.sockets]
util.close_on_exec(self.tmp.fileno())

self.wait_fds = self.sockets + [self.PIPE[0]]

self.log.close_on_exec()

self.init_signals()
Expand Down Expand Up @@ -164,6 +166,9 @@ def init_signals(self):
signal.siginterrupt(signal.SIGTERM, False)
signal.siginterrupt(signal.SIGUSR1, False)

if hasattr(signal, 'set_wakeup_fd'):
signal.set_wakeup_fd(self.PIPE[1])

def handle_usr1(self, sig, frame):
self.log.reopen_files()

Expand Down
5 changes: 4 additions & 1 deletion gunicorn/workers/sync.py
Expand Up @@ -32,7 +32,7 @@ def accept(self, listener):
def wait(self, timeout):
try:
self.notify()
ret = select.select(self.sockets, [], self.PIPE, timeout)
ret = select.select(self.wait_fds, [], [], timeout)
if ret[0]:
return ret[0]

Expand Down Expand Up @@ -93,6 +93,9 @@ def run_for_multiple(self, timeout):

if ready is not None:
for listener in ready:
if listener == self.PIPE[0]:
continue

try:
self.accept(listener)
except EnvironmentError as e:
Expand Down

0 comments on commit d37d763

Please sign in to comment.