Repository navigation
Apply WebSocket subscribe/unsubscribe in the order they were sent - #551
Merged
Merged
Conversation
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
evantahler
commented
Oct 20, 2026
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
closes #550
Problem
Bun calls
websocket.messagefor each frame without waiting for the previous call to finish.handleWebsocketSubscribeawaits session loading, channel middleware, and presence before it callsconnection.subscribe(), while unsubscribe awaits less. Sosubscribefollowed immediately byunsubscribecould be applied in reverse order, leaving the connection subscribed and receiving broadcasts.Fix (
packages/keryx/servers/web.ts)WebServerkeeps a per-connection promise chain (wsChannelOpQueue). Each frame is parsed synchronously on arrival so itsmessageTypeis known in send order.subscribe/unsubscribeframes wait for the previous channel op and then become the new tail, so they run one at a time in send order.actionframes 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.handleWebSocketConnectionClosewaits for in-flight channel ops before presence cleanup, so cleanup sees the final subscription set.onMessagehooks still run before dispatch, and errors for bad JSON or an unknownmessageTypeare unchanged.Tests (
__tests__/servers/websocket.test.ts)These use a channel whose
runBeforemiddleware sleeps 100 ms, like the DB query in the repro:PINGis not delivered.The first three tests fail on
mainand pass with this change. The fullpackages/keryxsuite passes except for one test,oauthHandlers"an unreachable document surfaces an error". It fails identically onmainin my sandbox because it depends on outbound network access.bun run lint(tsc + biome) is clean.Other
docs/guide/channels.md.keryxto 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
messagehandlers concurrently, so a quick subscribe then unsubscribe could finish in reverse and leave the client still subscribed (closes #550).WebServernow 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.