Skip to content

fix(rpc): stop a JSON-RPC batch whose caller has disconnected - #1610

Merged
QuantumExplorer merged 2 commits into
devfrom
fix/rpc-stop-abandoned-batch
Nov 10, 2026
Merged

QuantumExplorer merged 2 commits into
devfrom
fix/rpc-stop-abandoned-batch

Conversation

@QuantumExplorer

@QuantumExplorer QuantumExplorer commented Nov 10, 2026 •

Copy link
Copy Markdown
Member

TL;DR: A JSON-RPC batch now stops running its calls once the caller has closed the connection.

User story

As a service that reads blocks from Tenderdash over JSON-RPC (such as DAPI), I want Tenderdash to stop working on a batch I have given up on, so that the load I put on the node ends when I stop waiting.

Scenario

Base flow

A client posts a batch of calls, for example block, block_results and commit for ten heights, which is 30 calls. Its own caller goes away or its deadline passes, so it closes the connection before the answer arrives.

Actual behavior

Tenderdash runs every remaining call of the batch and then tries to write an answer nobody reads. A client cannot tell from its side when that work is over, so it cannot safely count a request against a concurrency limit only for as long as it waits.

batch of 30 calls, caller disconnects during call 3  ->  calls 4..30 still run

Expected behavior

The batch stops before its next call, and nothing is written.

batch of 30 calls, caller disconnects during call 3  ->  call 3 finishes, calls 4..30 do not run

Detailed discussion

What was done

  • rpc/jsonrpc/server/http_json_handler.go: the batch loop checks the request's context before each call and returns once it is cancelled. Go's HTTP server cancels that context when the client's connection closes.
  • A single call, or the call of a batch that is already running, still runs to its end: the RPC methods (block, block_results, commit and the rest) do not watch their context. So the work left behind by a caller is now at most one call, not the rest of the batch.

Testing

  • TestRPCBatchStopsWhenCallerIsGone (http_json_handler_test.go): a batch of three calls with the caller staying, leaving before the first call, during the first and during the second. Without the fix the last three cases fail.
  • TestServeStopsBatchWhenCallerDisconnects (http_server_test.go): the same through Serve on a real TCP connection that the client closes during the first call. The call sees its context cancelled and the two calls after it do not run.
  • go test ./rpc/jsonrpc/... passes locally on macOS arm64. gofmt and go vet are clean for the package. The full test suite and the linters were not run locally.

Breaking changes

None. A caller that stays connected gets the same answer as before.

Prior work

Attribution

🤖 Generated with Claude Code

PR Hygiene · a464e56

  • Bots — thepastaclaw ✓
  • Self-review — post /self-reviewed
  • Build green
  • Approvals
    • files with no dedicated owner (rpc/jsonrpc/server/http_json_handler.go, rpc/jsonrpc/server/http_json_handler_test.go, rpc/jsonrpc/server/http_server_test.go) — approved by lklimek

When every merge requirement is met, the PR Hygiene check passes. Reviewer limits do not block merging; other required GitHub checks and protections still apply.

A batch ran every one of its calls even after the caller had closed the
connection, so nobody was left to read the answers. The handler now checks
the request's context before each call and stops once it is cancelled.

A single call, or the call of a batch that is already running, still runs
to its end: the methods do not watch their context.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
@coderabbitai

coderabbitai Bot commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

@github-actions

Copy link
Copy Markdown

Waiting for bot review — thepastaclaw not yet. Wait for the missing reviews, or a writer can post /skip-bots to proceed without them; blocking findings still need addressing.
Full checklist in the description.

@github-actions github-actions Bot added the waiting-bots Waiting for the review bots to report on this head label Nov 10, 2026
@thepastaclaw

thepastaclaw commented Nov 10, 2026 •

Copy link
Copy Markdown

✅ Final review complete — no blockers (commit a464e56) · triage: low

@thepastaclaw thepastaclaw left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Final validation — Phase 1 + Phase 2

The implementation correctly stops a JSON-RPC batch before starting another call after the HTTP request context is canceled, and the added tests cover both direct handler cancellation and real TCP disconnects. Two non-blocking issues remain: the new Serve-level test lacks the leaktest guard used by its sibling tests, and golangci-lint fails on the new US-English spelling check for “cancelled.”

💬 2 nitpick(s)

Approved: nothing here blocks the merge. The non-blocking findings are optional: fix them forward, in this PR or a follow-up, without waiting for another review round.

Review provenance

