Skip to content

fix(gateway): key the manifest fill lock per hash - #2866

Merged
yigitdot merged 4 commits into
mainfrom
fix/2848-per-hash-manifest-fill
Nov 13, 2026
Merged

yigitdot merged 4 commits into
mainfrom
fix/2848-per-hash-manifest-fill

Conversation

@yigitdot

@yigitdot yigitdot commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

manifest() held one gateway-wide mutex across the whole pull_manifest, so a pull for a hash nobody holds — up to 120 s per silent gap, unbounded while bytes trickle — made every request for every other uncached bundle queue behind it. The field's own doc already described the intended scope as per hash.

The fill lock is now per hash and routing namespace, using the same Weak-map flight pattern as the cache engine's outboard fills: waiters hold the Arc, entries vanish on the last drop (cancellation-safe, no leak), and the double-checked cache read keeps one paid pull per hash and namespace. A pull routed through one namespace neither waits on nor shares another's fill. The map prunes finished fills in amortized O(1) per new entry.

A byte budget bounds the manifest bytes buffered at once to four buffers at the manifest cap (256 MiB), so the per-hash change does not trade the serialization for sixteen 64 MiB manifest buffers. A fill takes budget for its buffer's capacity as the buffer grows, so a pull still looking for a holder, or one whose bytes trickle in, holds only what it has buffered. A fill that would pass the budget fails at once instead of waiting: no pull parks on a worker slot, and two fills each holding part of the budget cannot wait on each other.

On a public gateway, requests that waited on a fill which failed upstream answer with its failure instead of each pulling again in turn. A fill counter read before the wait limits this to requests that were waiting when the fill ended, so a request that comes after the failure pulls again. Funding and allowance stops are not shared.

Follow-up for the cache engine's copy of the flight map: #2868.

Closes #2848

🤖 Generated with Claude Code

manifest() held one gateway-wide mutex across the whole pull_manifest, so
a stalled pull for one hash made every request for every other uncached
bundle queue behind it. The fill lock is now one async lock per hash, held
as a Weak in a map whose entries vanish when the last waiter drops, and a
small semaphore bounds how many fills buffer at once.

Closes #2848

Co-Authored-By: Claude Fable 5 <[email protected]>
Copilot AI balanced review requested due to automatic review settings November 11, 2026 14:05

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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@github-code-quality

github-code-quality Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

Code coverage overview

Languages: Rust, Solidity

Rust / code-coverage/cargo-llvm-cov

main f238ccf fix/2848-per-hash-ma... 9f2168a +/-
77% 77% 0%
View coverage of the 3 most impacted files.
File main f238ccf fix/2848-per-hash-ma... 9f2168a +/-
crates/node/src/dht/publish.rs 78% 77% -1%
crates/cli/src/...ands/gateway.rs 81% 81% 0%
crates/protocol/src/framing.rs 92% 93% +1%

Solidity / code-coverage/forge

main f238ccf fix/2848-per-hash-ma... 9f2168a +/-
92% 92% 0%

ℹ️ Calculations represent line coverage. Results may change as calculations are updated.


Updated November 13, 2026 08:27 UTC

…fill failures

A fill now takes its manifest fill slot once its first bytes arrive, not
before the pull starts, so pulls still looking for a holder buffer nothing
and hold no slot: four stalled discoveries no longer block every other
uncached manifest. The fill buffer grows by hand within MAX_MANIFEST_BYTES.

Pool-paid requests that waited on a pool-paid fill which failed upstream
answer with its failure instead of each pulling again in turn. The flight
map prunes finished fills in amortized O(1) per new entry.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@alpergundogdu

Copy link
Copy Markdown
Contributor

Review

The per-hash lock does what it should: a stalled manifest pull no longer blocks other hashes. The issues below are in the two parts added around it: sharing a failed fill's error, and the four fill slots. I read the code but did not build or run the tests.

Bugs

1. A shared failure can stick with no new pull (gateway.rs:2380)
The failure stored in the FillLock lasts as long as any request holds that hash's Arc. On a busy public site, requests overlap: waiters return the stored failure at once, and a new request that arrives while one of them still holds the Arc gets the same lock and the same failure. While arrivals keep overlapping, the entry never dies, and every request gets 502 with no new pull, even after the content becomes available.
Fix: share the failure only with requests that were already waiting when it was stored. For example, keep a fill counter next to the failure, read it before lock().await, and reuse the failure only when the counter matches.

2. The shared failure ignores the routing namespace (gateway.rs:2380)
The lock and its stored failure are keyed on the hash alone, but the pull is routed by namespace (site.namespace). A pool request routed through namespace 7 fails (no holder there), and a waiter for the same hash routed through namespace 9, where nodes do hold it, returns the stored 502 without pulling.
Fix: key the stored failure by (hash, namespace), or reuse it only when the waiter's namespace matches.

3. A fill waiting for a slot still holds a pull-worker slot (gateway.rs:2447)
manifest_fill_slots.acquire() runs after start_pull returns, so a fill parked behind the four fill slots keeps one of the MAX_PULLS = 16 worker slots busy doing nothing. With 4 trickling fills and 12 more distinct uncached bundles, all 16 worker slots hold manifest pulls, and every raw-blob request queues behind them. Before this PR, the global mutex was taken before start_pull, so at most one manifest pull held a worker slot. The parked pulls also stop reading their pipe, so they can reach their own silent-gap timeout and still be charged the read-ahead through the armed guard.
Fix: take the fill slot before start_pull (discovery then holds a fill slot), or give manifest pulls their own small worker-slot budget.

