rpc: cancel in-flight op on fatal read error to fix Call hang#20932
Merged
rpc: cancel in-flight op on fatal read error to fix Call hang#20932
Conversation
…n close The readErr handler in dispatch was passing lastOp to cancelAllRequests, which deliberately preserves the inflight op's resp channel — contradicting the comment right above that says all pending requests must be cancelled. When readErr won the select against reqSent, the in-flight op was orphaned: respWait was emptied, lastOp was reset to nil, but op.resp was never closed, so op.wait blocked forever on a channel that no one would ever close or send on. This was the underlying race behind the long-running flake of TestWebsocketLargeCall (#16875). Pass nil to cancelAllRequests on a fatal read error so the inflight op is cancelled along with the rest, and clear lastOp so a concurrent reconnect doesn't re-register an op whose resp is already closed (a later send on it would panic). Nil-guard addRequestOp in the reconnected handler for the same reason. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes a race in the RPC websocket client dispatch loop where a fatal read error could leave an in-flight request orphaned, causing Call/BatchCall to hang indefinitely waiting on an unclosed op.resp.
Changes:
- Treat fatal
readErras cancelling all pending requests (including the in-flightlastOp) by passingniltoconn.close. - Clear
lastOpafter fatal read error to prevent reconnect from re-registering an op whoserespchannel has already been closed. - Add a nil-guard when re-registering
lastOpon reconnect.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
After the readErr branch clears lastOp, a write error already in flight on the send goroutine can still arrive on c.reqSent. Without a guard, removeRequestOp(nil) would dereference op.ids and panic. Per Copilot review on #20932. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Giulio2002
approved these changes
May 1, 2026
Collaborator
Giulio2002
left a comment
There was a problem hiding this comment.
LGTM — small, targeted fix for a reconnect/read-error edge case: clear the cancelled in-flight op and guard the later re-register/remove paths to avoid reusing a dead response channel.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Client.dispatch(rpc/client.go) where a fatalreadErrracing againstreqSentcould orphan the in-flight op'srespchannel, leavingop.waitblocked forever on a channel nobody would ever close.lastOptocancelAllRequests, which deliberately preserves the inflight op's resp — directly contradicting the comment immediately above:nilinstead so the inflight op is cancelled too. ClearlastOpso a concurrent reconnect doesn't re-register an op whose resp is already closed (a laterop.resp <- batchwould panic). Nil-guardaddRequestOpin the reconnect handler accordingly.Why
Surfaced as the long-running CI flake #16875 —
TestWebsocketLargeCallhanging for ~59 minutes until the test timeout fires. From a recent failing run's goroutine dump:(*requestOp).wait(rpc/client.go:144) for 58 minutes — i.e., pastc.send, with the write already returned;Client.dispatchwas idle inselect(rpc/client.go:597) for the same 58 minutes;coder/websocketRead or Write — both had returned.The only way to reach that state is:
readErrfired,dispatchprocessed it before/instead ofreqSent'slastOp = nil,cancelAllRequestsskipped the in-flight op (becauselastOpwas passed asinflightReq), and soop.respwas never closed. The select betweenc.readErrandc.reqSent(which is buffered, size 1) is a coin flip — that's the flake.Test plan
make lint— cleanmake erigon integration— buildsgo test -race ./rpc/...— full suite passesgo test -race -count=10 -run '^TestWebsocketLargeCall$' ./rpc/— 10/10 pass🤖 Generated with Claude Code