Skip to content

Commit 7e9729b

Browse files
whitslackrustyrussell
authored andcommitted
pyln-client: don't leak dirfd after connecting Unix socket
This file descriptor leak was causing test failures due to exceeding the limit on open file descriptors. Note that the leak only occurred if the RPC socket path was longer than can fit in a struct sockaddr_un. Changelog-Fixed: pyln-client no longer leaks a file descriptor when connecting to an RPC socket with a long path name.
1 parent e732eda commit 7e9729b

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

contrib/pyln-client/pyln/client/lightning.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -254,9 +254,12 @@ def connect(self) -> None:
254254
# Open an fd to our home directory, that we can then find
255255
# through `/proc/self/fd` and access the contents.
256256
dirfd = os.open(dirname, os.O_DIRECTORY | os.O_RDONLY)
257-
short_path = "/proc/self/fd/%d/%s" % (dirfd, basename)
258-
self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
259-
self.sock.connect(short_path)
257+
try:
258+
short_path = "/proc/self/fd/%d/%s" % (dirfd, basename)
259+
self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
260+
self.sock.connect(short_path)
261+
finally:
262+
os.close(dirfd)
260263
elif (e.args[0] == "AF_UNIX path too long" and os.uname()[0] == "Darwin"):
261264
temp_dir = tempfile.mkdtemp()
262265
temp_link = os.path.join(temp_dir, "socket_link")

0 commit comments

Comments
 (0)