-
Notifications
You must be signed in to change notification settings - Fork 131
fix(rivetkit): fix race condition with websocket open events #3439
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(rivetkit): fix race condition with websocket open events #3439
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
3 Skipped Deployments
|
More templates
@rivetkit/actor
@rivetkit/cloudflare-workers
@rivetkit/core
@rivetkit/db
@rivetkit/framework-base
@rivetkit/next-js
@rivetkit/react
rivetkit
@rivetkit/sql-loader
@rivetkit/engine-runner
@rivetkit/engine-runner-protocol
commit: |
PR Review: Fix Race Condition with WebSocket Open EventsSummaryThis PR addresses a critical race condition in WebSocket connection handling by splitting connection lifecycle into two phases: Code Quality & Architecture✅ Excellent Architectural ChangesConnection Lifecycle Split
Driver Pattern Improvements
🎯 Good Practices
Potential Issues & Concerns
|
abedf42 to
dba206d
Compare
- Created StateManager class for connection state management - Moved state proxying and change tracking to dedicated manager - Changed conn methods to use symbols for internal access: - actor, stateEnabled, persistRaw (getters) - hasChanges(), markSaved(), sendMessage() (methods) - Updated all references across codebase to use new symbols - Maintains backward compatibility for public API (send, disconnect, etc) - Consistent naming pattern with instance/mod.ts StateManager 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
e8b192b to
afc9c8e
Compare
PR Review: Fix Race Condition with WebSocket Open EventsSummaryThis PR addresses a critical race condition in the WebSocket connection lifecycle by splitting connection preparation and connection phases. The changes ensure that WebSocket messages aren't dropped during the connection setup process. Key Changes1. Connection Lifecycle Split ✅Good: The separation of
Why this matters: WebSocket 2. Deferred WebSocket Assignment Pattern ✅The changes to const { driver, setWebSocket } = createWebSocketSocket(...);
const conn = await actor.connectionManager.prepareConn(driver, ...);
// ... later in onOpen (synchronous)
setWebSocket(ws);
actor.connectionManager.connectConn(conn);This ensures the driver exists before the WebSocket is ready, preventing null/undefined access issues. 3. Connection State Guards ✅The addition of // In conn/mod.ts:65-70
#assertConnected() {
if (!this[CONN_CONNECTED_SYMBOL])
throw new InternalError(
"Connection not connected yet. This happens when trying to use the connection in onBeforeConnect or createConnState.",
);
}Good defensive programming that will help catch misuse early. Potential Issues1. Missing Guard in
|

No description provided.