Skip to content

Commit bacbf8a

Browse files
authored
Merge pull request #2938 from Affirm/reuse-port-fix
Fix reuse-port to balance requests across Gunicorn workers
2 parents 903792f + b0115b9 commit bacbf8a

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

gunicorn/arbiter.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import sys
1010
import time
1111
import traceback
12+
import socket
1213

1314
from gunicorn.errors import HaltServer, AppImportError
1415
from gunicorn.pidfile import Pidfile
@@ -151,7 +152,8 @@ def start(self):
151152
for fd in os.environ.pop('GUNICORN_FD').split(','):
152153
fds.append(int(fd))
153154

154-
self.LISTENERS = sock.create_sockets(self.cfg, self.log, fds)
155+
if not (self.cfg.reuse_port and hasattr(socket, 'SO_REUSEPORT')):
156+
self.LISTENERS = sock.create_sockets(self.cfg, self.log, fds)
155157

156158
listeners_str = ",".join([str(lnr) for lnr in self.LISTENERS])
157159
self.log.debug("Arbiter booted")
@@ -604,6 +606,8 @@ def spawn_worker(self):
604606
try:
605607
util._setproctitle("worker [%s]" % self.proc_name)
606608
self.log.info("Booting worker with pid: %s", worker.pid)
609+
if self.cfg.reuse_port:
610+
worker.sockets = sock.create_sockets(self.cfg, self.log)
607611
self.cfg.post_fork(self, worker)
608612
worker.init_process()
609613
sys.exit(0)

gunicorn/workers/ggevent.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def patch(self):
4141
sockets = []
4242
for s in self.sockets:
4343
sockets.append(socket.socket(s.FAMILY, socket.SOCK_STREAM,
44-
fileno=s.sock.fileno()))
44+
fileno=s.sock.detach()))
4545
self.sockets = sockets
4646

4747
def notify(self):

0 commit comments

Comments
 (0)