Skip to content

Wake SSLStream handshake waiters when the handshake fails - #3583

Open
AboodFares wants to merge 1 commit into
python-trio:mainfrom
AboodFares:fix-ssl-handshake-waiter-hang
Open

AboodFares wants to merge 1 commit into
python-trio:mainfrom
AboodFares:fix-ssl-handshake-waiter-hang

Conversation

@AboodFares

Copy link
Copy Markdown

Fixes #3581.

If the lazy handshake fails or gets cancelled while another task is waiting on it (e.g. send_all + receive_some), the waiter hangs forever, because _Once only set the done event on success.

Changes:

  • _Once always sets the event now and tracks failure with a _failed flag
  • waiters (and the checkpoint=False path) raise BrokenResourceError if it failed
  • the original exception isn't re-raised, to avoid the ref cycle issue from Fix _Once.ensure() to propagate handshake failure to concurrent waiters #3397
  • done still means "succeeded", so NeedHandshakeError still works
  • updated the do_handshake docstring

Tests cover a failed handshake, a cancelled handshake, and _Once directly for the checkpoint=False path. All of them hang without the fix.

If the task performing the lazy TLS handshake raised or was cancelled,
_Once never set its done event, so a concurrent task waiting on the
handshake (e.g. receive_some while another task is in send_all) slept
forever.

Always set the event, record failure in a flag, and raise
BrokenResourceError for waiters and later fast-path callers. The original
exception is not stored, avoiding reference cycles and mutated
tracebacks. `done` still means the handshake succeeded, so attribute
forwarders keep raising NeedHandshakeError after a failure.

Fixes python-trio#3581.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov

codecov Bot commented Nov 8, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00000%. Comparing base (3cb6acd) to head (83cee08).
⚠️ Report is 3 commits behind head on main.

Additional details and impacted files
@@               Coverage Diff               @@
##                 main        #3583   +/-   ##
===============================================
  Coverage   100.00000%   100.00000%           
===============================================
  Files             128          128           
  Lines           19555        19625   +70     
  Branches         1330         1333    +3     
===============================================
+ Hits            19555        19625   +70     
Files with missing lines Coverage Δ
src/trio/_ssl.py 100.00000% <100.00000%> (ø)
src/trio/_tests/test_ssl.py 100.00000% <100.00000%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@CoolCat467 CoolCat467 added the LLM Slop Entirely generated by a large language model with little or no human oversight label Nov 10, 2026

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

LLM Slop Entirely generated by a large language model with little or no human oversight

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

2 participants