# HG changeset patch # User Matt Harbison # Date 1602621897 14400 # Node ID 87c35b5a14eb66bd4b1937bb65f948a3abe41d79 # Parent 755214a84b9d69a755b02b6195b4842f99a2a46d posix: avoid a leaked file descriptor in a unix domain socket exception case Differential Revision: https://phab.mercurial-scm.org/D9206 diff -r 755214a84b9d -r 87c35b5a14eb 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)