From 41c6bf8e3e52b670d5686a3ca038f66b5da2545c Mon Sep 17 00:00:00 2001 From: Kadir Can Ozden <101993364+bysiber@users.noreply.github.com> Date: Fri, 20 Feb 2026 06:16:54 +0300 Subject: [PATCH] fix: prevent UnboundLocalError in sd_notify when socket creation fails --- gunicorn/systemd.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/gunicorn/systemd.py b/gunicorn/systemd.py index 9b185506..976edabe 100644 --- a/gunicorn/systemd.py +++ b/gunicorn/systemd.py @@ -61,6 +61,7 @@ def sd_notify(state, logger, unset_environment=False): if addr is None: # not run in a service, just a noop return + sock = None try: sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM | socket.SOCK_CLOEXEC) if addr[0] == '@': @@ -72,4 +73,5 @@ def sd_notify(state, logger, unset_environment=False): finally: if unset_environment: os.environ.pop('NOTIFY_SOCKET') - sock.close() + if sock is not None: + sock.close()