Source: reviewer 1: glm-5.3-flash (agent: phase1-reviewer, role: general); reviewer 2: gpt-6.1-sol (agent: phase2-reviewer, role: general); reviewer 3: gpt-6.1-sol (agent: phase2-reviewer, role: tenderdash-consensus-security); final verifier: gpt-6.1-sol (agent: sol-verifier, role: final-verifier)

  • Triage: low by gpt-6.1-sol (effort low) — This is a small, well-contained HTTP JSON-RPC request-cancellation change with focused tests and straightforward behavior to verify.
  • Phase 1 reviewers: glm-5.3-flash — general (completed, effort high); agent phase1-reviewer
  • Phase 1 model: glm-5.3-flash — zai quota: 5h 99% left, weekly 91% left; passed over gemini-3.8-flash-high (antigravity quota lookup failed)
  • Fresh verifier: gpt-6.1-sol — final-verifier; agent sol-verifier
  • Phase 2 reviewers: gpt-6.1-sol — general (completed, effort medium); agent phase2-reviewer, gpt-6.1-sol — tenderdash-consensus-security (completed, effort medium); agent phase2-reviewer
🤖 Prompt for all review comments with AI agents
These findings are from an automated code review (PastaClaw). How to work through them:
- Verify each finding against the code; fix only what is real, as narrowly as the finding.
- If a finding is wrong, decline it on its thread in one or two sentences; the bot answers once and stops.
- To defer one, link a tracking issue on its thread (or reply asking the bot to file one).
- Suggestions are optional: fix them forward in a later PR rather than pushing another round just for them.
- Address every current finding in one push: self-review the full diff first, and don't push piecemeal.
- Keep the PR's scope; don't grow it to satisfy hardening suggestions.
- Carried items were raised in an earlier round: read the thread first, and don't re-fix what you already fixed, declined or deferred there. Disputed and deferred items are already handled and are not listed.

In `rpc/jsonrpc/server/http_server_test.go`:
- [NITPICK] rpc/jsonrpc/server/http_server_test.go:96-98: Serve-level cancellation test does not check for goroutine leaks
  This test starts both the Serve goroutine and Serve's internal shutdown-watcher goroutine, but it does not register the leaktest cleanup used by TestMaxOpenConnections and TestServeTLS in the same file. The goroutines exit on the current path, but without the guard a future shutdown regression could leave them behind unnoticed. Add the existing leaktest check for consistency and coverage of this lifecycle test.
- [NITPICK] rpc/jsonrpc/server/http_server_test.go:94-95: golangci-lint rejects the new spelling in the test comment
  The new comment uses “cancelled” at line 95, while .golangci.yml configures misspell with the US locale. The golangci-lint check is therefore failing on code introduced by this PR; change the spelling to “canceled” before merge.

