Skip to content

fix(dash-spv): hedge in a task of its own - #1531

Merged
ZocoLini merged 2 commits into
devfrom
fix/hedge-own-task
Nov 26, 2026
Merged

ZocoLini merged 2 commits into
devfrom
fix/hedge-own-task

Conversation

@xdustinface

@xdustinface xdustinface commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

The flow controller sent the hedges itself, the second copies of a filter batch or block that holds up sync. Each one waits for its write, up to 5 s when the peer stopped reading or another write holds its writer, and meanwhile no peer's rate or cap was updated and no slow peer was flagged.

Hedging now runs in a task of its own on the same 250 ms tick, so a slow write no longer pauses the flow controller.

Split out of #1474.

PR Hygiene · 674bd0d

  • Bots — coderabbitai ✓
  • Self-review — post /self-reviewed
  • Build green
  • Approvals — you own every area touched; none needed

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.

Summary by CodeRabbit

  • Bug Fixes
    • Improved network responsiveness when peer writes are delayed. Flow-control updates can continue while block-request hedging waits, reducing the chance that a slow peer stalls updates.

A hedge waits for its write, up to 5 s when the peer stopped reading. The flow controller awaited it, so one slow write paused the rate and cap updates of every peer. Hedging now runs in its own task.
@coderabbitai

coderabbitai Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The manager starts a separate hedger task. The flow controller no longer performs block or filter hedging, and a test checks that flow-cap updates continue while a hedge write is blocked.

Changes: Hedging and flow control

Layer / File(s) Summary
Run hedging outside the flow controller
dash-spv/src/network/manager.rs
The manager starts a hedger task that checks block and filter hedges on each tick. The flow controller releases its peer-set read lock and no longer performs hedging. A paused-clock test verifies that flow-cap updates continue while a hedge write is blocked.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix

Sequence Diagram(s)

sequenceDiagram
  participant PeerNetworkManager
  participant FlowController
  participant spawn_hedger
  participant Broker
  PeerNetworkManager->>FlowController: start flow controller
  PeerNetworkManager->>spawn_hedger: start hedger task
  loop each FLOW_TICK
    spawn_hedger->>Broker: check block and filter hedges
    Broker-->>spawn_hedger: return hedge result
  end
  FlowController->>FlowController: update peer flow caps
Loading

Possibly related issues

Possibly related PRs

Suggested reviewers: zocolini

Merge Risk: Minimal · up to 674bd

Hedging moves to its own task so slow hedge writes no longer pause flow-cap updates. No concrete merge-blocking risk was identified.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title "fix(dash-spv): hedge in a task of its own" matches the main change. The PR moves block and filter hedging out of the flow controller into a separate spawned hedger task on the same tick, so…
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.
Full details: Title check

Explanation

The title "fix(dash-spv): hedge in a task of its own" matches the main change. The PR moves block and filter hedging out of the flow controller into a separate spawned hedger task on the same tick, so flow-cap and rate updates no longer wait on hedge writes. The title is specific and names the core change. It does not mention the secondary change of skipping hedging when no peer is connected, which is an acceptable omission.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • 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

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 6b002e9 and 674bd0d.

Files selected for processing: 1

  • dash-spv/src/network/manager.rs

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

@manki-review

manki-review Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

✅ Review complete in 5m 8s

Planner (3s)
    feat · 89 lines · 2 agents
    judge effort: medium

Review (behavior · full): 3 findings
    ✅ Claude Opus 5.5 · high: 0 findings (1m 33s)
    ✅ GPT 6.2 Sol · high: 3 findings (2m 16s)
    ✅ Related checks, claim checks, pattern checks and fix checks: 8 of 8 done (53s)
    ✅ Second reads: 1 of 1 done (49s)

Judge (1m 24s)
    graded 3 findings: 3 kept · 0 dropped
    origin checks: 1 checked · 1 older than this PR
    verdict reader: read the diff (21s)

Posted: 2 findings
    2 suggestions
    not posted: 1 older than this PR

Review metadata

Config:

  • Models: reviewer=claude-opus-5-5, judge=claude-opus-5-5
  • Triage: trivial=false, subtle=true, paired=true (planner)
  • Team: Claude Opus 5.5 · high, GPT 6.2 Sol · high
  • Memory: enabled (central)

