From 64a5b5cdf3b5d541a956479fe50dd881498386ed Mon Sep 17 00:00:00 2001 From: benoitc Date: Wed, 22 Jul 2015 06:22:24 +0200 Subject: [PATCH] don't close the unix socket when the worker exit When the worker was exiting, eventlet is closing the listening socket in th worker. Since the socket instances are shared, this was also removing the unix socket on close. This change make sure that the socket can only be closed by its parent (where the socket have been bound). While I'm here, also make sure we don't use any blocking function in eventlet while switching). fix #965 --- gunicorn/sock.py | 5 +++-- gunicorn/workers/geventlet.py | 9 ++++++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/gunicorn/sock.py b/gunicorn/sock.py index 6405ba38..4de97285 100644 --- a/gunicorn/sock.py +++ b/gunicorn/sock.py @@ -57,7 +57,6 @@ class BaseSocket(object): self.sock.close() except socket.error as e: self.log.info("Error while closing socket %s", str(e)) - time.sleep(0.3) del self.sock @@ -104,6 +103,7 @@ class UnixSocket(BaseSocket): os.remove(addr) else: raise ValueError("%r is not a socket" % addr) + self.parent = os.getpid() super(UnixSocket, self).__init__(addr, conf, log, fd=fd) def __str__(self): @@ -118,7 +118,8 @@ class UnixSocket(BaseSocket): def close(self): super(UnixSocket, self).close() - os.unlink(self.cfg_addr) + if self.parent == os.getpid(): + os.unlink(self.cfg_addr) def _sock_type(addr): diff --git a/gunicorn/workers/geventlet.py b/gunicorn/workers/geventlet.py index 48b63fb2..dcb2f3d6 100644 --- a/gunicorn/workers/geventlet.py +++ b/gunicorn/workers/geventlet.py @@ -25,7 +25,6 @@ import greenlet from gunicorn.http.wsgi import sendfile as o_sendfile from gunicorn.workers.async import AsyncWorker - def _eventlet_sendfile(fdout, fdin, offset, nbytes): while True: try: @@ -87,11 +86,11 @@ def patch_sendfile(): class EventletWorker(AsyncWorker): def patch(self): + hubs.use_hub() eventlet.monkey_patch(os=False) patch_sendfile() def init_process(self): - hubs.use_hub() self.patch() super(EventletWorker, self).init_process() @@ -119,7 +118,11 @@ class EventletWorker(AsyncWorker): while self.alive: self.notify() - eventlet.sleep(1.0) + try: + eventlet.sleep(1.0) + except AssertionError: + self.alive = False + break self.notify() try: