Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lock domain socket and remove on last arbiter exit #1185

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 13 additions & 2 deletions gunicorn/sock.py
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