Skip to content

Commit

Permalink
fix unix socket locking
Browse files Browse the repository at this point in the history
This change add proper file locking to gunicorn. By default "gunicorn.lock" is created in the temporary directory when a unix socket is bound.  In case someone want to fix the lock file path or use multiple gunicorn instance the "--lock-file" setting can be used to set the path of this file.

fix #1259
  • Loading branch information
benoitc committed May 14, 2016
1 parent fbe865f commit 6dca48e
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions gunicorn/arbiter.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import traceback

from gunicorn.errors import HaltServer, AppImportError
from gunicorn.lockfile import LockFile
from gunicorn.pidfile import Pidfile
from gunicorn.sock import create_sockets
from gunicorn import util
Expand All @@ -39,6 +40,7 @@ class Arbiter(object):
START_CTX = {}

LISTENERS = []
LOCK_FILE = None
WORKERS = {}
PIPE = []

Expand Down Expand Up @@ -131,8 +133,16 @@ def start(self):
self.cfg.on_starting(self)

self.init_signals()
need_lock = False
if not self.LISTENERS:
self.LISTENERS = create_sockets(self.cfg, self.log)
self.LISTENERS, need_lock = create_sockets(self.cfg, self.log)

self.log.info("lock file is needed %s", need_lock)
if need_lock:
self.log.info("%s created", self.cfg.lockfile)
if not self.LOCK_FILE:
self.LOCK_FILE = LockFile(self.cfg)
self.LOCK_FILE.lock()

listeners_str = ",".join([str(l) for l in self.LISTENERS])
self.log.debug("Arbiter booted")
Expand Down Expand Up @@ -335,8 +345,13 @@ def stop(self, graceful=True):
:attr graceful: boolean, If True (the default) workers will be
killed gracefully (ie. trying to wait for the current connection)
"""
locked = False
if self.LOCK_FILE:
self.LOCK_FILE.unlock()
locked = self.LOCK_FILE.locked()

for l in self.LISTENERS:
l.close()
l.close(locked)
self.LISTENERS = []
sig = signal.SIGTERM
if not graceful:
Expand Down

0 comments on commit 6dca48e

Please sign in to comment.