Comment on lines +96 to +98
func TestServeStopsBatchWhenCallerDisconnects(t *testing.T) {
ln, err := net.Listen("tcp", "localhost:0")
require.NoError(t, err)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💬 Nitpick: Serve-level cancellation test does not check for goroutine leaks

This test starts both the Serve goroutine and Serve's internal shutdown-watcher goroutine, but it does not register the leaktest cleanup used by TestMaxOpenConnections and TestServeTLS in the same file. The goroutines exit on the current path, but without the guard a future shutdown regression could leave them behind unnoticed. Add the existing leaktest check for consistency and coverage of this lifecycle test.

Suggested change
func TestServeStopsBatchWhenCallerDisconnects(t *testing.T) {
ln, err := net.Listen("tcp", "localhost:0")
require.NoError(t, err)
t.Cleanup(leaktest.Check(t))

source: glm-5.3-flash (phase1-reviewer: general)

Comment thread rpc/jsonrpc/server/http_server_test.go Outdated
Comment on lines +94 to +95
// A caller that closes its connection in the middle of a batch ends the batch: the call in
// progress sees its context cancelled, and the calls after it do not run.

@thepastaclaw thepastaclaw Nov 10, 2026 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✅ Resolved at a464e56a; see the replies below.

💬 Nitpick: golangci-lint rejects the new spelling in the test comment

The new comment uses “cancelled” at line 95, while .golangci.yml configures misspell with the US locale. The golangci-lint check is therefore failing on code introduced by this PR; change the spelling to “canceled” before merge.

Suggested change
// A caller that closes its connection in the middle of a batch ends the batch: the call in
// progress sees its context cancelled, and the calls after it do not run.
// A caller that closes its connection in the middle of a batch ends the batch: the call in
// progress sees its context canceled, and the calls after it do not run.

source: glm-5.3-flash (phase1-reviewer: general)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Resolved (re-reviewed at a464e56a): Your spelling correction at http_server_test.go:95 addresses the reported lint failure, and golangci-lint passes on this head.

@github-actions

Copy link
Copy Markdown

Bots are done — your move: post /self-reviewed.
Full checklist in the description.

@github-actions github-actions Bot added waiting-self-review Waiting for the author to post /self-reviewed and removed waiting-bots Waiting for the review bots to report on this head labels Nov 10, 2026
@github-actions

github-actions Bot commented Nov 10, 2026 •

Copy link
Copy Markdown

Waiting for bot review — thepastaclaw not yet. Wait for the missing reviews, or a writer can post /skip-bots to proceed without them; blocking findings still need addressing.
Full checklist in the description.

@github-actions github-actions Bot added waiting-bots Waiting for the review bots to report on this head and removed waiting-self-review Waiting for the author to post /self-reviewed labels Nov 10, 2026
@lklimek
lklimek enabled auto-merge (squash) November 10, 2026 12:51

@thepastaclaw thepastaclaw left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Re-review — Final validation — Phase 1 + Phase 2

At a464e56, the only change since the previous review corrects the test comment's spelling, and golangci-lint passes. Static inspection confirms cancellation is checked before each batch call; no new production defect was found. CI's tests (00) failure is unrelated: the unchanged consensus load test charged 137 work units against a timing-dependent requirement of more than 137.1863; the remaining build, test, and lint checks pass.

💬 1 nitpick(s)

Approved: nothing here blocks the merge. The non-blocking findings are optional: fix them forward, in this PR or a follow-up, without waiting for another review round.

Still open from earlier rounds (1)

  • 💬 Nitpick: Serve-level cancellation test does not check for goroutine leaks — rpc/jsonrpc/server/http_server_test.go:96 (thread)
Review provenance

Source: reviewer 1: glm-5.3-flash (agent: phase1-reviewer, role: general); reviewer 2: gpt-6.1-sol (agent: phase2-reviewer, role: general); reviewer 3: gpt-6.1-sol (agent: phase2-reviewer, role: tenderdash-consensus-security); final verifier: gpt-6.1-sol (agent: sol-verifier, role: final-verifier)

  • Triage: low by gpt-6.1-sol (effort low) — This is a small, well-contained HTTP JSON-RPC request-context cancellation change with focused tests and no critical consensus, cryptographic, storage, or funds-related behavior.
  • Phase 1 reviewers: glm-5.3-flash — general (completed, effort high); agent phase1-reviewer
  • Phase 1 model: glm-5.3-flash — zai quota: 5h 97% left, weekly 91% left; passed over gemini-3.8-flash-high (antigravity quota lookup failed)
  • Fresh verifier: gpt-6.1-sol — final-verifier; agent sol-verifier
  • Phase 2 reviewers: gpt-6.1-sol — general (completed, effort medium); agent phase2-reviewer, gpt-6.1-sol — tenderdash-consensus-security (completed, effort medium); agent phase2-reviewer
🤖 Prompt for all review comments with AI agents
These findings are from an automated code review (PastaClaw). How to work through them:
- Verify each finding against the code; fix only what is real, as narrowly as the finding.
- If a finding is wrong, decline it on its thread in one or two sentences; the bot answers once and stops.
- To defer one, link a tracking issue on its thread (or reply asking the bot to file one).
- Suggestions are optional: fix them forward in a later PR rather than pushing another round just for them.
- Address every current finding in one push: self-review the full diff first, and don't push piecemeal.
- Keep the PR's scope; don't grow it to satisfy hardening suggestions.
- Carried items were raised in an earlier round: read the thread first, and don't re-fix what you already fixed, declined or deferred there. Disputed and deferred items are already handled and are not listed.

In `rpc/jsonrpc/server/http_server_test.go`:
- [NITPICK] rpc/jsonrpc/server/http_server_test.go:96: Serve-level cancellation test does not check for goroutine leaks
  (carried; thread: https://github.com/dashpay/tenderdash/pull/1610#discussion_r4524741272)

@github-actions

Copy link
Copy Markdown

Bots are done — your move: post /self-reviewed.
Full checklist in the description.

@github-actions github-actions Bot added waiting-self-review Waiting for the author to post /self-reviewed and removed waiting-bots Waiting for the review bots to report on this head labels Nov 10, 2026
@QuantumExplorer
QuantumExplorer merged commit dbcc17a into dev Nov 10, 2026
29 of 31 checks passed
@QuantumExplorer
QuantumExplorer deleted the fix/rpc-stop-abandoned-batch branch November 10, 2026 23:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

waiting-self-review Waiting for the author to post /self-reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants