Skip to content

Apply WebSocket subscribe/unsubscribe in the order they were sent - #551

Merged
evantahler merged 2 commits into
mainfrom
claude-evan/focused-tesla-msqz6l
Oct 20, 2026
Merged

evantahler merged 2 commits into
mainfrom
claude-evan/focused-tesla-msqz6l

Conversation

@evantahler

@evantahler evantahler commented Oct 20, 2026 •

Copy link
Copy Markdown
Member

closes #550

Problem

Bun calls websocket.message for each frame without waiting for the previous call to finish. handleWebsocketSubscribe awaits session loading, channel middleware, and presence before it calls connection.subscribe(), while unsubscribe awaits less. So subscribe followed immediately by unsubscribe could be applied in reverse order, leaving the connection subscribed and receiving broadcasts.

Fix (packages/keryx/servers/web.ts)

  • WebServer keeps a per-connection promise chain (wsChannelOpQueue). Each frame is parsed synchronously on arrival so its messageType is known in send order.
  • subscribe / unsubscribe frames wait for the previous channel op and then become the new tail, so they run one at a time in send order.
  • action frames wait for channel ops sent before them (an action that broadcasts to a channel you just joined reaches you), but don't extend the chain. Actions, including long-running streams, still run concurrently and never block later subscribes.
  • handleWebSocketConnectionClose waits for in-flight channel ops before presence cleanup, so cleanup sees the final subscription set.
  • Rate limiting is still applied synchronously on arrival. onMessage hooks still run before dispatch, and errors for bad JSON or an unknown messageType are unchanged.

Tests (__tests__/servers/websocket.test.ts)

These use a channel whose runBefore middleware sleeps 100 ms, like the DB query in the repro:

  • subscribe + unsubscribe in one tick: acks arrive in send order, and a later broadcast PING is not delivered.
  • subscribe, unsubscribe, subscribe: acks arrive in order 1, 2, 3, and the connection ends subscribed.
  • subscribe, then action: the action reply comes after the subscribe ack.
  • slow action, then subscribe: the subscribe ack comes first, so actions don't block later frames.

The first three tests fail on main and pass with this change. The full packages/keryx suite passes except for one test, oauthHandlers "an unreachable document surfaces an error". It fails identically on main in my sandbox because it depends on outbound network access. bun run lint (tsc + biome) is clean.

Other

  • Docs: added a "Message Ordering" section to docs/guide/channels.md.
  • Bumped keryx to 0.48.1.

Generated by Claude Code


Note

Medium Risk
Changes core WebSocket dispatch and subscription state; behavior is more correct but could surface timing differences for clients that relied on the old race.

Overview
Fixes a race where Bun can invoke multiple WebSocket message handlers concurrently, so a quick subscribe then unsubscribe could finish in reverse and leave the client still subscribed (closes #550).

WebServer now keeps a per-connection promise chain for subscribe / unsubscribe frames so they run strictly in send order. Action frames wait only for channel ops that were sent before them (so a join-then-act sequence is safe) but do not block later subscribes or other actions. On disconnect, the server waits for in-flight channel ops before presence cleanup.

Adds regression tests with slow channel middleware, documents per-connection message ordering in the channels guide, and bumps keryx to 0.48.1.

Reviewed by Cursor Bugbot for commit 958b15d. Bugbot is set up for automated code reviews on this repo. Configure here.

Bun calls websocket.message for each frame without waiting for the
previous call to settle, so a subscribe whose channel middleware awaits
anything could be applied after an unsubscribe sent right behind it,
leaving the connection subscribed.

Chain subscribe/unsubscribe frames per connection so they run one at a
time in send order. Action frames wait for channel ops sent before them
but don't block later frames, so actions and streams stay concurrent.
Close waits for in-flight channel ops before presence cleanup.

closes #550

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016G4izCiRyRZ3UcHUHwv4ZK
Comment thread docs/guide/channels.md Resolved
@evantahler
evantahler marked this pull request as ready for review October 20, 2026 20:36
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016G4izCiRyRZ3UcHUHwv4ZK
@evantahler
evantahler enabled auto-merge (squash) October 20, 2026 20:37
@evantahler
evantahler merged commit ffb6126 into main Oct 20, 2026
20 checks passed
@evantahler
evantahler deleted the claude-evan/focused-tesla-msqz6l branch October 20, 2026 20:38
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.

WebSocket frames from one connection are handled concurrently, so subscribe/unsubscribe apply out of order

2 participants