Skip to content

Tighten heuristics for representing control frames. - #1770

Merged
aaugustin merged 1 commit into
mainfrom
improve-ctrl-frames-repr
Sep 24, 2026
Merged

aaugustin merged 1 commit into
mainfrom
improve-ctrl-frames-repr

Conversation

@aaugustin

Copy link
Copy Markdown
Member

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.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 Medium severity · 2 Low severity

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.

Comment thread src/websockets/frames.py
Comment thread src/websockets/frames.py
Comment thread src/websockets/frames.py
Comment thread tests/test_frames.py
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.
@aaugustin
aaugustin force-pushed the improve-ctrl-frames-repr branch from 7ac9ba8 to a591d62 Compare September 24, 2026 20:36
@aaugustin
aaugustin merged commit a591d62 into main Sep 24, 2026
8 checks passed
@aaugustin
aaugustin deleted the improve-ctrl-frames-repr branch September 24, 2026 20:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants