posix: avoid a leaked file descriptor in a unix domain socket exception case
Differential Revision: https://phab.mercurial-scm.org/D9206
--- 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)