uniformize context managers - #1755
Merged
Merged
Conversation
aaugustin
commented
Aug 29, 2026
Member
- Simplify error handling in servers.
- Make clients use close code 1011 on exceptions.
Rely on the connection's context manager implementation instead of duplicating its logic.
According to errata 3227, which is approved, the meaning of code 1011 also applies to clients. It makes sense to use them symetrically.
There was a problem hiding this comment.
Pull request overview
Unifies modern client/server context-manager cleanup and applies close code 1011 when exiting after an exception.
Changes:
- Delegates client context exit to connection implementations.
- Simplifies server handler cleanup with connection context managers.
- Adds normal and exceptional close-code tests across all runtimes.
Reviewed changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
src/websockets/asyncio/client.py |
Delegates async context management. |
src/websockets/asyncio/server.py |
Context-manages handler connections. |
src/websockets/sync/client.py |
Delegates sync context management. |
src/websockets/sync/server.py |
Context-manages handler connections. |
src/websockets/trio/client.py |
Delegates context management while preserving nursery cleanup. |
src/websockets/trio/server.py |
Context-manages handler connections. |
tests/asyncio/test_client.py |
Tests close codes 1000 and 1011. |
tests/sync/test_client.py |
Tests close codes 1000 and 1011. |
tests/trio/test_client.py |
Tests close codes 1000 and 1011. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
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.