Skip to content

fix(rpc): stop resubscribing subscription IDs as queries after reconnect - #1623

Merged
lklimek merged 4 commits into
devfrom
fix/rpc-ws-stale-subscription-alias
Nov 20, 2026
Merged

lklimek merged 4 commits into
devfrom
fix/rpc-ws-stale-subscription-alias

Conversation

@Claudius-Maginificent

@Claudius-Maginificent Claudius-Maginificent commented Nov 17, 2026 •

Copy link
Copy Markdown
Collaborator

TL;DR: Restore event subscriptions after a dropped connection without repeated errors or stalled event delivery.

User story

As a developer using rpc/client/http to subscribe to node events, directly or through the light client proxy, I want my subscriptions restored cleanly when the connection to the node drops and comes back, so events keep flowing without errors.

Scenario

Base flow

An application subscribes to node events (for example new blocks) through the HTTP RPC client. The node restarts or the network blips, and the client reconnects.

Actual behavior

Besides each query, the client stored the subscription ID from incoming events as an extra subscription key. Tenderdash sends events with an empty subscription ID, so after the first event the client tracked an extra key "". On every reconnect it sent subscribe "", which the node rejects (failed to parse query: offset 0: EOF). Each rejection makes the client wait a second and resubscribe everything again, so the loop never ends: events are delayed and WS error is logged every second. Upstreams that do send subscription IDs leaked one entry per reconnect and later resubscribed those IDs as queries.

Expected behavior

After a reconnect, the client resubscribes exactly the queries it was asked to subscribe to, and events keep arriving with no errors.

Detailed discussion

What was done

  • rpc/client/http/ws.go: subscriptions are tracked by query only. Removed the id and unused query fields, the ID alias written by eventListener (also its only map write under RLock), the alias delete in Unsubscribe, and the alias skip in redoSubscriptionsAfter. Event delivery remains keyed by result.Query, and subscribers still receive the SubscriptionID data field unchanged. The undocumented ID-based cancellation path is intentionally removed; the supported cancellation argument is the original query.
  • rpc/client/http/ws_reconnect_test.go: regression test against the node's real subscribe handler (real EventBus and rpccore routes behind rpcserver.NewWebsocketManager, with the node's OnDisconnect cleanup). Across three sessions with two forced reconnects it requires that each session still delivers events and that no WS error is logged.

Testing

  • The new test fails 3/3 on dev (logged failed to parse query: offset 0: EOF, or no resubscription on the new session) and passes repeatedly with the fix under -race, including a separate check against an upstream that assigns a new ID per session.
  • ./rpc/client/http/, ./rpc/client/, ./light/proxy/ and ./light/rpc/ pass with -race; gofmt, go vet and golangci-lint --new-from-rev=origin/dev are clean.
  • After merging current dev (f2097db82), go test -mod=readonly -race -p 1 -count=1 -timeout=5m ./rpc/client/http ./rpc/client ./light/proxy ./light/rpc passes; gofmt and the CI-pinned golangci-lint v2.15.0 report no issues in ./rpc/client/http/....
  • The original failed test check failed only on TestReactorValidatorSetChanges after its 120-second deadline. The same failure occurs on dev after #1625; the next dev run also fails TestStateLock_POLSafety2. The refreshed head (31434e054) reproduces only the same 120-second TestReactorValidatorSetChanges timeout. These are consensus failures outside this RPC diff. Historical flake tracking: Flaky tests: TestReactorValidatorSetChanges and TestRouter_EvictPeers time out intermittently on v1.6-dev #1405. The latest green dev workflow skipped its test steps and does not establish a fix.
  • Final independent review covers the merged base and test cleanup; no blocking findings.
  • The documented Unsubscribe API accepts a query. ID aliases and the undocumented ID-based cancellation path are intentionally removed: no known subscriber uses that path. Verified Platform v5.1-dev (9ce1de7d) and v5.0-dev (a8a2e7e5): rs-dapi does not read subscription_id or send Tenderdash unsubscribe, and legacy DAPI cancels by query. The Go interface, OpenAPI, and ADR specify query-based cancellation. Callers should use the original query; unknown third-party consumers cannot be exhaustively ruled out. The event data field and JSON-RPC request IDs are unchanged.

Breaking changes

No changes to the documented API. Undocumented cancellation by an upstream event ID in the HTTP client is intentionally removed; callers must pass the original query.

Prior work

🤖 Co-authored by Claudius the Magnificent AI Agent

🤖 Generated with Claude Code

