changeset 52605:72d461cbd1f3

server: don't leak a file descriptor when I/O fails writing a pid file The file never got closed if the write failed, but take the opportunity to simplify the method with the corresponding util functions.
author Matt Harbison <matt_harbison@yahoo.com>
date Tue, 17 Dec 2024 01:31:17 -0500
parents bbb43c5b753a
children f4d1f0713b49
files mercurial/server.py
diffstat 1 files changed, 5 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/server.py	Tue Dec 17 01:18:54 2024 -0500
+++ b/mercurial/server.py	Tue Dec 17 01:31:17 2024 -0500
@@ -73,14 +73,13 @@
                 os.close(fd)
 
     def writepid(pid):
-        if opts[b'pid_file']:
+        file = opts[b'pid_file']
+        if file:
+            pid_line = b'%d\n' % pid
             if appendpid:
-                mode = 'ab'
+                util.appendfile(file, pid_line)
             else:
-                mode = 'wb'
-            fp = open(opts[b'pid_file'], mode)
-            fp.write(b'%d\n' % pid)
-            fp.close()
+                util.writefile(file, pid_line)
 
     if opts[b'daemon'] and not opts[b'daemon_postexec']:
         # Signal child process startup with file removal