Skip to content

fix(websocket): add close reasons and suppress noisy warnings#4476

Open
NathanFlurry wants to merge 1 commit intomainfrom
empty-close-reason-log
Open

fix(websocket): add close reasons and suppress noisy warnings#4476
NathanFlurry wants to merge 1 commit intomainfrom
empty-close-reason-log

Conversation

@NathanFlurry
Copy link
Member

Description

This PR fixes WebSocket close handling to provide complete close reason information and reduces noisy logging for normal disconnect scenarios.

When WebSocket connections close, the close reason was either missing entirely or would trigger warnings even on normal disconnects (network drops, browser closes). This fix ensures all closes include proper reason codes for debugging while only warning on actually malformed reasons.

Changes

  • Add close reason codes to all bare .close() calls using the group.code format
  • Suppress "failed to parse close reason" warnings when reason is empty (normal case for network disconnects)
  • Standardize close reason formats across codebase (pegboard., ws., sandbox.*)

Type of change

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

The changes suppress logging on normal WebSocket disconnects. Close reasons now follow the standard format for proper error tracking and debugging through the logging system.

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my code
  • My changes generate no new warnings

…s noisy warnings

Add proper close reason codes to all WebSocket close calls that were missing
them. This ensures complete close reason information flows through the system
for debugging and logging. Also suppress the "failed to parse close reason"
warning when the reason is empty, since this is a normal case for network
disconnects and browser-initiated closes.

Changes:
- Add close reasons following group.code format to all bare .close() calls
- Only warn about malformed close reasons if the reason is non-empty
- Update close reason formats to be consistent (pegboard.*, ws.*, sandbox.*)

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
@railway-app
Copy link

railway-app bot commented Mar 22, 2026

🚅 Deployed to the rivet-pr-4476 environment in rivet-frontend

Service Status Web Updated (UTC)
frontend-cloud 😴 Sleeping (View Logs) Web Mar 24, 2026 at 11:49 am
frontend-inspector 😴 Sleeping (View Logs) Web Mar 22, 2026 at 8:42 am
website ❌ Build Failed (View Logs) Web Mar 22, 2026 at 8:36 am
kitchen-sink ❌ Build Failed (View Logs) Web Mar 22, 2026 at 8:35 am
mcp-hub ✅ Success (View Logs) Web Mar 22, 2026 at 8:34 am
ladle ❌ Build Failed (View Logs) Web Mar 22, 2026 at 8:33 am

@claude
Copy link

claude bot commented Mar 22, 2026

PR Review: fix(websocket): add close reasons and suppress noisy warnings

Overall this is a clean, focused fix. A few observations:

Duplicate branch in ws-proxy.ts (minor)

After the change, both branches in onError are now identical:

if (state.targetWs.readyState === WebSocket.OPEN) {
    state.targetWs.close(1011, "ws.client_error");
} else if (state.targetWs.readyState === WebSocket.CONNECTING) {
    state.targetWs.close(1011, "ws.client_error");
}

These can be collapsed:

if (state.targetWs.readyState === WebSocket.OPEN || state.targetWs.readyState === WebSocket.CONNECTING) {
    state.targetWs.close(1011, "ws.client_error");
}

Incomplete standardization in ws-proxy.ts (follow-up)

The same file still has several non-group.code format close reasons not touched by this PR:

  • Line 56: targetWs.close(1001, "Client disconnected")
  • Line 102: closeWebSocketIfOpen(clientWs, 1011, "Target WebSocket error")
  • Lines 135-136: closeWebSocketIfOpen(clientWs, 1011, "Failed to connect to target")
  • Line 155: state.targetWs.close(1000, event.reason || "Client disconnected")

Not a blocker, but worth a follow-up to keep parseWebSocketCloseReason parsing consistent across the file.

Core fixes look correct

  • Suppressing warnings on empty reason strings is correct. Empty reasons are the normal case for browser tab closes and network drops.
  • The group.code format standardization (pegboard.shutdown_error, ws.client_error, sandbox.client_closed) aligns with the existing parsing convention in parseWebSocketCloseReason.
  • Using 1011 (internal error) for the previously bare .close() on a CONNECTING socket in ws-proxy.ts is semantically correct for the error path.
  • The Swift client change mirrors the TypeScript logic appropriately.

@pkg-pr-new
Copy link

pkg-pr-new bot commented Mar 22, 2026

More templates

@rivetkit/cloudflare-workers

pnpm add https://pkg.pr.new/rivet-dev/rivet/@rivetkit/cloudflare-workers@4476

@rivetkit/framework-base

pnpm add https://pkg.pr.new/rivet-dev/rivet/@rivetkit/framework-base@4476

@rivetkit/next-js

pnpm add https://pkg.pr.new/rivet-dev/rivet/@rivetkit/next-js@4476

@rivetkit/react

pnpm add https://pkg.pr.new/rivet-dev/rivet/@rivetkit/react@4476

rivetkit

pnpm add https://pkg.pr.new/rivet-dev/rivet/rivetkit@4476

@rivetkit/sql-loader

pnpm add https://pkg.pr.new/rivet-dev/rivet/@rivetkit/sql-loader@4476

@rivetkit/sqlite-vfs

pnpm add https://pkg.pr.new/rivet-dev/rivet/@rivetkit/sqlite-vfs@4476

@rivetkit/traces

pnpm add https://pkg.pr.new/rivet-dev/rivet/@rivetkit/traces@4476

@rivetkit/workflow-engine

pnpm add https://pkg.pr.new/rivet-dev/rivet/@rivetkit/workflow-engine@4476

@rivetkit/virtual-websocket

pnpm add https://pkg.pr.new/rivet-dev/rivet/@rivetkit/virtual-websocket@4476

@rivetkit/engine-runner

pnpm add https://pkg.pr.new/rivet-dev/rivet/@rivetkit/engine-runner@4476

@rivetkit/engine-runner-protocol

pnpm add https://pkg.pr.new/rivet-dev/rivet/@rivetkit/engine-runner-protocol@4476

commit: 384d66b

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