Cleanups

4. pool is always self.public() (gateway.rs:2379)
paying_for returns Payment::Pool only on a public gateway, and a loopback gateway never pays from the pool. So matches!(paying, Payment::Pool { .. }) equals self.public(), and the docs on manifest and FillLock describe a mix of pool-paid and capability-paid requests that no gateway reaches.
Fix: use self.public(), and say "on a public gateway, requests that waited on a fill which failed upstream share its failure."

5. A second copy of the Weak lock map (gateway.rs:2411)
ManifestFills / manifest_flight repeats the cache engine's outboard_flights (crates/cache/src/engine.rs:546), with a different pruning rule.
Fix: move one generic version (get or insert an Arc<tokio::sync::Mutex<V>>, with amortized pruning) into a crate both cli and cache depend on, and use it here. #2868 then only switches the engine to it.

6. No test for failure expiry or a namespace split (tests.rs:556)
waiters_share_a_failed_pool_fill covers only the waiters present at the failure.
Fix: add a test where a 4th request starts after the failure while one waiter still holds the Arc, and assert it pulls (caps == 2). Add one where two waiters carry different namespaces. These catch 1 and 2.

Design decision

7. Four slow upstreams can still block every other new manifest (gateway.rs:34)
A fill keeps its slot for as long as bytes trickle in, and that has no bound. Four slow or adversarial upstreams, each sending a chunk just inside the 120 s silent gap, hold all four slots, so the #2848 stall comes back, four wide instead of one.
Options:

  • Deadline: put a time limit (or a minimum byte rate) on how long a fill holds its slot.
  • Byte budget: bound the total buffered bytes instead of the number of fills, e.g. a semaphore of 4 * MAX_MANIFEST_BYTES permits, taken per chunk as it is buffered.

Open question: which option, and whether it belongs in this PR or a follow-up.

🤖 Generated with Claude Code

…red bytes

Key each fill lock by hash and routing namespace, so a pull routed
through one namespace neither waits on nor shares another's fill.

Share a failed fill's upstream error only with requests that waited on
that fill: a fill counter read before the wait tells them apart, so a
request that comes after the failure pulls again.

Bound manifest fills by the bytes their buffers hold rather than by a
count of fills. A fill takes budget as its buffer grows and fails at
once past it, so no pull parks on a worker slot and trickling upstreams
hold only the bytes they delivered.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@yigitdot

Copy link
Copy Markdown
Contributor Author

Addressed in 3079c91.

1. Sticky failure — fixed. Each fill lock carries a counter of ended fills. A request reads it before it waits and reuses the stored failure only if a fill ended after it arrived; otherwise it pulls itself. A request that comes after the failure always pulls again. Test: a_failure_is_not_shared_with_later_requests holds the lock's Arc across two requests and asserts two pulls (fails without the counter).

2. Namespace — fixed. The flight is keyed by (hash, namespace), so a pull routed through another namespace neither waits on nor shares the fill. This also stops one namespace's stalled pull from delaying another's. Test: another_namespace_does_not_wait_on_a_fill (fails with a hash-only key).

3 + 7. Fill slots — replaced with a byte budget. Taking the slot before start_pull would bring back the stall: four discoveries for hashes nobody holds would block every other manifest. Instead, a semaphore counts buffered bytes (4 * MAX_MANIFEST_BYTES, 256 MiB, the same worst case). A fill takes permits for its buffer's capacity as it grows, with try_acquire_many, and fails at once when the budget is spent.

  • No fill ever waits, so none parks on a worker slot (3).
  • A trickling upstream holds only the bytes it delivered, so blocking other manifests takes about 256 MiB of verified bytes pending, not four slow connections (7).
  • Waiting per chunk is ruled out: two fills that each hold part of the budget would wait on each other.

Tests: trickling_fills_block_no_other_manifest (five trickling fills, a sixth still answers 200), a_spent_buffer_budget_fails_a_fill_fast, stalled_discoveries_buffer_nothing.

4. pool — fixed. Sharing now keys on self.public(), and the docs say "on a public gateway".

5. Shared helper — kept in #2868. Moving it here would still leave the cache engine's copy, because that crate is out of this PR's scope, so this PR would add a module without removing a copy. The crate choice is also a design call: decdn-protocol is the only leaf both crates reach that already has tokio, and it holds the wire format. #2868 now covers moving both sides in one change, with a helper generic over the value (the gateway's Flight holds a counter and a failure beside its lock).

6. Tests — added, as listed above.

@github-actions

Copy link
Copy Markdown

Gas snapshot

No gas changes vs base.

@yigitdot
yigitdot merged commit b6d673c into main Nov 13, 2026
38 checks passed
@yigitdot
yigitdot deleted the fix/2848-per-hash-manifest-fill branch November 13, 2026 13:04
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.

gateway: one gateway-wide manifest_fill lock serializes every uncached site behind the slowest manifest pull

3 participants