Skip to content

Commit

Permalink
lock domain socket and remove on last arbiter exit
Browse files Browse the repository at this point in the history
  • Loading branch information
paulnivin committed Jan 20, 2016
1 parent 3ccdafb commit 4fc3fbe
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions gunicorn/sock.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# See the NOTICE for more information.

import errno
import fcntl
import os
import socket
import stat
Expand Down Expand Up @@ -105,6 +106,8 @@ def __init__(self, addr, conf, log, fd=None):
raise ValueError("%r is not a socket" % addr)
self.parent = os.getpid()
super(UnixSocket, self).__init__(addr, conf, log, fd=fd)
# each arbiter grabs a shared lock on the unix socket.
fcntl.lockf(self.sock, fcntl.LOCK_SH | fcntl.LOCK_NB)

def __str__(self):
return "unix:%s" % self.cfg_addr
Expand All @@ -117,9 +120,17 @@ def bind(self, sock):


def close(self):
super(UnixSocket, self).close()
if self.parent == os.getpid():
os.unlink(self.cfg_addr)
# attempt to acquire an exclusive lock on the unix socket.
# if we're the only arbiter running, the lock will succeed, and
# we can safely rm the socket.
try:
fcntl.lockf(self.sock, fcntl.LOCK_EX | fcntl.LOCK_NB)
except:
pass
else:
os.unlink(self.cfg_addr)
super(UnixSocket, self).close()


def _sock_type(addr):
Expand Down

0 comments on commit 4fc3fbe

Please sign in to comment.