Skip to content

Handle empty and absent payloads in WebSocketEndpoint.decode() - #3372

Merged
Kludex merged 5 commits into
Kludex:mainfrom
lkk7:fix/websocket-endpoint-empty-text
Sep 6, 2026
Merged

Kludex merged 5 commits into
Kludex:mainfrom
lkk7:fix/websocket-endpoint-empty-text

Conversation

@lkk7

@lkk7 lkk7 commented Jul 7, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Check Noneness instead of Trueness in the WebSocketEndpoint.decode() method.
Fixes issue in discussion #3369

Update:

Addressed a review comment. text/bytes branches in decode() only checked key presence ("text" not in message), not the value.

ASGI spec says:

Exactly one of bytes or text must be non-None. One or both keys may be present, however.

That could result in ASGI compliant message silently returning None instead of raising an error. So fixed that too.

Checklist

  • I understand that this PR may be closed in case there was no previous discussion. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.

Review in cubic

@cubic-dev-ai cubic-dev-ai Bot 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.

1 issue found across 2 files

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Comment thread starlette/endpoints.py
@lkk7 lkk7 changed the title Fix KeyError on empty text frame in WebSocketEndpoint.decode() Fix bad handling of empty/falsy message values inWebSocketEndpoint.decode() Jul 7, 2026
@lkk7
lkk7 force-pushed the fix/websocket-endpoint-empty-text branch from 9991f71 to 91e0055 Compare July 7, 2026 11:14

@Mukller Mukller 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.

Note for maintainers: this is a byte-for-byte identical fix to #3381 (same hunks in endpoints.py, same bug). I verified the before/after behavior locally — details and evidence are in my review on #3381; everything applies here too:

  • main: empty text frame ({'text': ''}) hits the falsy check → KeyError: 'bytes'
  • this branch: delivered as '', 28/28 endpoint tests green

Whichever of the two you pick is correct; the other can be closed as a duplicate.

@lkk7

lkk7 commented Aug 23, 2026

Copy link
Copy Markdown
Contributor Author

@Mukller thanks Claude!

@Kludex Kludex changed the title Fix bad handling of empty/falsy message values inWebSocketEndpoint.decode() Handle empty and absent payloads in WebSocketEndpoint.decode() Sep 6, 2026
@codspeed

codspeed Bot commented Sep 6, 2026 •

Copy link
Copy Markdown

Merging this PR will improve performance by 13.11%

⚡ 1 improved benchmark
✅ 241 untouched benchmarks

Performance Changes

Mode Benchmark BASE HEAD Efficiency
⚡ Simulation test_urlencoded_form[field-1MiB-64KiB] 8.9 ms 7.9 ms +13.11%

Tip

Curious why performance improved? Comment @codspeedbot explain why performance improved on this PR, or directly use the CodSpeed MCP with your agent.


Comparing lkk7:fix/websocket-endpoint-empty-text (8d612fa) with main (0fcaff1)

Open in CodSpeed

@Kludex
Kludex merged commit b4c1d18 into Kludex:main Sep 6, 2026
12 checks passed
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.

3 participants