Skip to content

incentive: watcher bring-up race — bootstrap returns before .watch() filters install, dropping events in the gap #762

Closed

Description

@thiras

Summary

PaymentChannelService::bootstrap returns as soon as the watcher task is spawned, but that task then installs four sequential .watch() (eth_newFilter) streams in run_watcher_once (ChannelOpened, ChannelToppedUp, ChannelSettled, ChannelCloseInitiated). .watch() only delivers logs from blocks after the filter exists, so a ChannelOpened (or other event) emitted in the window between bootstrap returning and the filters being installed is never observed — the channel is never registered and its vouchers are rejected with WrongChannel.

In production this is rare: a real client opens a channel, waits for confirmations, then connects — human-scale latency that dwarfs the sub-ms filter-install window. But it is a genuine bring-up race, not a guarantee.

Where it surfaced

PR #752 (anvil e2e settlement harness) has to bridge this race with a fixed sleep(2s) before the first openChannel, because the test mines events milliseconds after bootstrap. A fixed sleep is the classic flaky-test smell; the only reason it's safe today is the large margin (the sleep plus the intervening mint/approve roundtrips). Review thread: #752 (comment)

Proposed fix

Make watcher startup capture a start block and backfill, rather than relying on .watch() alone:

  1. On bootstrap, record the current head S.
  2. Install the .watch() filters; note the block F at which they take over.
  3. get_logs for ChannelOpened (provider == self) over [S, F], registering each (dedup on the overlap block so an event seen by both paths isn't double-applied).

This eliminates the race deterministically and would let #752 replace its readiness sleep with a deterministic wait.

Relationship to #751

This is the same root cause and same fix as the first checkbox in #751 ("Opened-during-downtime backfill") — both stem from .watch() being forward-only, and a start-block get_logs backfill closes both the downtime gap and this bring-up race. Filing separately per request; these should be implemented together (or this folded into #751) to avoid duplicate work. Whoever picks up #751's backfill item should close this too.

Scope

Deferred post-PoC robustness; does not block #752 (the test's bounded sleep + clean 60s poll-timeout failure mode is acceptable interim). Production-side change to payment_settlement.rs.

Activity

  1. added
    prio:medium
    rust
    on Jun 1, 2026
  2. added
    kind:bug
    kind:test
    on Jun 1, 2026
  3. github-actions commented on Jun 1, 2026

    @github-actions

    🎯 Triage report

    PaymentChannelService::bootstrap has a TOCTOU race: it returns before the four eth_newFilter watch streams are installed in run_watcher_once, creating a window where on-chain events (ChannelOpened, etc.) are silently dropped. In production the window is sub-millisecond vs. human-scale latency, so this is benign in the field today but causes a real flaky-test smell in the anvil e2e harness (#752's sleep(2s) workaround). The author explicitly scopes this as a post-PoC robustness fix and notes it shares root cause and fix with #751's "Opened-during-downtime backfill" checkbox — the two should be implemented together.

    📊 Assessment

    Dimension Value Reasoning
    Type Bug Genuine correctness gap in payment_settlement.rs: events in the bring-up window are permanently lost
    Labels bug, prod-blocking, testing added (existing: technical, medium, rust) bug — correctness gap; prod-blocking — post-PoC robustness item per issue scope; testing — directly causes flaky-test workaround in #752
    Coding agent Suitable Algorithm is fully specified (record head S, install filters noting block F, backfill get_logs([S,F]) with dedup on overlap), file is named (payment_settlement.rs), and success criteria are clear: remove the sleep(2s) in #752 and have tests pass deterministically

    🔗 Similar issues

    💡 Notes and suggestions

    Root cause: eth_newFilter (alloy .watch()) is strictly forward-looking from the block at filter creation time. The four sequential filter installs in run_watcher_once create an observable gap after bootstrap returns.

    Proposed fix (from issue):

    1. Before spawning the watcher task, snapshot current head block S via eth_blockNumber.
    2. In run_watcher_once, install all four .watch() filters as today; record the block F at which the last filter takes effect (can use a second eth_blockNumber call immediately after all four watch() calls return).
    3. Immediately after filter installation, call get_logs for all four event types (or at minimum ChannelOpened / ChannelToppedUp / ChannelSettled / ChannelCloseInitiated, filtered to provider == self) over [S, F].
    4. Process the backfill events through the same handler path as live events. Dedup on block F (events in that exact block may be delivered by both paths).

    Test impact: Implementing this allows #752 to replace its sleep(2s) readiness guard with a deterministic wait (e.g., poll until the watcher signals "filters installed and backfill complete"), eliminating the flakiness surface entirely.

    Coordination note: Whoever picks up #751's backfill checkbox should close this issue simultaneously, as the implementation is shared. Consider folding this into #751 to avoid split PRs against the same code path.

    File: crates/incentive/src/payment_settlement.rs (functions bootstrap and run_watcher_once).

    Generated by Agentic Triage for issue #762 · sonnet46 503.3K · ◷

    Add this agentic workflows to your repo

    To install this agentic workflow, run

    gh aw add githubnext/agentics/workflows/issue-triage.md@94ec5db57374c5b04a4b8eef8b4413f9af44d63f
    
  4. self-assigned this
    on Jun 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind:bugkind:testprio:mediumrust

Type

No type

Fields

Priority

None yet

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions