posix: avoid a leaked file descriptor in a unix domain socket exception case
authorMatt Harbison <matt_harbison@yahoo.com>
Tue, 13 Oct 2020 16:44:57 -0400
changeset 45718 87c35b5a14eb
parent 45717 755214a84b9d
child 45719 c10c87c8fe79
posix: avoid a leaked file descriptor in a unix domain socket exception case Differential Revision: https://phab.mercurial-scm.org/D9206
mercurial/posix.py
--- a/mercurial/posix.py	Tue Oct 13 16:41:01 2020 -0400
+++ b/mercurial/posix.py	Tue Oct 13 16:44:57 2020 -0400
@@ -764,10 +764,14 @@
     # platforms (see sys/un.h)
     dirname, basename = os.path.split(path)
     bakwdfd = None
-    if dirname:
-        bakwdfd = os.open(b'.', os.O_DIRECTORY)
-        os.chdir(dirname)
-    sock.bind(basename)
-    if bakwdfd:
-        os.fchdir(bakwdfd)
-        os.close(bakwdfd)
+
+    try:
+        if dirname:
+            bakwdfd = os.open(b'.', os.O_DIRECTORY)
+            os.chdir(dirname)
+        sock.bind(basename)
+        if bakwdfd:
+            os.fchdir(bakwdfd)
+    finally:
+        if bakwdfd:
+            os.close(bakwdfd)