# HG changeset patch # User Matt Harbison # Date 2020-10-13 20:44:57 # 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 --git a/mercurial/posix.py b/mercurial/posix.py --- a/mercurial/posix.py +++ b/mercurial/posix.py @@ -764,10 +764,14 @@ def bindunixsocket(sock, path): # 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)