Skip to content

fix: Improve how websockets deal with timeouts and closed connections#33

Merged
sanbotto merged 2 commits into
mainfrom
fix/improve-ws-timeouts
May 12, 2026
Merged

fix: Improve how websockets deal with timeouts and closed connections#33
sanbotto merged 2 commits into
mainfrom
fix/improve-ws-timeouts

Conversation

@sanbotto
Copy link
Copy Markdown
Member

@sanbotto sanbotto commented May 12, 2026

Summary by CodeRabbit

  • Bug Fixes
    • WebSocket close-handshake now forwards close frames correctly, enabling more graceful shutdowns
    • Certain close codes are normalized to avoid treating expected closures as failures
    • Abnormal connection closures are logged distinctly to surface unexpected terminations
    • Coordinated connection teardown reduces resource leaks during concurrent proxying operations

Review Change Stack

Review Change Stack

@sanbotto sanbotto self-assigned this May 12, 2026
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 12, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 22db0619-5279-4aad-8ccf-4c79278fbb46

📥 Commits

Reviewing files that changed from the base of the PR and between 8f08888 and abcb20b.

📒 Files selected for processing (1)
  • internal/server/server.go
🚧 Files skipped from review as they are similar to previous changes (1)
  • internal/server/server.go

Walkthrough

The pull request refines WebSocket proxying behavior in the server by improving close-handshake handling and goroutine coordination. proxyWebSocketCopy now detects close errors from the source connection and forwards the corresponding close frame to the destination instead of only returning the error. The handler synchronization in defaultProxyWebSocket was restructured to wait for one direction to complete, immediately close both connections to unblock the other goroutine, then await the second direction—replacing the previous deferred cleanup pattern. Abnormal closures are now distinguished in logging from expected normal closures.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: improving WebSocket handling for timeouts and closed connections through proper close handshake forwarding and refined connection teardown logic.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/improve-ws-timeouts

Tip

💬 Introducing Slack Agent: The best way for teams to turn conversations into code.

Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.

  • Generate code and open pull requests
  • Plan features and break down work
  • Investigate incidents and troubleshoot customer tickets together
  • Automate recurring tasks and respond to alerts with triggers
  • Summarize progress and report instantly

Built for teams:

  • Shared memory across your entire org—no repeating context
  • Per-thread sandboxes to safely plan and execute work
  • Governance built-in—scoped access, auditability, and budget controls

One agent for your entire SDLC. Right inside Slack.

👉 Get started


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@internal/server/server.go`:
- Around line 1190-1198: The loop that reads from src (calling
src.ReadMessage()) wrongly forwards websocket.CloseError codes 1005/1006/1015
directly to dst via websocket.FormatCloseMessage, which can send forbidden close
codes; update the error handling in that loop to detect a *websocket.CloseError,
map any CloseNoStatusReceived (1005), CloseAbnormalClosure (1006), or 1015 to a
safe code such as websocket.CloseGoingAway before calling
websocket.FormatCloseMessage and dst.WriteMessage, and then write the mapped
close frame (preserving the original Text if desired) so no reserved codes are
sent on the wire.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 3dba10c9-b47f-4049-b258-3247859759bd

📥 Commits

Reviewing files that changed from the base of the PR and between 57251dd and 8f08888.

📒 Files selected for processing (1)
  • internal/server/server.go

Comment thread internal/server/server.go
@sanbotto sanbotto merged commit 59f3d52 into main May 12, 2026
6 checks passed
@sanbotto sanbotto deleted the fix/improve-ws-timeouts branch May 12, 2026 21:58
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