From 6ada1ce03aa92f6b3be7038bc5bef15b89ce78e4 Mon Sep 17 00:00:00 2001 From: Kadir Can Ozden <101993364+bysiber@users.noreply.github.com> Date: Fri, 20 Feb 2026 06:17:29 +0300 Subject: [PATCH] fix: ensure pidfile fd is closed on rename failure --- gunicorn/pidfile.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/gunicorn/pidfile.py b/gunicorn/pidfile.py index b171f7d9..8ba7fec2 100644 --- a/gunicorn/pidfile.py +++ b/gunicorn/pidfile.py @@ -33,12 +33,14 @@ class Pidfile: if fdir and not os.path.isdir(fdir): raise RuntimeError("%s doesn't exist. Can't create pidfile." % fdir) fd, fname = tempfile.mkstemp(dir=fdir) - os.write(fd, ("%s\n" % self.pid).encode('utf-8')) - if self.fname: - os.rename(fname, self.fname) - else: - self.fname = fname - os.close(fd) + try: + os.write(fd, ("%s\n" % self.pid).encode('utf-8')) + if self.fname: + os.rename(fname, self.fname) + else: + self.fname = fname + finally: + os.close(fd) # set permissions to -rw-r--r-- os.chmod(self.fname, 420)