fix(core): prevent stale Fiber generations from committing - #54
Open
morluto wants to merge 1 commit into
Open
Conversation
morluto
marked this pull request as ready for review
August 13, 2026 19:57
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #34.
Problem
A Fiber currently uses its dependency epoch both as the desired-state snapshot and as the identity of in-flight work. If an async plugin load observes
A → B → A, the original load sees the same epoch when it resumes and is allowed to commit. The Fiber can therefore reportACTIVEwith a new config or provider even though that generation was never applied.A related failure occurs after plugin execution rejects:
restart()successfully runs the plugin again, but the previous execution error remains attached to the Fiber, leaving it inFAILED.stateDiagram-v2 [*] --> LoadingA: generation 1 / config A LoadingA --> DesiredB: update while apply awaits DesiredB --> DesiredA2: desired epoch returns to A LoadingA --> Unloading: generation 1 settles stale Unloading --> LoadingA2: start latest generation LoadingA2 --> Active: only generation 3 commitsOn current
main, the issue #34 sequence produces:main[1][1, 2]{ value: 2 }{ value: 2 }ACTIVEACTIVEThe current state is internally inconsistent: config
2is published without ever running the plugin with config2.Change
Track a monotonic generation alongside the dependency epoch. Every real desired-state transition receives a fresh generation, even if its epoch string repeats. Reload execution, async-generator collection, error publication, and completion now require both the epoch and generation to remain current.
Starting a loadable generation also clears the previous execution error. A successful explicit restart can therefore recover a failed Fiber, while a failure from superseded work cannot poison the latest generation.
This keeps the existing serialized reload/unload pump and coalesces notifications whose epoch does not change.
Regression coverage
The tests exercise:
A → B → Arace from Bug: epoch ABA race #34;FAILEDthrough a successful restart.The ABA and restart tests were also run against
8cc9e33; they respectively produced[1]with published config2, and remainedFAILEDwith the original error.Performance sanity check
A synthetic loop creating, awaiting, and disposing 5,000 synchronous plugins took 932.8 ms on
8cc9e33and 941.6 ms with this change (single run, Node 22.22.1). This is a smoke check rather than a performance claim; the added counter does not introduce per-effect records or a new scheduler.Validation
corepack yarn vitest packages/core/tests/fiber.spec.ts --run— 11 tests passedcorepack yarn test— 19 files and 166 tests passedgit diff --check origin/main...HEAD— passed