Skip to content

fix(cache): run origin request sends on the node's main runtime and restore HTTP keep-alive - #2308

Merged
thiras merged 3 commits into
mainfrom
fix/1675-origin-keepalive-io-runtime
Oct 5, 2026
Merged

thiras merged 3 commits into
mainfrom
fix/1675-origin-keepalive-io-runtime

Conversation

@thiras

@thiras thiras commented Oct 5, 2026

Copy link
Copy Markdown
Contributor

What

A serve-miss pull leg runs on its own current-thread runtime, and that runtime drops when the leg returns. hyper-util's pooled client spawns three tasks onto whichever runtime polls the request send: each connection's dispatch task, the task that returns a connection to the pool, and the pool's idle reaper. So a connection opened by one pull leg died with that leg's runtime. A concurrent leg that reused the connection then lost its in-flight request with "dispatch task is gone" (#1673).

  • HTTP: HttpOrigin worked around this with pool_max_idle_per_host(0), which costs a fresh TCP and TLS handshake on every request.
  • S3: S3Origin kept its shared hyper pool and had no workaround, so the race is still live on main for S3 origins.

This PR applies the same approach as #2187's dial_runtime:

  • IoRuntime (crates/cache/src/origin/io_runtime.rs) captures the runtime an origin is built on. In the node that is the main runtime, via build_cache.
  • Every HTTP and S3 request send runs there.
  • The response body stays on the caller's runtime. hyper feeds it through a channel, so any runtime can read it.
  • HttpOrigin drops the keep-alive workaround.
  • A request task that panics, or that the shutting-down main runtime cancels, fails as a permanent origin fault. A cancelled one cannot succeed on retry.

Closes #1675

Reviewer notes

Verification

  • cargo fmt -- --check
  • cargo clippy --workspace --all-targets --all-features -- -D warnings
  • cargo nextest run
  • RUSTDOCFLAGS="-D warnings" cargo doc --workspace --no-deps --document-private-items
  • pre-commit run --all-files
  • New tests in crates/cache/tests/origin_runtime_keepalive.rs, 12 in total. They run against a raw keep-alive server that counts TCP connections and honours Range.
    • 8 keep-alive tests: each sending operation (fetch, range, outboard, size) opens the connection on a runtime that is then dropped. A second leg must reuse that connection. Run for HTTP and for S3.
    • 4 in-flight tests: a leg running fetch or range survives the drop of the runtime that opened its connection. Run for HTTP and for S3.
    • Without the fix: with IoRuntime::run bypassed, every test fails (a second connection, an error, or a hang).
    • Per site: bypassing any one S3 send site fails exactly its matching test.
    • Stability: 10/10 loops pass.
  • CHANGELOG.md entry added (Fixed)
  • Design change: none (no ADR covers origin connection pooling)
  • PR title: fix(cache): …

🤖 Generated with Claude Code

thiras and others added 2 commits October 5, 2026 09:00
…estore HTTP keep-alive (#1675)

A serve-miss pull leg runs on its own current-thread runtime, which drops
when the leg returns. hyper spawns a pooled connection's dispatch task, its
pool-return task and the pool's idle reaper onto whichever runtime polls the
request send. A connection opened by one pull leg therefore died with that
leg's runtime, and a concurrent serve that reused it lost its in-flight
request ("dispatch task is gone", #1673).

HttpOrigin worked around this with pool_max_idle_per_host(0), which paid a
fresh TCP and TLS handshake per request. S3Origin kept its shared hyper pool
with no workaround, so the race was still live for S3 origins.

Both origins now capture the runtime they are built on (IoRuntime, the
node's main runtime via build_cache) and run every request send there,
following the dial_runtime pattern from #2187. The response body stays on
the caller's runtime: hyper feeds it through a channel. HttpOrigin drops
the keep-alive workaround.

New tests in crates/cache/tests/origin_runtime_keepalive.rs cover both
backends against a raw keep-alive server. Two pull legs on throwaway
runtimes share one connection, and a leg mid-body survives the runtime
drop of the leg that opened its connection. All four fail with the spawn
bypassed: a second connection, an error, or a hang.

Closes #1675

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…d path (#1675)

Review follow-ups for the origin IoRuntime change:

- A task the shut-down main runtime cancels is a permanent fault. A
  shut-down runtime cancels every later spawn, so retrying only waited out
  the budget and raised a false "retry budget exhausted" error.
- A panicking request task keeps its JoinError (task id and panic message)
  as the error source and logs at error level.
- HttpOrigin's headers timeout runs on the caller's runtime, so it also
  fires when the owning runtime stops polling the send.
- S3 task faults name the operation (GetObject / HeadObject).
- IoRuntime uses tokio-util's AbortOnDropHandle (workspace tokio-util
  gains the `rt` feature).
- The keep-alive tests run every sending operation (fetch, range, outboard,
  size) as the connection opener, for HTTP and S3, against a server that
  honours Range; in-flight tests cover fetch and range. Each leg has a
  deadline, and a leg that fails early surfaces its own error. Bypassing any
  one S3 send site fails its matching test.
- Docs: pull-leg runtimes drop when the leg returns; idle connections are
  reaped normally, it is the owning runtime that lives with the node;
  S3Origin::from_parts is not test-only; constructor `# Errors` sections.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Copilot AI balanced review requested due to automatic review settings October 5, 2026 06:35
@thiras thiras self-assigned this Oct 5, 2026

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 review overview

🟡 Changes recommended

The new S3 runtime contract needs public API documentation, and its breaking signature change belongs in the breaking-changes section.

Review effort: Balanced
Findings: 2 Low severity

Open (2)
What changed in this PR

Routes HTTP and S3 request sends through the node’s main runtime, preventing pooled connections from dying with temporary pull-leg runtimes.

Changes:

  • Adds IoRuntime for runtime-owned request tasks.
  • Restores HTTP keep-alive and wraps all S3 sends.
  • Adds cross-runtime keep-alive and in-flight regression tests.
File Description
Cargo.toml Enables Tokio task utilities.
CHANGELOG.md Documents origin runtime changes.
crates/​cache/​src/​origin/​io_runtime.rs Implements runtime capture and task execution.
crates/​cache/​src/​origin/​http.rs Routes requests through the captured runtime.
crates/​cache/​src/​origin/​s3.rs Routes SDK requests through the captured runtime.
crates/​cache/​src/​origin/​mod.rs Registers the runtime module.
crates/​cache/​tests/​origin_runtime_keepalive.rs Adds keep-alive and in-flight regressions.
crates/​cache/​tests/​s3_origin.rs Adapts tests to fallible construction.
crates/​node/​src/​runtime/​mod.rs Documents main-runtime origin construction.

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

Comment thread CHANGELOG.md Resolved
Comment thread crates/cache/src/origin/s3.rs Resolved
…d list it as breaking (#1675)

S3Origin::new documents that its pooled connections live on the runtime it
runs on and that it fails outside a tokio runtime. The constructor changes
move from Fixed to Changed (BREAKING) in the changelog.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-code-quality

Copy link
Copy Markdown

Code Coverage Overview

Languages: Rust

Rust / code-coverage/cargo-llvm-cov

The overall line coverage in commit 6757fd6 in the fix/1675-origin-keep... branch remains at 87%, unchanged from commit b37bbc4 in the main branch.

Show a line coverage summary of the most impacted files.
File main b37bbc4 fix/1675-origin-keep... 6757fd6 +/-
crates/node/src/dht/publish.rs 92% 92% 0%
crates/client/src/lib.rs 92% 92% 0%
crates/cache/src/origin/s3.rs 90% 90% 0%
crates/node/src...s/client/mod.rs 95% 95% 0%
crates/client/src/streamer.rs 91% 91% 0%
crates/cache/src/engine.rs 94% 94% 0%
crates/node/src...gin/pull_leg.rs 91% 91% 0%
crates/cache/sr.../origin/http.rs 91% 96% +5%
crates/cache/sr...n/io_runtime.rs 0% 95% +95%

@thiras
thiras merged commit 0b4f285 into main Oct 5, 2026
31 of 32 checks passed
@thiras
thiras deleted the fix/1675-origin-keepalive-io-runtime branch October 5, 2026 06:53
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.

Pull legs run on ephemeral per-serve runtimes, stranding shared connection drivers (HTTP + QUIC)

2 participants