Account limits:

  • Claude subscription: weekly 76% (resets 2026-12-01 05:00 UTC)
  • ChatGPT subscription: weekly 2% (resets 2026-12-02 01:49 UTC)

Judge decisions:

  • Kept: "The hedger scans the request registry even with no connected peers" (suggestion): "note. The claim is accurate. The flow controller used to skip hedging through if g.is_empty() { continue; } at manager.rs:640, and spawn_hedger at manager.rs:743-751 has no such skip. So with zero peers it still locks requests and walks it in lowest_on_wire (broker.rs:483) every 250 ms. The extra work happens only while no peer is connected. It is one linear pass over the registry, which the sync managers fill with bounded batches. enqueue is called by the crate's own sync layer, not by outside input. Nobody would notice this cost. An early return when fewer than two peers are connected is a cheap tidy-up."
  • Not listed, on code this PR did not change: "A blocked hedge still delays hedges to other peers" (suggestion): "warning cell, but no realistic trigger. The new task awaits broker.hedge (manager.rs:745) before broker.hedge_filters (manager.rs:751), and both await peer.send, which can wait up to WRITE_TIMEOUT (peer.rs:345). So a stalled block-hedge write delays the filter hedge by up to 5 s. The trigger needs several conditions at once:
  • the endgame, where broker.len() == 0
  • a block that has had no answer for HEDGE_AFTER
  • a filter batch that also needs a hedge
  • the chosen peer's writer stalled

A stalled write ends by disconnecting the peer, so the delay is bounded and every result stays correct. The trigger is left empty."

  • Kept: "The regression test forces the flow controller's caps by hand" (suggestion): "note. The test calls peer.set_cap(1) (manager.rs:2413). The limiter never produces that value, because it clamps caps to MIN_CAP_BYTES, so the test breaks the rule against setting state by hand. The test can still fail on the revert. If the hedge moves back into the flow controller, the held write at hold_writes blocks the tick, and the caps stay at 1 past line 2416. The 1 is only a marker that a tick ran. The finding is about how the test is built, so revertGap is empty."

Timing:

  • Parse: 3.9s
  • Review agents: 219.9s
  • Judge: 84.1s
  • Total: 307.9s

@github-actions

github-actions Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Waiting for bot review — coderabbitai 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 25, 2026

@manki-review manki-review Bot 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.

✅ Approved. 2 suggestions, none of which block.

What this does: Hedged requests, the duplicate requests sent to a second peer for work that holds up sync, used to run inside the bandwidth controller's loop. One slow write could stall bandwidth measurement for every peer.


What to test

The review model wrote these steps. Check what a step runs before you follow it.

Setup: Run a filter sync against several peers, ideally with one peer that is slow to accept writes, with peer_speed and dash_spv::network debug logging on.

  1. Run cargo test -p dash-spv --lib a_hedge_waiting_for_its_write_holds_up_no_flow_tick. Expect it to pass.
  2. Sync filters on testnet with several peers. Expect peer_speed cap lines to keep appearing about every 500 ms while hedged filter batch lines also appear.
  3. Near the end of block download with an empty queue, watch the log. Expect endgame: hedged block lines as before.
  4. Stop and restart the network manager. Expect no hedges sent after stop and none to the peers of the next start.
  5. Edge case: start the client with no peers configured or reachable. Expect no hedge log lines and no errors from the hedger task.
About this review
  • Reviewed 4ecf2c4, round 1
  • 89 changed lines read in 5m 8s
  • Found by claude-opus-5-5 and gpt-6.2-sol, judged by claude-opus-5-5
  • Tokens: 2.7M in (2.0M from cache) · 40k out · about $3.62 at API prices
  • Runner: self-hosted · started after 6s · peak memory 3.3 GiB of 30.0 GiB
  • How Manki reviews

Comment thread dash-spv/src/network/manager.rs Resolved
Comment thread dash-spv/src/network/manager.rs Resolved
@codecov

codecov Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.33333% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 87.61%. Comparing base (6b002e9) to head (674bd0d).

Files with missing lines Patch % Lines
dash-spv/src/network/manager.rs 98.33% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##              dev    #1531      +/-   ##
==========================================
- Coverage   87.62%   87.61%   -0.01%     
==========================================
  Files         270      270              
  Lines       71695    71745      +50     
