Skip to content

Discard stale WebSocket state when reconnecting - #617

Merged
bsergean merged 2 commits into
masterfrom
fix/websocket-reconnect-state
Oct 15, 2026
Merged

bsergean merged 2 commits into
masterfrom
fix/websocket-reconnect-state

Conversation

@bsergean

@bsergean bsergean commented Oct 15, 2026 •

Copy link
Copy Markdown
Collaborator

When a connection drops during a fragmented receive or a backpressured send, the transport retains state from that session. A reconnect can combine OLD from the previous connection with NEW from the next, or send an old frame tail before the next message.

Reset queued output, incomplete receive data, receive targets and fragment assembly state before a new client or server handshake. Finish any active application send before resetting and replacing the transport, so it cannot append old continuation frames after the reset. The wait remains cancellable so a send progress callback can call stop() without deadlocking. Release the write lock before Open callbacks so those callbacks can send normally.

Queued bytes belong to their original connection and are discarded on reconnect; this does not add automatic message replay.

Validation:

  • Six local test executables passed: reconnect, server, close, subprotocol, send timeout, and native Network.framework TLS.
  • New loopback tests cover automatic reconnects, incomplete frames, queued output, failed reconnect attempts an interrupted fragmented send, and stopping/restarting from its progress callback.
  • Removing the reset makes all four regression cases fail. Removing only send coordination makes the interrupted-send case fail independently.
  • Replacing the cancellable wait with a blocking lock reproduces a deadlock in the progress-callback stop test.
  • New test formatting and git diff --check passed.

Fixes #610.
Fixes #611.

Discard queued wire frames, partial receive data, receive targets and
fragment assembly state before a new client or server handshake. Old
connection data must not be replayed or joined with a new connection's
messages.

Serialize handshakes with active application sends, releasing the write
lock before Open callbacks so callbacks can still send normally. This
prevents an interrupted fragmented send from appending old continuations
after the new session's reset.

Add loopback regressions for automatic reconnect, partial frames, queued
output, failed reconnect attempts and a send paused between fragments.
The tests fail without the reset; removing send coordination independently
reproduces cross-connection continuation writes.

Fixes #610
Fixes #611
@bsergean
bsergean merged commit 750341d into master Oct 15, 2026
7 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.

Should the outgoing buffer be reset on a reconnect? Should the outgoing buffer be reset on a reconnect?

1 participant