From 0a0ef9d140a1eedaa03e791eca9090f7a15ced42 Mon Sep 17 00:00:00 2001 From: Ankit Patel Date: Wed, 1 Feb 2023 16:06:02 -0500 Subject: [PATCH 1/2] only create sockets in forked processes not in master --- gunicorn/arbiter.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/gunicorn/arbiter.py b/gunicorn/arbiter.py index 7b9ed76f5..f3f63d204 100644 --- a/gunicorn/arbiter.py +++ b/gunicorn/arbiter.py @@ -10,6 +10,7 @@ import sys import time import traceback +import socket from gunicorn.errors import HaltServer, AppImportError from gunicorn.pidfile import Pidfile @@ -152,7 +153,8 @@ def start(self): for fd in os.environ.pop('GUNICORN_FD').split(','): fds.append(int(fd)) - self.LISTENERS = sock.create_sockets(self.cfg, self.log, fds) + if not (self.cfg.reuse_port and hasattr(socket, 'SO_REUSEPORT')): + self.LISTENERS = sock.create_sockets(self.cfg, self.log, fds) listeners_str = ",".join([str(lnr) for lnr in self.LISTENERS]) self.log.debug("Arbiter booted") @@ -579,6 +581,8 @@ def spawn_worker(self): try: util._setproctitle("worker [%s]" % self.proc_name) self.log.info("Booting worker with pid: %s", worker.pid) + if self.cfg.reuse_port: + worker.sockets = sock.create_sockets(self.cfg, self.log) self.cfg.post_fork(self, worker) worker.init_process() sys.exit(0) From b0115b9c272d8d1b67a0a3efc2711c3c9f2690c5 Mon Sep 17 00:00:00 2001 From: Ankit Patel Date: Wed, 1 Feb 2023 16:06:43 -0500 Subject: [PATCH 2/2] detach socket in child process --- gunicorn/workers/ggevent.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gunicorn/workers/ggevent.py b/gunicorn/workers/ggevent.py index 0a844db3a..314a38ca8 100644 --- a/gunicorn/workers/ggevent.py +++ b/gunicorn/workers/ggevent.py @@ -41,7 +41,7 @@ def patch(self): sockets = [] for s in self.sockets: sockets.append(socket.socket(s.FAMILY, socket.SOCK_STREAM, - fileno=s.sock.fileno())) + fileno=s.sock.detach())) self.sockets = sockets def notify(self):