Skip to content

rpc: send clean close frame on websocket disconnect#20788

Merged
yperbasis merged 6 commits into
erigontech:mainfrom
Sahil-4555:rpc/fix-ws-abnormal-closure
Apr 28, 2026
Merged

rpc: send clean close frame on websocket disconnect#20788
yperbasis merged 6 commits into
erigontech:mainfrom
Sahil-4555:rpc/fix-ws-abnormal-closure

Conversation

@Sahil-4555

Copy link
Copy Markdown
Contributor

Currently, when a WebSocket connection is closed by the RPC server, the underlying TCP connection gets torn down immediately without sending a final WebSocket Close frame. Because of this abrupt teardown, clients connected to the node experience an abnormal closure (error code 1006) instead of a clean, normal disconnect (error code 1000).

This PR fixes that behavior by explicitly sending a clean 1000 normal closure frame right before terminating the connection. It uses the Close() method from the coder/websocket library to safely transmit this control frame.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR aims to make RPC server–initiated WebSocket disconnects appear as a clean, normal WebSocket closure (1000) to peers, instead of an abnormal closure (1006), by explicitly sending a Close control frame before tearing down the connection.

Changes:

  • Send a WebSocket Close frame (StatusNormalClosure) in websocketCodec.Close() prior to shutting down the JSON codec and ping loop.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread rpc/websocket.go Outdated
Comment thread rpc/websocket.go Outdated
Comment thread rpc/websocket.go Outdated
Comment thread rpc/websocket.go Outdated

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread rpc/websocket.go Outdated
Comment thread rpc/websocket_test.go
yperbasis
yperbasis previously approved these changes Apr 27, 2026
@yperbasis

Copy link
Copy Markdown
Member

@Sahil-4555 fix lint plz

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread rpc/websocket.go Outdated
Comment thread rpc/websocket.go Outdated
Comment thread rpc/websocket_test.go Outdated
@yperbasis yperbasis dismissed their stale review April 27, 2026 07:19

let's address copilot's comments

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@yperbasis yperbasis added this pull request to the merge queue Apr 28, 2026
Merged via the queue into erigontech:main with commit 9dc5ae5 Apr 28, 2026
35 of 36 checks passed
@anacrolix

Copy link
Copy Markdown
Contributor

I thought there was a helper in the websocket library for this so you don't need to spin up a bunch of timers and goroutines?

yperbasis added a commit that referenced this pull request Apr 29, 2026
Replace the manual goroutine + timer pattern in wsConnAdapter.Close with
a direct call to coder/websocket Conn.Close(StatusNormalClosure, ""),
which already performs the close handshake with bounded internal
timeouts (5s write, 5s read). Follow-up to #20788.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@yperbasis

Copy link
Copy Markdown
Member

I thought there was a helper in the websocket library for this so you don't need to spin up a bunch of timers and goroutines?

Good point. I've raise follow-up PR #20913

@Sahil-4555

Copy link
Copy Markdown
Contributor Author

I thought there was a helper in the websocket library for this so you don't need to spin up a bunch of timers and goroutines?

Good point. I've raise follow-up PR #20913

I have done first commit as per it changes you have done in the followup PR. As due to copilot commets i have to do following commit changes as per it to resolve it

@Sahil-4555

Copy link
Copy Markdown
Contributor Author

Some times copilot commets feels too overcomplex

@Sahil-4555 Sahil-4555 deleted the rpc/fix-ws-abnormal-closure branch April 30, 2026 03:30
Sahil-4555 pushed a commit to Sahil-4555/erigon that referenced this pull request Apr 30, 2026
)

## Summary

Follow-up to erigontech#20788. Address review feedback from @anacrolix:

> I thought there was a helper in the websocket library for this so you
don't need to spin up a bunch of timers and goroutines?

The `coder/websocket` `Conn.Close(code, reason)` method already performs
the close handshake with bounded internal timeouts (5s write, 5s read),
so the manual goroutine + timer plumbing in `wsConnAdapter.Close` is
redundant. Replace it with a direct call.

The existing `TestWebsocketServerGracefulClose` test still passes —
clients see a clean `StatusNormalClosure` (1000) instead of
`StatusAbnormalClosure` (1006).

## Test plan

- [x] `go test ./rpc/ -run Websocket` — all pass
- [x] `make lint` — clean

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Co-authored-by: info@weblogix.biz <admin@10gbps.weblogix.it>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants