Skip to content

fix: Improve how we deal with WS closures#34

Merged
sanbotto merged 3 commits into
mainfrom
fix/properly-close-ws-connections
May 15, 2026
Merged

fix: Improve how we deal with WS closures#34
sanbotto merged 3 commits into
mainfrom
fix/properly-close-ws-connections

Conversation

@sanbotto
Copy link
Copy Markdown
Member

@sanbotto sanbotto commented May 14, 2026

This makes sure the connection is always properly closed by aetherlay whenever the remote endpoint closes it for any reason. In some cases, we track the closure as an issue, but we always close the connection.

This should fix an issue where the end user has a socket open but no data flows through it. I have to run more tests.

Summary by CodeRabbit

  • Bug Fixes

    • Improved WebSocket failure semantics so backend "graceful" closes are not treated as errors while unexpected backend errors mark endpoints unhealthy.
    • Distinguish echoed close frames from client-initiated disconnects to avoid false health failures.
    • Client disconnects and drops are handled gracefully without marking endpoints unhealthy.
  • Tests

    • Added integration tests covering various backend and client WebSocket close codes and health updates.

Review Change Stack

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

coderabbitai Bot commented May 14, 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: 7cd44394-e3ab-4a4c-9508-5b2805be6f2e

📥 Commits

Reviewing files that changed from the base of the PR and between 952224f and 058ecd3.

📒 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

defaultProxyWebSocket now runs two concurrent copy goroutines and uses directional results plus an atomic flag to attribute close frames. proxyWebSocketCopy was changed to report whether a close frame was sent. Backend graceful closes (1000, 1001, and echoed no-status) return nil and do not mark the endpoint unhealthy; unexpected backend close codes or backend copy errors mark the ws endpoint unhealthy and return the error. Client-initiated disconnects are treated as non-failures. New tests exercise backend- and client-initiated close behaviors and health updates.

🚥 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 'fix: Improve how we deal with WS closures' directly summarizes the main change: improving WebSocket closure handling in the proxy server, including proper connection closure and better error attribution.
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/properly-close-ws-connections

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 1317-1330: Change proxyWebSocketCopy to return (error, bool) where
the bool indicates whether the error came from Read (readErr) vs Write, then
update the two goroutines that call proxyWebSocketCopy(backendConn, clientConn)
and proxyWebSocketCopy(clientConn, backendConn) to send wsResult using the
returned readErr rather than the goroutine-sourced fromBackend flag; use the
readErr to attribute the failure to the reader peer and call
markEndpointUnhealthyProtocol against backend or client accordingly. Ensure
wsResult (and any consumers) accept the new readErr flag, propagate it through
errc, and adjust the code that waits on errc and performs
clientConn.Close()/backendConn.Close() to use the new attribution logic.
🪄 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: 2c8700af-8aa3-4ca5-a366-3b3acae59c73

📥 Commits

Reviewing files that changed from the base of the PR and between 59f3d52 and 952224f.

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

Comment thread internal/server/server.go Outdated
@sanbotto sanbotto merged commit b434c2d into main May 15, 2026
6 checks passed
@sanbotto sanbotto deleted the fix/properly-close-ws-connections branch May 15, 2026 13: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.

1 participant