Uh oh!
There was an error while loading. Please reload this page.
Handle rejected handshakes cleanly in the threading server - #1726
Conversation
There was a problem hiding this comment.
I don't add changelog entries for small fixes like this one (e.g. cdeb882, which fixed the same problem in the asyncio implementation, didn't include one).
| assert connection.protocol.state is OPEN | ||
| if connection.protocol.state is not OPEN: | ||
| # process_request or process_response rejected the handshake. | ||
| # The connection is already closing or closed. |
There was a problem hiding this comment.
Remove this line. We're in a branch "state is not OPEN"; it's clear that it's closing or closed.
| str(raised.exception), | ||
| "server rejected WebSocket connection: HTTP 403", | ||
| ) | ||
| with self.assertNoLogs("websockets", logging.ERROR): |
There was a problem hiding this comment.
If you look at cdeb882, you'll see that it added assertNoLogs assertions to a bunch of tests in the asyncio implementation.
Likely, the same tests should have assertNoLogs in the threading implementation. Can you check and add where appropriate please?
aaugustin
commented
Jul 10, 2026
The failure in GitHub Actions looks spurious :-/ |
aaugustin
commented
Jul 10, 2026
Thank you, this looks valid. I'd like a few changes; you're welcome to update the PR if you'd like; or I can make them myself and take it from there. |
78ccb0d to
3eba136Compareaaugustin
commented
Jul 13, 2026
Looks perfect. Thank you very much! |
Uh oh!
There was an error while loading. Please reload this page.
Summary
The threading server didn't handle a rejected opening handshake explicitly. When
process_requestorprocess_responsereturned an HTTP response,handshake()returned normally while the protocol state wasn'tOPEN. The following assertion failed silently—or disappeared underpython -O, allowing the handler to run on a closed connection and log"connection handler failed".Fix
Replace the assertion with an explicit rejection path that closes the socket, joins the receive thread, and returns, mirroring the asyncio implementation.
Tests / Verification
test_process_request_returns_responsewithassertNoLogs("websockets", logging.ERROR)."opening handshake failed"error and exception forprocess_requestandprocess_responsefailures.python -O -m unittest tests.sync.test_server: 50 tests passed.Refs #1513.
Disclosure: prepared with AI assistance; manually reviewed and verified locally.