Skip to content

Keep WebSocket pings and closes answered when the handler stops reading - #263

Open
swhitty wants to merge 3 commits into
mainfrom
websocket-handoff-followup
Open

swhitty wants to merge 3 commits into
mainfrom
websocket-handoff-followup

Conversation

@swhitty

@swhitty swhitty commented Nov 15, 2026

Copy link
Copy Markdown
Owner

Follow-ups to #249, in two commits.

AsyncHandoff.send throws CancellationError whenever the sender is cancelled

send was documented to throw CancellationError when the sending task is cancelled, but only checked while suspended. A cancelled sender could still hand its element to a waiting receiver or add it to the buffer.

Pings and closes are still answered once the handler stops reading

Since #249, once 16 client messages wait for the handler, the frame loop stops reading and TCP pushes back on the client, so pings and closes wait too. That is the intended backpressure while the handler is reading. But a handler that has stopped reading for good, such as a broadcast-only handler, held the connection back for messages it would never read.

  • Once the task reading the handler's input is cancelled, or the stream is released, the input is finished and later client messages are dropped. Frames are still read, so pings and closes are still answered, and nothing more is buffered. AsyncStream(unfolding:) releases its closure in both cases, and the closure holds a HandlerInput whose deinit finishes the handoff.
  • The README keeps the advice from Bound the WebSocket frame and message buffers #249: a handler that does not need the client's messages should still read and discard them, or now not keep the stream at all.
  • capacity is now a parameter of MessageFrameWSHandler.init and .webSocket(_:frameSize:capacity:), defaulting to 16, as offered in Bound the WebSocket frame and message buffers #249. Its docs note that each waiting message can be as large as webSocketMaxFrameSize.
  • Only the frame loop ends the handler's input. Teardown could previously finish it first when the output loop ended, losing the close message the frame loop was delivering.
  • The backpressure tests now check that they reach their expected counts, not only that they stay under them.

Relation to #249

@lhoward, this changes code from #249, so I'd appreciate your review. What it keeps, adds and changes:

  • Keeps the bounded capacity, and your reasoning that answering pings however slow the handler is would need an unbounded buffer. Nothing recommends an unbounded capacity.
  • Adds dropping messages once the handler's input is cancelled or released, and the public capacity.
  • Changes teardown: messagesIn is no longer finished as soon as either loop ends; the frame loop finishes it when it is cancelled. framesOut is still finished right away, so the connection still closes when the handler's output ignores cancellation. Socket reads and handoff sends both respond to cancellation, so the handler's input still ends promptly.
  • Changes AsyncHandoff.send to check for cancellation before handing an element off.

Testing

  • New tests: a ping is answered after more messages than the capacity when the handler cancels reading its input but keeps it, and when it releases it; a large capacity keeps every message; send throws CancellationError when cancelled with buffer space or with a waiting receiver.
  • On macOS the full suite passes on the branch and merged with the current main. The WebSocket and AsyncHandoff suites passed 30 runs in a row. Not run on Linux locally.

🤖 Generated with Claude Code

@codecov

codecov Bot commented Nov 15, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.93%. Comparing base (90dbd69) to head (2170ff6).
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #263      +/-   ##
==========================================
+ Coverage   93.75%   93.93%   +0.17%     
==========================================
  Files          73       73              
  Lines        4022     4042      +20     
==========================================
+ Hits         3771     3797      +26     
+ Misses        251      245       -6     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

@swhitty
swhitty force-pushed the websocket-handoff-followup branch 4 times, most recently from 0ad8d20 to 99ce2bc Compare November 15, 2026 22:04
… cancelled

send was documented to throw CancellationError when the sending task is
cancelled, but only checked while suspended. A cancelled sender could still
hand its element to a waiting receiver or add it to the buffer.
Follow-up to #249:

- Finish the handler's input once the task reading it is cancelled or the
  stream is released, and drop later client messages, so frames are still
  read and pings and closes still answered. Tests cover a handler that
  cancels while keeping its input, and one that releases it.
- Keep the advice from #249: a handler that does not need the client's
  messages should still read and discard them, or now not keep its input
  at all.
- Make capacity a public parameter of MessageFrameWSHandler.init and
  HTTPHandler.webSocket, as offered in #249. Its documentation notes that
  each waiting message can be as large as webSocketMaxFrameSize.
- Only the frame loop ends the handler's input. Teardown could previously
  finish it first when the output loop ended, losing the close message.
- Check the backpressure tests reach their expected counts, not only that
  they stay under them.
- Cite RFC 6455 §5.5.1 for the close echo, and stop finishing the input
  with a Result that cannot fail.
@swhitty
swhitty force-pushed the websocket-handoff-followup branch from 99ce2bc to 760d548 Compare November 16, 2026 00:56
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.

1 participant