Skip to content

fix(incentive): graceful failure on concurrent buyer-store open (#942) - #946

Merged
alpergundogdu merged 2 commits into
mainfrom
claude/concurrent-fetch-graceful-942
Jun 19, 2026
Merged

alpergundogdu merged 2 commits into
mainfrom
claude/concurrent-fetch-graceful-942

Conversation

@alpergundogdu

Copy link
Copy Markdown
Contributor

Closes #942 as superseded — the truly-concurrent shared-channel case is out of scope (it needs the single-owner puller daemon from the #944 design comment). This PR is the minimal meantime fix: make the collision fail gracefully instead of corrupting a shared voucher nonce.

Problem

Two decdn fetch / bundle pull processes on the same --data-dir can't safely share one payment channel — its nonce/bytes/amount ledger is serial and channel-wide. redb already prevents the dangerous case via a process-exclusive write lock on the buyer-channel DB, but RedbBuyerChannelStore::open reported it as a raw backend string:

channel store backend: open buyer channel db: Database already open. ...

Fix

  • New dedicated StoreError::AlreadyOpen { path }.
  • open() maps redb's DatabaseError::DatabaseAlreadyOpen to it, so the second opener fails fast with an actionable message:

    another decdn process is using the channel store at <path> — run one fetch/pull at a time per --data-dir, or give each invocation a separate --data-dir

  • The node's buyer-reconcile path matches StoreError exhaustively (no catch-all, by design); the new variant joins the "store unavailable → skip, don't clobber" arm (it can't actually arise from that read path).

This is the safe outcome the issue's "Cross-process concurrency" section calls for: SAFE (no nonce corruption), blunt (whole-process granularity, fail-not-queue). Internal multiplexing within one process (e.g. bundle pull --jobs) is unaffected and already nonce-safe via the per-provider lock from #391.

Test

second_concurrent_open_is_already_open_not_raw_backend: a second open on the same data dir returns AlreadyOpen with the user-facing message (not a redb dump) and recovers once the first handle drops.

Verification

cargo fmt --check, cargo clippy --workspace --all-targets -D warnings, cargo doc, cargo deny, cargo nextest run --workspace (1872 passed) all green.

🤖 Generated with Claude Code

A second `decdn fetch`/`bundle pull` against the same `--data-dir` trips redb's
process-exclusive write lock. `RedbBuyerChannelStore::open` mapped that to a raw
`StoreError::Backend("...DatabaseAlreadyOpen...")` string. Add a dedicated
`StoreError::AlreadyOpen { path }` and map redb's `DatabaseError::DatabaseAlreadyOpen`
to it, so the second process fails fast with a clear, actionable message
("another decdn process is using the channel store at <path> — run one
fetch/pull at a time per --data-dir, or give each invocation a separate
--data-dir") instead of a backend dump.

This is the deliberate, safe outcome: two processes must never share one
channel's serial voucher nonce. Truly-concurrent cross-process shared-channel
fetch stays out of scope — the supported answer is one process that multiplexes
internally (the puller-daemon direction in #944). #942 closes as superseded by
that design; this just makes the meantime failure graceful.

- store: new `StoreError::AlreadyOpen` variant.
- node buyer reconcile: its exhaustive (no catch-all) StoreError match gains the
  variant in the "store unavailable, skip — don't clobber" arm (it can't arise
  from that read path, but classifies cleanly).
- test: second concurrent open returns `AlreadyOpen` with the user-facing
  message, and recovers after the first handle drops.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Copilot AI review requested due to automatic review settings June 18, 2026 13:55

Copilot AI 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.

Pull request overview

This PR improves client-side (buyer) channel-store ergonomics and safety by converting redb’s “database already open” condition into a dedicated, user-facing StoreError::AlreadyOpen { path }, preventing confusing backend dumps and ensuring concurrent use of the same --data-dir fails fast and clearly.

Changes:

  • Add StoreError::AlreadyOpen { path } with an actionable error message for cross-process store-lock collisions.
  • Map redb::DatabaseError::DatabaseAlreadyOpen to StoreError::AlreadyOpen in RedbBuyerChannelStore::open.
  • Extend node-side buyer-channel reconcile matching to include the new AlreadyOpen variant and add a regression test covering the new error behavior.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
crates/node/src/buyer_channel.rs Updates the explicit StoreError match to include AlreadyOpen in the “store unavailable → skip” arm.
crates/incentive/src/store.rs Introduces StoreError::AlreadyOpen { path } with a dedicated, user-facing error message.
crates/incentive/src/buyer_channel_redb.rs Maps redb’s DatabaseAlreadyOpen to StoreError::AlreadyOpen and adds a unit test verifying the dedicated error and recovery after dropping the first handle.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@github-actions

github-actions Bot commented Jun 18, 2026 •

Copy link
Copy Markdown

Coverage report

Total: 84.22% (was 84.21%, ▲ +0.01%) — 38434/45635 lines

File Coverage Lines Δ
crates/incentive/src/buyer_channel_redb.rs 87.25% 431/494 ▲ +0.43%
crates/incentive/src/store.rs 90.91% 160/176 ● 0.00%
crates/node/src/buyer_channel.rs 35.48% 231/651 ● 0.00%
History
Commit Total Δ vs base
728189f 84.22% ● 0.00%
0e57834 (HEAD) 84.22% ▲ +0.01%

@gemini-code-assist gemini-code-assist 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.

Code Review

This pull request introduces a dedicated StoreError::AlreadyOpen error variant to handle cases where a second concurrent process attempts to open the channel store database, replacing raw backend error strings with a user-friendly message. It also updates the reconciliation logic to skip this error and adds a corresponding unit test. The review feedback highlights a compilation issue where the closure mapping the error needs the move keyword to take ownership of path, and suggests enhancing the unit test to verify the path field in the asserted error.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Strengthen the concurrent-open assertion to also verify the StoreError::AlreadyOpen
path field matches the buyer-channels db file, per PR #946 review.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@alpergundogdu
alpergundogdu merged commit 6309811 into main Jun 19, 2026
20 checks passed
@alpergundogdu
alpergundogdu deleted the claude/concurrent-fetch-graceful-942 branch June 19, 2026 16:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants