Repository navigation
proxy: ensure handle_connection releases transports on cancellation - #8643
Open
jtoledo3970 wants to merge 2 commits into
Open
jtoledo3970 wants to merge 2 commits into
jtoledo3970 wants to merge 2 commits into
Conversation
jtoledo3970
force-pushed
the
fix/8620-release-upstream-transports
branch
from
November 24, 2026 19:41
7722150 to
6149faa
Compare
This branch has not been deployed
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.
Description
Fixes #8620
ConnectionHandler.handle_connection()previously only closedwriterand popped the entry fromself.transportsat the end of the method, and only caughtasyncio.CancelledErroraroundreader.read()anddrain_writers().When an upstream task was cancelled while awaiting
server_event(events.DataReceived(...))orserver_event(events.ConnectionClosed(...))(for example, contending on_server_event_lockacross multiplexed HTTP/2 streams),CancelledErrorbypassed cleanup and left thewriterunclosed and the entry inself.transports.This PR:
ConnectionHandler._release_transport()helper to popself.transportsand closeio.writer.handle_connection()intry ... finally: self._release_transport(connection)so transport cleanup always runs on EOF, cancellation, or error.asyncio.CancelledErroraroundawait self.server_event(events.DataReceived(...))(matchingdrain_writers()) soConnectionClosedis still dispatched before cleanup.test/mitmproxy/proxy/test_server.pyverifying cleanup when cancelled duringDataReceivedandConnectionClosed, as well as the normal EOF path.(Note:
open_connection()also acquiresself.max_conns[address]outside itstryblock, which can leave a placeholderConnectionIOentry inself.transportsif cancelled while waiting for a semaphore slot — happy to address that in a follow-up if desired.)Checklist