Skip to content

decdn fetch: concurrent invocations to the same new provider can double-open a channel #942

Closed

Description

@alpergundogdu

Summary

decdn fetch (#940) opens-or-reuses a buyer PaymentChannel per provider via get_by_provider → openChannel → store.record. That sequence is not atomic across the on-chain open, and the one-shot CLI has no cross-process in-flight guard (the node's BuyerChannelService has one — a per-provider opens_in_flight set). So two decdn fetch processes started simultaneously against the same provider for which no channel exists yet can both miss the reuse check and each submit openChannel. The second store.record (provider-keyed) overwrites the first, leaving one channel's deposit escrowed on-chain but untracked locally — reclaim-able only manually.

Preconditions (why it's rare)

  • Same provider, no prior live channel for it (first fetch), AND
  • Two fetch invocations overlapping within the open window.

Steady state (a channel already exists → both reuse) and distinct providers are unaffected.

Options

  • Cross-process file lock around the per-provider open (e.g. a lockfile in the data dir keyed by provider). Closes the local race but not the on-chain gap if the lock is bypassed.
  • Reconcile-after-open: re-read the store under a lock after openChannel; if a row for the provider already exists, prefer it and surface the just-opened channel for manual reclaim. Mirrors the node's TOCTOU re-check.
  • Document as a known limitation for v1 (the deposit is recoverable manually; the window is narrow).

Notes

Activity

  1. added
    kind:bug
    area:cli
    prio:low
    on Jun 17, 2026
  2. github-actions commented on Jun 17, 2026

    @github-actions

    🎯 Triage report

    A TOCTOU race in decdn fetch's channel open sequence: two simultaneous CLI processes targeting the same new provider can both pass the reuse-check gap and each submit openChannel on-chain. The second store.record overwrites the first, leaving one channel's deposit escrowed on-chain but untracked locally, recoverable only by manual reclaim. The window is narrow (first-ever fetch to a provider, concurrent invocations), and the common path is unaffected.

    📊 Assessment

    Dimension Value Reasoning
    Type Bug Fund-loss (locked deposit) from a race condition, even if the window is rare
    Labels bug, technical, cli, low (unchanged) Already correctly applied; low priority matches the narrow precondition
    Coding agent Suitable Three concrete options are described; "reconcile-after-open" is a clear, self-contained implementation path mirroring the existing node-side TOCTOU re-check

    🔗 Similar issues

    💡 Notes and suggestions

    Recommended fix: reconcile-after-open (mirrors node-side pattern)

    The node's BuyerChannelService already handles this with a per-provider opens_in_flight set that acts as an in-process guard. The CLI needs an analogous mechanism that works cross-process. The reconcile approach is the safest because it tolerates the on-chain race without relying purely on local locking:

    1. After openChannel returns, re-read store.get_by_provider under a lock (file lock on a per-provider lockfile in $data_dir/locks/<provider_pubkey>.lock).
    2. If a record now exists (written by a racing concurrent invocation that won), prefer the existing record and surface the just-opened channel for reclaim (log its channel ID with WARN-level guidance to manually reclaim the deposit).
    3. If no record exists, write the new channel as normal.

    Sub-tasks:

    • Add a per-provider file lock (e.g., fd-lock crate or flock(2)) in the CLI's open_or_reuse path
    • Implement the post-open TOCTOU re-check (store re-read under lock)
    • On double-open detection, log the orphaned channel ID with reclaim instructions
    • Consider adding a decdn channel list --orphaned or decdn channel reclaim sub-command (ties into the deferred reclaim follow-up from Client-side buyer channel management: auto-open, track, reuse PaymentChannels for decdn fetch #940)
    • Add an integration test: two concurrent fetch processes → assert only one openChannel TX submitted (or both submitted but only one tracked, with the other logged)

    Cross-process file lock option alone (without the reconcile re-check) doesn't close the on-chain gap if the lock is bypassed or if the process crashes between open and store.record, so prefer the combined approach.

    Documentation fallback (option 3 from the issue) is reasonable for v1 given the narrow precondition, but the reconcile approach is low-effort enough to prefer over documenting a known fund-loss path.

    Generated by Agentic Triage for issue #942 · ◷

    Add this agentic workflows to your repo

    To install this agentic workflow, run

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

Metadata

Metadata

Assignees

Labels

area:clikind:bugprio:low

Type

Fields

Priority

None yet

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions