From 6f44ff4e3f419f636c1bfac8c9e31eb7c7ad4e25 Mon Sep 17 00:00:00 2001 From: Tony Y Date: Fri, 20 Nov 2026 11:17:12 +0000 Subject: [PATCH] Fix UnixHTTPConnection.connect leaking the socket on failure Fixes #3464 connect() created the AF_UNIX socket and then raised from sock.connect() without closing it, leaving the socket open until the garbage collector ran (ResourceWarning: unclosed socket). Close the socket on any failure before re-raising. --- docker/transport/unixconn.py | 8 +++- tests/unit/transport/__init__.py | 0 tests/unit/transport/test_unixconn.py | 61 +++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 tests/unit/transport/__init__.py create mode 100644 tests/unit/transport/test_unixconn.py diff --git a/docker/transport/unixconn.py b/docker/transport/unixconn.py index d571833f04..00eebcc9b9 100644 --- a/docker/transport/unixconn.py +++ b/docker/transport/unixconn.py @@ -22,8 +22,12 @@ def __init__(self, base_url, unix_socket, timeout=60): def connect(self): sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) - sock.settimeout(self.timeout) - sock.connect(self.unix_socket) + try: + sock.settimeout(self.timeout) + sock.connect(self.unix_socket) + except Exception: + sock.close() + raise self.sock = sock diff --git a/tests/unit/transport/__init__.py b/tests/unit/transport/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/unit/transport/test_unixconn.py b/tests/unit/transport/test_unixconn.py new file mode 100644 index 0000000000..62f58fde9e --- /dev/null +++ b/tests/unit/transport/test_unixconn.py @@ -0,0 +1,61 @@ +import gc +import os +import socket +import tempfile +import threading +import unittest +import warnings + +from docker.transport.unixconn import UnixHTTPConnection + + +class UnixHTTPConnectionTest(unittest.TestCase): + def test_connect_closes_socket_on_failure(self): + """A failed connect() must not leak the socket (docker-py#3464).""" + conn = UnixHTTPConnection( + base_url='http+unix://localhost', + unix_socket='/nonexistent/docker.sock', + timeout=5, + ) + with warnings.catch_warnings(record=True) as caught: + warnings.simplefilter('always', ResourceWarning) + with self.assertRaises(FileNotFoundError): + conn.connect() + gc.collect() + leaked = [w for w in caught + if issubclass(w.category, ResourceWarning) + and 'unclosed' in str(w.message)] + self.assertEqual(leaked, []) + self.assertIsNone(conn.sock) + + def test_connect_sets_sock_on_success(self): + with tempfile.TemporaryDirectory() as tmpdir: + path = os.path.join(tmpdir, 'docker.sock') + server = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) + server.bind(path) + server.listen(1) + accepted = [] + + def _accept(): + client, _ = server.accept() + accepted.append(client) + client.close() + + thread = threading.Thread(target=_accept, daemon=True) + thread.start() + try: + conn = UnixHTTPConnection( + base_url='http+unix://localhost', + unix_socket=path, + timeout=5, + ) + conn.connect() + self.assertIsNotNone(conn.sock) + conn.close() + finally: + thread.join(timeout=5) + server.close() + + +if __name__ == '__main__': + unittest.main()