Handle rejected handshakes cleanly in the threading server#1726
Open
apoorvdarshan wants to merge 1 commit into
Open
Handle rejected handshakes cleanly in the threading server#1726apoorvdarshan wants to merge 1 commit into
apoorvdarshan wants to merge 1 commit into
Conversation
When process_request or process_response rejects the opening handshake by returning an HTTP response, handshake() returns normally and handshake_exc stays None, so conn_handler reached `assert connection.protocol.state is OPEN`. Since the state is CONNECTING or CLOSED after a rejection, the assertion failed and the AssertionError was swallowed by the surrounding `except Exception`, which closed the raw socket without going through close_socket() or joining recv_events_thread. With `python -O`, the assertion was stripped instead and the connection handler ran on the already-closed connection, logging "connection handler failed" on every rejection. Replace the assertion with an explicit branch that takes the rejection path cleanly, mirroring the asyncio server. Guard the corresponding test with assertNoLogs, matching its asyncio counterpart. Fixes python-websockets#1722.
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.
Summary
In the :mod:
threadingserver, whenprocess_requestorprocess_responserejects the opening handshake by returning an HTTP response,handshake()returns normally andhandshake_excstaysNone(it's only set for failures, not deliberate rejections).conn_handlerthen reached:After a rejection the state is
CONNECTINGorCLOSED, neverOPEN, so the assertion failed. The resultingAssertionErrorwas swallowed by the surroundingexcept Exception:block, which closed the raw socket without going throughclose_socket()or joiningrecv_events_thread. Nothing was logged, so the failure was silent.With
python -O, the assertion was stripped instead, so the connection handler ran on the already-closed connection:start_keepalive()andhandler(connection)executed against aCLOSEDconnection and every rejection logged"connection handler failed".This is the exact case the asyncio server already handles explicitly (
asyncio/server.py), so the two implementations had diverged.Fix
Replace the assertion with an explicit branch that takes the rejection/close path cleanly, mirroring the asyncio server and using the threading server's own cleanup idiom:
This also makes the
# pragma: no coveron the outerexcept Exception:accurate again: that branch no longer runs on every rejection.Tests / Verification
tests/sync/test_server.py::test_process_request_returns_responsewithself.assertNoLogs("websockets", logging.ERROR), matching its asyncio counterpart. On the unpatched server this fails underpython -O(the handler runs on the rejected connection and logs an error); with the fix it passes under bothpythonandpython -O.python -m unittest tests.sync.test_serverpasses (50 tests) under bothpythonandpython -O.tests.sync.test_client,tests.sync.test_router, andtests.asyncio.test_serverremain green.src/websockets/sync/server.pystays at 100% (the new branch is exercised by the rejection and success paths).ruff format --check,ruff check, andmypy --strictare clean.Disclosure: prepared with AI assistance; reviewed and verified locally.