Repository navigation
Fix UnixHTTPConnection.connect leaking the socket on failure - #3466
Open
tony1233200 wants to merge 1 commit into
Open
tony1233200 wants to merge 1 commit into
tony1233200 wants to merge 1 commit into
Conversation
Fixes docker#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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix UnixHTTPConnection.connect leaking the socket on failure
Fixes #3464
Root cause
UnixHTTPConnection.connect()indocker/transport/unixconn.pycreated theAF_UNIXsocket and then calledsock.connect(self.unix_socket)withoutclosing it on failure. When the daemon socket cannot be reached (Docker not
running, wrong path), the exception propagated while the socket stayed open
until the garbage collector ran, which then emitted
ResourceWarning: unclosed <socket.socket ...>.Fix
Wrap the
settimeout/connectcalls intry/except, close the socket onany failure, then re-raise.
self.sockis only assigned after a successfulconnect, so a failed connection no longer leaves a half-open socket behind.
Reproduction
ResourceWarning: unclosed <socket.socket fd=3, family=1, type=1, proto=0>atgc.collect().DockerExceptionis unchanged.Tests
New regression tests in
tests/unit/transport/test_unixconn.py:test_connect_closes_socket_on_failure— fails on unfixed code (leakedResourceWarningrecorded), passes with the fix; also assertsconn.sockstays
None.test_connect_sets_sock_on_success— connects against a real local unixsocket server; passes before and after.
Results:
pytest tests/unit/transport/test_unixconn.py— 2 passed(with
-W error::ResourceWarning).Full unit suite:
pytest tests/unit— 607 passed, 10 skipped, 8 xfailed,1 xpassed, 8 failed. The 8 failures are all pre-existing
TCPSocketStreamTestcases intests/unit/api_test.pyfailing withProxyErroragainst the sandbox egress proxy; they fail identically onunfixed code.
Lint:
ruff check docker tests(the CI lint command) is clean.AI disclosure: this change was developed with AI assistance (Claude) and verified by running the reproduction and tests above.