Skip to content

perf(node): last-out serve-miss observer joins the pull, freeing a finished owner - #1664

Merged
alpergundogdu merged 2 commits into
mainfrom
claude/infallible-turing-1f5330
Aug 12, 2026
Merged

alpergundogdu merged 2 commits into
mainfrom
claude/infallible-turing-1f5330

Conversation

@alpergundogdu

Copy link
Copy Markdown
Contributor

What

Follow-up from B3 multi-observer coalescing (#1660). A self-flagged, bounded lifecycle inefficiency — not a bug.

Both serve-miss orchestrations (serve_via_window_pull_through, serve_via_backend_origin) had the pull owner park in spawn_blocking(pull_thread.join()).await right after its own serve leg returned. When the owner's client finished first while an Attach observer was still streaming, the owner's accept task stayed blocked in that join until the pull ended — bounded (the pull ends on fill-completion or the last observer's lease-drop cancel), but it held the finished owner's QUIC/accept-task and a blocking-pool thread longer than necessary.

Change

Park the pull-thread JoinHandle on the shared FillSession and hand it back only to whichever observer leaves last, so a non-last-out owner returns immediately.

crates/cache/src/fill_session.rs

  • FillSession gains pull_thread: StdMutex<Option<JoinHandle<()>>> with set_pull_handle (owner parks it after spawn) and private take_pull_handle.
  • ObserverLease::release() -> Option<JoinHandle<()>> shares one teardown() with Drop (idempotent via an armed flag). Teardown runs the identical last-out decrement + map-removal + cancel under the registry map lock (B3.2 resurrection-race fix), and returns the parked handle only on the last-out drop. It takes the handle under the lock but never joins there; the node's async teardown joins off-task via spawn_blocking. detach() is removed.

crates/node/src/handlers/client/window.rs

  • Both orchestrations park the handle on the owner branch, and every teardown (owner and attach) joins iff release() yields a handle. So an attached observer that leaves last joins the owner's thread; a finished owner returns at once.

Invariants preserved

Tests / gate

  • TDD: two failing-first unit tests in fill_session.rs drove the mechanism — last_out_release_takes_the_pull_handle (non-last-out owner → None, returns at once, no cancel; last observer → Some, joins, cancels) and last_out_release_without_a_parked_handle_returns_none (spawn-failure parity).
  • Green: cargo fmt --all --check, clippy (cache + node, no warnings), 1338 node+cache tests pass, and cargo test --no-run -p decdn-e2e --features anvil-e2e compiles.

🤖 Generated with Claude Code

…nished owner

Follow-up from B3 multi-observer coalescing (#1660). Both serve-miss
orchestrations (`serve_via_window_pull_through`, `serve_via_backend_origin`)
had the pull OWNER park in `spawn_blocking(pull_thread.join()).await` right
after its OWN serve leg returned. When the owner's client finished first while
an `Attach` observer was still streaming, the owner's accept task stayed blocked
in that join until the pull ended — bounded (the pull ends on fill-completion or
the last observer's lease-drop cancel), but it held the finished owner's
QUIC/accept-task and a blocking-pool thread longer than necessary.

Park the pull-thread `JoinHandle` on the shared `FillSession` and hand it back
only to whichever observer leaves LAST, so a non-last-out owner returns at once:

- `FillSession` gains `pull_thread: StdMutex<Option<JoinHandle<()>>>` with
  `set_pull_handle` (owner parks it after spawn) and private `take_pull_handle`.
- `ObserverLease::release() -> Option<JoinHandle<()>>` shares one `teardown()`
  with `Drop` (idempotent via an `armed` flag). Teardown runs the identical
  last-out decrement + map-removal + cancel UNDER the registry map lock (the
  B3.2 resurrection-race fix), and returns the parked handle only on the last-out
  drop. It TAKES the handle under the lock but never JOINS there; the node's
  async teardown joins off-task via `spawn_blocking`. `detach()` is removed.
- Both orchestrations park the handle on the owner branch and, in every teardown
  (owner and attach), join iff `release()` yields a handle.

Invariants preserved: #1610 (last observer cancels the pull, no orphan ingest),
#852 `SettleOnDrop` buyer-watermark (runs inside the pull leg regardless of
joiner), and the pull thread is always joined by the last-out observer, never
detached.

Mechanism covered by new unit tests in `fill_session.rs`
(`last_out_release_takes_the_pull_handle`,
`last_out_release_without_a_parked_handle_returns_none`).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Copilot AI lite review requested due to automatic review settings August 12, 2026 15:01
@github-actions

github-actions Bot commented Aug 12, 2026 •

Copy link
Copy Markdown

Coverage report

Total: 80.55% (was 80.57%, ▼ 0.02%) — 62914/78101 lines

File Coverage Lines Δ
crates/cache/src/fill_session.rs 95.07% 694/730 ▲ +0.50%
crates/node/src/handlers/client/window.rs 81.82% 369/451 ▼ 0.94%
History
Commit Total Δ vs base
95b27a6 80.56% ▼ 0.01%
8eee2a3 (HEAD) 80.55% ▼ 0.02%

@coderabbitai

coderabbitai Bot commented Aug 12, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

@alpergundogdu, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 25 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Free

Run ID: 9c3de5cb-daf0-4072-a7cd-2282774abcd0

📥 Commits

Reviewing files that changed from the base of the PR and between 476db54 and 8eee2a3.

📒 Files selected for processing (2)
  • crates/cache/src/fill_session.rs
  • crates/node/src/handlers/client/window.rs

Note

🎁 Summarized by CodeRabbit Free

Your organization is on the Free plan. CodeRabbit will generate a high-level summary and a walkthrough for each pull request. For a comprehensive line-by-line review, please upgrade your subscription to CodeRabbit Pro by visiting https://app.coderabbit.ai/login.

Comment @coderabbitai help to get the list of available commands.

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 refactors serve-miss coalescing teardown so the upstream pull thread is joined by the last observer to leave, allowing a non-last-out owner (whose client finishes first) to return immediately instead of blocking on join().

Changes:

  • Added pull-thread handle parking to decdn_cache::FillSession, with last-out transfer via ObserverLease::release().
  • Updated both serve-miss orchestrations in the node window handler to park the handle on the session and join only when release() returns a handle.
  • Added unit tests covering handle handoff and spawn-failure parity.

Reviewed changes

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

File Description
crates/cache/src/fill_session.rs Stores the pull thread JoinHandle on FillSession, adds last-out release() semantics, and adds tests for handle handoff.
crates/node/src/handlers/client/window.rs Parks pull thread handles on the shared session and joins them only on last-out lease release for both owner and attach paths.
Suppressed comments (1)

crates/cache/src/fill_session.rs:489

  • ObserverLease::Drop currently discards the JoinHandle returned by teardown(). On a last-out implicit drop (e.g., task cancellation/shutdown before explicit release() runs), that drops the JoinHandle and detaches the pull thread, contradicting the stated invariant that the pull thread is always joined by the last observer. Consider joining off-thread here when teardown() returns a handle so an implicit drop can’t silently detach.
impl Drop for ObserverLease {
    fn drop(&mut self) {
        let _ = self.teardown();
    }

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

Comment thread crates/cache/src/fill_session.rs Resolved
…andle

Address Copilot review on #1664: overwriting an already-parked JoinHandle would
drop the first handle and detach its thread, orphaning a pull that keeps
paying/draining — violating the "always joined" guarantee. It cannot happen
today (FillRegistry::claim returns exactly one Owner per session, so the owner
parks once), but harden the setter against a future regression: keep the first
handle (the one the last-out observer joins) and debug_assert the double-park
loudly in tests/dev.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@alpergundogdu
alpergundogdu merged commit 208ae2d into main Aug 12, 2026
27 checks passed
@alpergundogdu
alpergundogdu deleted the claude/infallible-turing-1f5330 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.

2 participants