Skip to content

fix(flow): close stage after its last producer and drain accepted inputs - #5567

Merged
joelteply merged 2 commits into
fable/continuum-flowfrom
fix/flow-producer-close
Oct 29, 2026
Merged

joelteply merged 2 commits into
fable/continuum-flowfrom
fix/flow-producer-close

Conversation

@joelteply

Copy link
Copy Markdown
Contributor

The stage can sleep forever when its final producers drop concurrently: Arc counts were inspected before their automatic decrement, so no producer necessarily sends the final wake. A wake could also run before the final decrement and be consumed too early.

Track producer count explicitly and publish the final decrement before notifying. Observe queue emptiness and producer closure under the queue lock so a final accepted push cannot arrive between those observations and be abandoned.

Adds a concurrent two-producer fixture covering both an idle worker and draining accepted inputs before stage destruction. Diff-check passed; execution requires CI. This is stacked on #5561 and touches only stage.rs and its tests; scheduler priority/timestamp fixes remain with that owner.

@joelteply

Copy link
Copy Markdown
Contributor Author

PASS (source) at d863b1a (Cormac), conditional on CI. Read: the whole diff, and stage.rs at this head (113 lines).

The race it fixes: Drop checked Arc::strong_count(&self.alive) == 1 BEFORE its own alive field was released. So two last producers dropping concurrently could each read 2, neither would notify, and the worker would sleep forever with the stage open. fetch_sub(1, AcqRel) returning the previous value gives exactly one dropper the 1, and that one notifies.

The close and drain ordering, checked:

  1. No accepted input is lost. The worker pops and loads producers (Acquire) under the same items lock. A producer's push takes that lock and happens before its drop (program order), and the drop's release synchronizes with the worker's acquire load. So reading 0 implies every push already released the lock before the worker took it, and the pop sees them all. The worker returns only on None AND closed.
  2. No lost wakeup. If the worker sees None with producers > 0, it awaits notified(). A push or a last drop between its unlock and that await calls notify_one, which stores a permit when no waiter is registered, so the await returns at once.
  3. Clone: fetch_add(Relaxed) from a live producer can never raise the count from 0 (the same reasoning as Arc::clone), and spawn_stage is the only constructor, at 1.

The fixture drives both last drops from two OS threads behind a Barrier, with and without queued input, and requires the drained set ({1,2} or {}) within 2 s. The old code hangs it, though only probabilistically, so a CI run that passes is weaker evidence than this ordering argument.

@joelteply

Copy link
Copy Markdown
Contributor Author

PASS (Cormac) at exact ac93cf9, re-check of the commit since my earlier PASS. ac93cf9 only reclassifies continuum-flow as a below-kernel leaf in ci-change-scope.test.cjs (BELOW_KERNEL, and the host-change expectation). Verified the premise: core/continuum-flow/Cargo.toml at this head depends only on tokio (no workspace crate), so a kernel or host change can't reach it and dropping it from those scopes is correct. CI: 36 pass, 0 fail; the PR merges cleanly.

@joelteply
joelteply merged commit fb36410 into fable/continuum-flow Oct 29, 2026
40 of 41 checks passed
@joelteply
joelteply deleted the fix/flow-producer-close branch October 29, 2026 21:53
joelteply added a commit that referenced this pull request Nov 3, 2026
…uts (#5567)

* fix(flow): publish producer closure before waking stage

* test(ci): classify flow as an independent workspace leaf
joelteply added a commit that referenced this pull request Nov 3, 2026
…M5-calibrated simulator (orphaned since 10-29) (#5729)

* continuum-flow: QoS classes, a coalescing serial queue per owner, one event-driven dispatcher per capacity (draft, untested)

SERVING-ENGINE §1c. Scheduler is a pure state machine (no locks, no runtime): serial per owner,
effective class by aging up to Work, the lowest running class asked to yield, the next aging
instant as the only timer, and queues kept short here (an owner's newer work supersedes her
waiting one and keeps her place; utility/background shed their oldest past the bound). Flow is
one dispatcher task over it; a Grant is held while working and its drop is the release.
Not yet compiled: waits on the flow plan's review before it is built and wired.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo

* continuum-flow: Stage (CBAR QueueThread), the capacity dispatcher, and a simulator calibrated to the M5; 16 cases green

- Stage: a bounded keep-newest input, one worker task woken only by a push, a class.
- Scheduler fixes the validator caught: ask only as many yields as outranking waiters; preemption
  compares against the class a ticket started at, so an aged dream is not taken back by work.
- sim: decode 30.5*n^0.865 tok/s across n streams (101 at four), prefill 304/sqrt(n) per job, page-out
  0.1 s (M5, 2026-10-28). Reproduces the live gate removal's direction and size (76 -> 57 s simulated,
  122 -> 84 s live); a directed reply on a full engine gets its first token in 1.7 s; a dream runs
  16 times an hour and slows six residents by 4%.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo

* sim: the M5 at a working-context depth (stock llama-bench fit); forecast: 82k -> 79 s/call, 32k -> 43 s, 16k -> 33 s

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo

* continuum-flow: a stage's overflow is explicit, Reliable by default (backpressure, never drops) or KeepNewest for signals only

Codex on the FLOW plan (6d18b45): committed inputs and tool results must never ride a dropping
queue. Fixture: a full reliable stage hands a result back and loses none; a signal stage drops only
its oldest wake.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo

* fix(flow): close stage after its last producer and drain accepted inputs (#5567)

* fix(flow): publish producer closure before waking stage

* test(ci): classify flow as an independent workspace leaf

* continuum-flow: a stage holds one input in hand and one pending (CBAR's double buffer), not a queue it sizes itself

`Stage::DEPTH` was a size each stage picked (ONE-BUDGET's self_sized class). A stage now holds the
input it is handling and one pending input: a `Reliable` producer that finds the slot full gets its
input back (backpressure, nothing dropped), a `KeepNewest` one replaces the pending input. The two
stage tests assert the same contracts at that shape. Doc-comment and unwrap justification fixes for
clippy and the unwrap ratchet.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo

* continuum-flow: a reliable producer awaits the free slot (send), and a stage whose worker ended hands inputs back (Cormac on #5729)

`StageInput::send` waits on the stage's `space` signal (registered before the slot is read, so a
take between the read and the wait is never missed) while a `Reliable` slot is taken: backpressure a
producer awaits instead of spinning or dropping. `push` stays non-blocking and now says why it
handed an input back: `PushError::Full` or `PushError::Stopped`. A guard in the worker marks the
stage stopped however it ends (drained and closed, or its handler panicked as the task unwinds),
under the slot's lock, and wakes every waiting producer, so nothing waits on a slot nothing will
take. Tests: the awaited result is delivered; a panicked handler's stage hands back both a push and
a send.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LoTjvf5j3Ez13g6k8mRkFo

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant