Conversation
This only affects debug logs. * When a close frame cannot be parsed, always show it as binary. * Treat ping and pong frames as text only when it's printable ASCII or spaces (which will be escaped in the representation). This avoids treating our random 4-bytes payloads as text when they're UTF-8, which happens a bit more that 1 time out of 16. (Chances that it's ASCII are 1 in 16, and then in can be UTF-8 in a few more cases.) Refs #1763.
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
Control-frame formatting can still raise exceptions for supported memoryview payloads.
Get a fresh assessment by requesting another Copilot review.
Review effort: Balanced
Findings: 2
Open (4)
What changed in this PR
Refines debug-log rendering for WebSocket control frames.
Changes:
- Renders malformed close frames as binary.
- Restricts textual ping/pong payloads to printable ASCII and whitespace.
- Adds regression tests for control-frame formatting.
| File | Description |
|---|---|
| tests/test_frames.py | Updates ping and pong representation tests. |
| src/websockets/frames.py | Implements stricter control-frame rendering heuristics. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| if self.opcode is CLOSE: | ||
| try: | ||
| return str(Close.parse(self.data)), True | ||
| data_repr = str(Close.parse(self.data)) |
| # 4-bytes binary payloads are accidentally valid UTF-8 sequences. | ||
|
|
||
| elif self.opcode in CTRL_OPCODES: | ||
| if is_space_or_printable_ascii(self.data): |
Comment on lines
+200
to
+202
| # Control frames: display printable ASCII payloads as text, else binary. | ||
| # We could decode UTF-8 payloads, but this causes confusion when random | ||
| # 4-bytes binary payloads are accidentally valid UTF-8 sequences. |
Comment on lines
+375
to
+376
| str(Frame(PING, b"dG\x04I")), | ||
| "PING 64 47 04 49 [binary, 4 bytes]", |
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.


This only affects debug logs.
spaces (which will be escaped in the representation). This avoids
treating our random 4-bytes payloads as text when they're UTF-8,
which happens a bit more that 1 time out of 16. (Chances that it's
ASCII are 1 in 16, and then in can be UTF-8 in a few more cases.)
Refs #1763.