==========================================
+ Hits        62821    62859      +38     
- Misses       8874     8886      +12     
Flag Coverage Δ
core 83.38% <ø> (-0.02%) ⬇️
rpc 83.38% <ø> (-0.02%) ⬇️
spv 95.07% <98.33%> (-0.02%) ⬇️
wallet 83.38% <ø> (-0.02%) ⬇️
Files with missing lines Coverage Δ
dash-spv/src/network/manager.rs 96.22% <98.33%> (+0.05%) ⬆️

... and 3 files with indirect coverage changes

The flow controller skipped its whole tick, hedges included, while no peer was connected. The hedger task lost that check when it was split out, so it scanned the request registry under its lock every 250 ms with nobody to send a copy to.
@github-actions

github-actions Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Waiting for bot review — coderabbitai 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.

@manki-review

manki-review Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

✅ Review complete in 7m 53s

Planner (5s)
    feat · 92 lines · 2 agents
    judge effort: medium

Review (behavior · full): 4 findings
    ✅ Claude Opus 5.5 · high: 0 findings (1m 25s)
    ✅ GPT 6.2 Sol · high: 4 findings (3m 58s)
    ✅ Related checks, claim checks, pattern checks and fix checks: 7 of 7 done (1m 10s)

Judge (2m 20s)
    graded 4 findings: 2 kept · 2 dropped
    dropped, as the finders filed them: 1 blocker · 1 warning
    refuter: 1 checked · 1 confirmed
    origin checks: 1 checked · 1 older than this PR
    verdict reader: read the diff (22s)

Posted: 1 finding
    1 suggestion
    not posted: 1 older than this PR

Review metadata

Config:

  • Models: reviewer=claude-opus-5-5, judge=claude-opus-5-5
  • Triage: trivial=false, subtle=true, paired=true (planner)
  • Team: Claude Opus 5.5 · high, GPT 6.2 Sol · high
  • Memory: enabled (central)

Account limits:

  • Claude subscription: weekly 81% (resets 2026-12-01 05:00 UTC)
  • ChatGPT subscription: weekly 2% (resets 2026-12-02 01:49 UTC)

Judge decisions:

  • Dropped: "A batched block request can exhaust memory in the broker" (ignore): "ignore. The cloning at dash-spv/src/network/broker.rs:362 and :87 is in a file this PR does not change. The PR only moves the existing hedge and hedge_filters calls into a new task. It neither causes nor exposes this behaviour, so it fails test 1."
  • Kept: "The flow regression test can pass before the hedge starts waiting" (suggestion): "note. This is the same finding as P2, and the test code at dash-spv/src/network/manager.rs:2416 has not changed, so its cell stays. peer.set_cap(1) sets internal state by hand, which breaks the AGENTS.md rule on testing through the public API. The new point that the hedge may not yet be waiting on the writer is plausible. Still, advance_silent(3) likely lets the hedge start before the check, and the code shown does not prove that the test cannot fail on a revert."
  • Not listed, on code this PR did not change: "A blocked block hedge still delays filter hedges to other peers" (suggestion): "warning. The new task awaits broker.hedge at dash-spv/src/network/manager.rs:748 before it reaches hedge_filters at :754. peer.send can wait up to WRITE_TIMEOUT, which is 5 s (dash-spv/src/network/peer.rs:90, :345), so a stalled block hedge delays a due filter hedge to a different peer. Every result is still right, so this is a cost. Whether block and filter hedging can both be due in the same tick is not settled from the code shown.
    Refutation unsettled: The serialization is real: hedge awaits peer.send, and that send can wait up to WRITE_TIMEOUT (5 s) before hedge_filters runs. But the cost is a one-time delay of at most 5 s, not one that grows with input. After the first attempt o.hedges is non-empty, so later ticks skip the block hedge and the filter hedge goes out. The only effect is a short, bounded delay. Rule checked: Awaits inside one async task run one after another, so a pending peer.send in hedge delays the hedge_filters call that comes after it in the same loop iteration.
  • Premise "During sync, a tick can find the broker queue empty (broker.len() == 0) while a filter batch is on the wire and eligible for hedge_filters." unsettled. hedge_filters does not look at the queue length, and a filter batch is on the wire while the queue is empty whenever every queued request has been routed, for example the last batches of filter sync. Block GetData requests do exist (sync/blocks/manager.rs:283), but I did not trace whether a block and a filter batch are both due for a hedge in the same tick.
  • Premise "This trigger happens in realistic use: when a block hedge write stalls on one peer while a filter batch waits for its hedge" confirmed. Code part only: hedge picks its target peer by cap room alone, without the silence check that hedge_filters applies. It then awaits send, which can wait up to the 5 s write timeout. A peer with a stuck writer can therefore be chosen and hold up the call to hedge_filters that follows. Whether a real peer's writer stalls is network behaviour.
    Pair refutation confirmed: The hedger waits for the block send before it even checks filter eligibility, and the block send can wait on its peer's writer lock. The cited test holds a filter hedge's writer, not a block hedge's writer while a filter hedge to another peer is due, so it does not establish the full trigger in normal sync. Rule checked: Sequential awaits in one async task make the second operation wait for the first, even when they would write to different peers.
  • Premise "During sync, a tick can find the broker queue empty (broker.len() == 0) while a filter batch is on the wire and eligible for hedge_filters." confirmed. The broker removes a filter request from the counted queue before sending it and then tracks it separately as on wire, so a tick can see an empty queue while an unanswered batch and a recently active alternate peer satisfy the filter hedge checks.
  • Premise "This trigger happens in realistic use: when a block hedge write stalls on one peer while a filter batch waits for its hedge" confirmed. On the code side, production sends share the selected block peer's writer lock, block hedge selection does not check that lock, and the hedger waits for the send before checking filters even when the filter candidate would be a different peer."
  • Dropped: "A silent block hedge prevents retrying with another connected peer" (ignore): "ignore. The hedge eligibility check at dash-spv/src/network/broker.rs:393 and not_found at :207 are in a file this PR does not change. The PR only moves the call site into a new task, so it fails test 1. Project memory also records that not_found is unchanged from dev, and that Dash Core answers a block request it cannot serve with no reply rather than notfound."

