Patchwork D9206: posix: avoid a leaked file descriptor in a unix domain socket exception case

login
register
mail settings
Submitter phabricator
Date Oct. 14, 2020, 4:24 p.m.
Message ID <differential-rev-PHID-DREV-kt545j3ztmbrf7naagf3-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/47465/
State Superseded
Headers show

Comments

phabricator - Oct. 14, 2020, 4:24 p.m.
mharbison72 created this revision.
Herald added a reviewer: hg-reviewers.
Herald added a subscriber: mercurial-patches.

REPOSITORY
  rHG Mercurial

BRANCH
  default

REVISION DETAIL
  https://phab.mercurial-scm.org/D9206

AFFECTED FILES
  mercurial/posix.py

CHANGE DETAILS




To: mharbison72, #hg-reviewers
Cc: mercurial-patches, mercurial-devel

Patch

diff --git a/mercurial/posix.py b/mercurial/posix.py
--- a/mercurial/posix.py
+++ b/mercurial/posix.py
@@ -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)