Skip to content

Fix SSLStream waiter hang after a failed or cancelled handshake - #3596

Open
DeviousCardi wants to merge 1 commit into
python-trio:mainfrom
DeviousCardi:fix-sslstream-handshake-failure-hang
Open

DeviousCardi wants to merge 1 commit into
python-trio:mainfrom
DeviousCardi:fix-sslstream-handshake-failure-hang

Conversation

@DeviousCardi

@DeviousCardi DeviousCardi commented Nov 16, 2026 •

Copy link
Copy Markdown

Fixes #3581.

_Once.ensure() set its done event only after a successful handshake. If the handshake raised (EOF, reset, certificate error) or the task running it was cancelled, concurrent send_all/receive_some/do_handshake tasks stayed parked in await self._done.wait() forever.

The event is now set in a finally block, with a _failed flag recorded on failure. Waiters raise BrokenResourceError, matching later use of the stream, while the task that ran the handshake keeps its original exception/cancellation. done now means “completed successfully”, so guarded forwarders still raise NeedHandshakeError after a failed handshake.

Tests use the issue's memory_stream_pair reproducer for handshake failure, cancellation and concurrent success, all with fail_after so a regression fails instead of hanging. test_ssl.py passes (70 passed, 2 skipped); the new tests fail on the unpatched tree. Ruff, black, mypy and git diff --check are clean.

_Once.ensure() set its done event only on a successful handshake, so concurrent send/receive tasks waited forever when the handshake raised or was cancelled. Signal the event in a finally block, record failure, and make waiters raise BrokenResourceError. Successful-handshake behavior is unchanged.
@codecov

codecov Bot commented Nov 16, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00000%. Comparing base (7e0dc2d) to head (e975a1a).

Additional details and impacted files
@@                 Coverage Diff                  @@
##                main        #3596         +/-   ##
====================================================
+ Coverage   98.16743%   100.00000%   +1.83257%     
====================================================
  Files            128          128                 
  Lines          19972        19671        -301     
  Branches        1399         1337         -62     
====================================================
+ Hits           19606        19671         +65     
+ Misses           359            0        -359     
+ Partials           7            0          -7     
Files with missing lines Coverage Δ
src/trio/_ssl.py 100.00000% <100.00000%> (ø)
src/trio/_tests/test_ssl.py 100.00000% <100.00000%> (ø)

... and 56 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Concurrent SSLStream user hangs forever when the lazy handshake fails or is cancelled

1 participant