Timing:

  • Parse: 10.2s
  • Review agents: 322.3s
  • Judge: 140.1s
  • Total: 472.6s

Last reviewed 674bd0d · head 674bd0d · up to date

  • Pause automatic reviews
  • Review again
  • Deeper review

@github-actions

Copy link
Copy Markdown
Contributor

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 26, 2026

@manki-review manki-review Bot 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.

✅ Approved.

Caution

Claude subscription has used 81% of its weekly limit, past the 80% mark. It resets 2026-12-01 05:00 UTC.

Once a limit is used up, reviews on that account stop until it resets.

What this does: Sending a backup copy of a stuck request to another peer could wait on a slow socket write and freeze the bandwidth controller meanwhile. That backup sending now runs in its own background task, so per-peer bandwidth caps keep updating while it waits.


What to test

The review model wrote these steps. Check what a step runs before you follow it.

Setup: Run the unit tests of dash-spv/src/network/manager.rs, or sync against testnet with filters enabled and peer_speed debug logging on.

  1. Run cargo test -p dash-spv --lib a_hedge_waiting_for_its_write_holds_up_no_flow_tick. Expect it to pass without hanging.
  2. Sync with filters enabled and watch the peer_speed logs. Expect cap= lines to keep appearing every half second while hedged filter batch lines show up.
  3. Let sync reach the tip of block download. Expect endgame: hedged block debug lines only when the request queue is empty.
  4. Disconnect all peers. Expect no hedging log lines while the peer set is empty.
  5. Call stop() on the network manager. Expect the hedger task to exit with the other background tasks and no further hedge logs.
About this review
  • Reviewed 674bd0d, round 2
  • 92 changed lines read in 7m 53s
  • Found by claude-opus-5-5 and gpt-6.2-sol, judged by claude-opus-5-5
  • Tokens: 2.7M in (2.1M from cache) · 38k out · about $3.32 at API prices
  • Runner: self-hosted · started after 4m 30s · peak memory 1.9 GiB of 30.0 GiB
  • How Manki reviews

@xdustinface
xdustinface requested a review from ZocoLini November 26, 2026 02:01
@ZocoLini
ZocoLini merged commit b753967 into dev Nov 26, 2026
36 of 37 checks passed
@ZocoLini
ZocoLini deleted the fix/hedge-own-task branch November 26, 2026 10:06
manki-review Bot added a commit to manki-review/review-memory that referenced this pull request Nov 26, 2026
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.

2 participants