Skip to content

feat(payments): bound per-lane credit exposure with a concurrent-stream admission cap - #1700

Merged
alpergundogdu merged 7 commits into
mainfrom
claude/per-lane-mutex-stream-credit-bd18c1
Aug 14, 2026
Merged

alpergundogdu merged 7 commits into
mainfrom
claude/per-lane-mutex-stream-credit-bd18c1

Conversation

@alpergundogdu

Copy link
Copy Markdown
Contributor

Closes #1697.

Problem

The downstream credit window was enforced per stream, not per lane. The open-time solvency gate reserved one credit-window floor per request, and nothing aggregated concurrent same-lane draws. So N concurrent streams on one lane (pool_id, signer, provider) could put up to N × floor of unpaid egress in flight while the refundable-minimum floor M only ever guarded a single floor.

Latent today (the only shipped multi-stream client, CLI bundle pull, serializes same-lane access with provider_lock), but reachable by any non-serializing or hostile client.

Approach — an admission cap, not a divided window

A stream's credit window is fixed at admission and never re-divided among live streams. The lane mutex guards an atomic check-and-increment of the active-stream count against remaining − M — not a recomputation of everyone's share. Two admitted streams each keep their own full window; a third arrival is admitted with its own full window if budget covers one more floor, or refused — never "all three re-share a smaller slice." The serve loop is untouched.

  • Per-lane active-stream counter (Arc<AtomicU32>) behind the existing per-lane mutex.
  • Single hoisted gate, above the serve-path branch, so it covers every delivered stream — cache hit, backend-origin miss, window pull-through miss, buffered miss. Under the lane lock it reserves (n_active + 1) × floor; the surcharge applies only when another same-lane stream is already active, so n = 1 is byte-for-byte the prior behavior.
  • LaneSlot drop guard releases the slot lock-free on every exit path (success, ?-return, disconnect, panic) — a leaked slot would make the lane refuse forever.
  • ServeRejectReason::LaneAtCapacity collapses to wire NotFound (like InsufficientDeposit): no wire-format change, preserves balance/concurrency-state privacy, and the client already treats NotFound as transient/retryable ("back off / go serial").

The cap is dynamic: max_concurrent = floor((remaining − M) / floor_payment). As the pool drains the ceiling drops until even one stream is refused — the existing single-stream deposit refusal falling out of the same formula.

Out of scope (unchanged)

Tests

  • second_same_lane_stream_refused_when_budget_covers_one — surplus refused (NotFound) while the admitted stream settles.
  • finished_stream_releases_its_lane_slot — a later open succeeds after the first settles (proves Drop releases the slot).
  • single_same_lane_stream_admitted_unchanged — n = 1 identity.
  • concurrent_opens_admit_exactly_one — two simultaneous opens, exactly one admitted (deterministic under the lane lock).
  • concurrent_same_lane_misses_refuse_surplus — the same cap on the node→node miss path (origin/window pull-through).

Full decdn-node suite green (one unrelated pre-existing timing flake, a_slow_post_topup_delivery_scores_slow_not_instant).

🤖 Generated with Claude Code

…o miss paths are capped

The gate previously ran only right before the hit-path success response, so
serve_via_backend_origin and serve_via_window_pull_through returned before
reaching it and bypassed the aggregate cap entirely. Move the check-and-
increment to run once, immediately after known_lane resolves and before any
serve-path branch, so every delivered stream is counted regardless of path.

Add a regression test covering the previously-uncovered miss path: two
concurrent same-lane requests for a locally-missing hash on a one-floor
budget admit exactly one and refuse the surplus with NotFound.
Copilot AI lite review requested due to automatic review settings August 14, 2026 18:20

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 hardens the node-side payments/serving path by enforcing a per-lane (pool_id, signer, provider) concurrent-stream admission cap, preventing multiple same-lane streams from collectively exceeding the refundable-floor headroom while keeping wire behavior unchanged (refusals still collapse to NotFound).

Changes:

  • Add a per-lane active_streams counter with an RAII LaneSlot guard to ensure slots are released on all exit paths.
  • Hoist a single admission gate in the client dispatch path so it covers all serve branches (hits and misses), rejecting surplus opens as LaneAtCapacity → wire NotFound.
  • Add/refine tests covering refusal behavior, determinism under concurrent opens, and slot release semantics; add a dedicated metric for lane-capacity rejections.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
crates/node/src/handlers/client/mod.rs Adds active_streams to lane state, introduces LaneSlot, and adds focused unit tests for wire-collapse + drop behavior.
crates/node/src/handlers/client/dispatch.rs Implements the hoisted per-lane admission cap under the lane lock and wires LaneSlot lifetime into stream handling.
crates/node/src/handlers/client/wire.rs Records the new LaneAtCapacity reject reason via a dedicated metric.
crates/node/src/metrics.rs Adds serve_stream_rejected_lane_at_capacity counter and registers it for export/testing.
crates/node/src/handlers/client/voucher.rs Updates test lane fixtures to initialize the new active_streams field.
crates/node/tests/client_loopback.rs Adds helpers and multiple tests for same-lane concurrency admission/refusal and slot release behavior.
crates/node/tests/node_origin_pull.rs Adds an integration-style test ensuring the cap also applies to window pull-through miss paths.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +151 to 155
impl Drop for LaneSlot {
fn drop(&mut self) {
self.counter.fetch_sub(1, Ordering::Relaxed);
}
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch — addressed in 49e7dd0. LaneSlot::drop now uses a saturating fetch_update that floors the counter at 0, so an unpaired slot from a future refactor fails safe instead of wrapping to u32::MAX and wedging the lane. Behavior is unchanged in the always-paired case that holds today.

alpergundogdu and others added 2 commits August 14, 2026 19:31
…-stream-credit-bd18c1

# Conflicts:
#	crates/node/tests/client_loopback.rs
A LaneSlot only ever exists paired with a prior fetch_add, so the active-stream
counter is never 0 at drop today. Use a saturating fetch_update so a future
refactor that constructs an unpaired slot fails safe (counter floors at 0)
rather than underflowing to u32::MAX and wedging the lane into refusing every
admission. Raised in PR review.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@github-actions

Copy link
Copy Markdown

Coverage report

Total: 80.62% (was 80.60%, ▲ +0.02%) — 58735/72857 lines

File Coverage Lines Δ
crates/node/src/handlers/client/dispatch.rs 84.89% 382/450 ▲ +1.16%
crates/node/src/handlers/client/mod.rs 88.64% 710/801 ▲ +0.38%
crates/node/src/handlers/client/voucher.rs 88.89% 336/378 ▲ +0.06%
crates/node/src/handlers/client/wire.rs 97.64% 124/127 ▲ +0.06%
crates/node/src/metrics.rs 95.35% 1025/1075 ▲ +0.01%
History
Commit Total Δ vs base
49e7dd0 (HEAD) 80.62% ▲ +0.02%

@alpergundogdu
alpergundogdu merged commit 9729f08 into main Aug 14, 2026
74 of 78 checks passed
@alpergundogdu
alpergundogdu deleted the claude/per-lane-mutex-stream-credit-bd18c1 branch August 17, 2026 17:47
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.

feat(payments): bound per-lane credit exposure with a concurrent-stream admission cap

2 participants