PR Hygiene · 31434e0

  • Bots — thepastaclaw skipped after the window
  • Self-review — not asked of a bot author
  • Build green
  • Approvals
    • files with no dedicated owner (rpc/client/http/ws.go, rpc/client/http/ws_reconnect_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.

lklimek and others added 2 commits November 17, 2026 10:26
wsEvents stored each event's subscription ID as an extra key in its
subscription map, and resubscribed every key on reconnect except one
equal to the subscription's latest ID. Tenderdash sends events without a
subscription ID, so the empty key was resubscribed as a query: the node
rejected it, and each rejection made the event loop resubscribe again a
second later, stalling event delivery. Upstreams sending fresh IDs per
session leaked one key per reconnect, resubscribed the same way.

Nothing looks subscriptions up by ID, so drop the ID keys; the alias
write was also the event loop's only map write under a read lock.

<sub>🤖 Co-authored by [Claudius the Magnificent](https://github.com/lklimek/claudius) AI Agent</sub>

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@coderabbitai

coderabbitai Bot commented Nov 17, 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

@thepastaclaw

thepastaclaw commented Nov 17, 2026 •

Copy link
Copy Markdown

✅ Final review complete — no blockers (commit 31434e0)

lklimek and others added 2 commits November 19, 2026 07:42
Remove the unused subscription query field and avoid FailNow in the
reconnect test's cleanup goroutine.

<sub>🤖 Co-authored by [Claudius the Magnificent](https://github.com/lklimek/claudius) AI Agent</sub>

Co-Authored-By: Codex GPT-6 <[email protected]>
@Claudius-Maginificent
Claudius-Maginificent marked this pull request as ready for review November 19, 2026 07:49
@github-actions

github-actions Bot commented Nov 19, 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 the waiting-bots Waiting for the review bots to report on this head label Nov 19, 2026

@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

Updated after discussion: 1 finding(s) of this review were settled in the inline threads; no blocking finding remains.

  • ✅ Withdrawn: 🔴 Unsubscribe by event ID leaves the query registered locally

The query-only reconnect tracking fixes the reported empty and upstream subscription-ID replay loop, and the added reconnect test covers the real WebSocket path. However, removing the local subscription-ID alias introduces a cancellation regression: when an upstream accepts an event ID for unsubscription, the remote subscription is removed but the canonical query remains locally and is resubscribed after reconnect. CI code checks are successful; the pending review and PR Hygiene checks do not indicate a code failure.

🔴 1 0 blocking

Review provenance

Source: reviewer 1: muse-spark-1.3-contributor (agent: phase1-reviewer, role: general); reviewer 2: gpt-6.2-sol (agent: phase2-reviewer, role: general); reviewer 3: gpt-6.2-sol (agent: phase2-reviewer, role: general); final verifier: gpt-6.2-sol (agent: sol-verifier, role: final-verifier)

  • Triage: normal by gpt-6.2-sol (effort low) — This changes reconnect and subscription-tracking behavior in the HTTP RPC client and adds a substantial regression test, so it is more than a trivial contained fix but does not alter a critical surface.
  • Phase 1 reviewers: muse-spark-1.3-contributor — general (completed, effort xhigh); agent phase1-reviewer
  • Phase 1 model: muse-spark-1.3-contributor — not quota-gated; passed over gemini-3.8-flash-high (antigravity quota lookup failed), glm-5.3-flash (not used above high effort; tier asks max)
  • Second Phase-2 pass: on this first full review every Phase-2 reviewer also ran a second, independent time, and the verifier weighed both sets
  • Fresh verifier: gpt-6.2-sol — final-verifier; agent sol-verifier
  • Phase 2 reviewers: gpt-6.2-sol — general (completed, effort high); agent phase2-reviewer, gpt-6.2-sol — general (completed, effort high); 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 don't block the merge, but most are worth fixing in this PR; judge each on its merits.
- 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/client/http/ws.go`:
- [BLOCKING → WITHDRAWN in discussion; do not fix] rpc/client/http/ws.go:103-110: Unsubscribe by event ID leaves the query registered locally
  The previous implementation recorded the nonempty ResultEvent.SubscriptionID as an alias and removed both the alias and canonical query when Unsubscribe was called with that ID. This change deletes only w.subscriptions[query]. Therefore, if a compatible upstream assigns an ID and successfully accepts ws.Unsubscribe(ctx, subscriptionID), the server-side subscription is canceled while the client's canonical query remains in w.subscriptions. The next reconnect iterates that stale query in redoSubscriptionsAfter and silently recreates the subscription, undoing the caller's cancellation. The SubscriptionClient documentation names the argument query, but the existing implementation supported ID-based cancellation and the PR explicitly targets interoperability with upstreams that provide IDs. Retain a separate ID-to-query index used only to resolve unsubscription cleanup, or otherwise resolve the supplied ID to its canonical query before deleting it, and add a regression test for unsubscribe followed by reconnect.

Comment thread rpc/client/http/ws.go Resolved
@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 19, 2026

@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 after discussion — commit 31434e0

Standing review was CHANGES_REQUESTED; this re-review is APPROVE: no blocking findings remain.

Approved: the discussion withdrew or deferred every finding the full review of 31434e05 (run 11720) raised, and the approval rests on that review.

No longer blocking:

  • Unsubscribe by event ID leaves the query registered locally

Same commit as the standing review; the inline threads above carry the per-finding outcome. This follow-up exists only to correct the verdict.

Review provenance

Source: conversation lane gpt-6.2-sol (agent: conversation); no reviewer or verifier lanes ran for this follow-up

  • Verdict moved because the inline discussion withdrew or deferred findings of the standing review of this commit; the code was not re-reviewed

@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 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 19, 2026
@github-actions

Copy link
Copy Markdown

@thepastaclaw review

No review for 31434e05 yet, so PR Hygiene is asking once. If nothing arrives, the requirement is dropped for this commit and the pull request is labelled bot-review-skipped.

@lklimek
lklimek enabled auto-merge (squash) November 19, 2026 20:23
@github-actions

Copy link
Copy Markdown

Policy satisfied — this can merge.
Full checklist in the description.

@github-actions github-actions Bot added bot-review-skipped A required review bot did not report; it was skipped by the window or by a person. and removed waiting-bots Waiting for the review bots to report on this head labels Nov 20, 2026
@lklimek
lklimek merged commit 107f35b into dev Nov 20, 2026
39 of 40 checks passed
@lklimek
lklimek deleted the fix/rpc-ws-stale-subscription-alias branch November 20, 2026 00:39
@github-actions github-actions Bot added this to the v2.0 milestone Nov 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot-review-skipped A required review bot did not report; it was skipped by the window or by a person.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants