Eager fiber recovery on wake (onStart)#1259
Merged
threepointone merged 4 commits intomainfrom Apr 4, 2026
Merged
Conversation
Make fiber recovery run eagerly on the first request after a Durable Object wakes instead of relying solely on the persisted alarm. Updates include: - packages/agents/src/index.ts: call await this._checkRunFibers() during onStart so interrupted fibers are recovered immediately on first request. - packages/ai-chat/src/tests/durable-chat-recovery.test.ts: add a test to ensure fibers are not double-recovered when both onStart and the alarm path call _checkRunFibers(). - experimental/forever.md and experimental/forever-fibers/README.md: clarify documentation to state that onStart performs primary recovery on first wake and the persisted alarm is a fallback (with a re-entrancy guard to prevent double recovery), and adjust wording describing local/production behavior. These changes ensure faster, deterministic recovery after hibernation and guard against duplicate recovery runs.
🦋 Changeset detectedLatest commit: b0af15d The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Made-with: Cursor
The test sent request 1 with only 300ms response delay and 20ms gap before the clear, causing flaky timeouts under CI load. Increase response delay to 500ms, widen gaps between steps, and raise the waitUntil timeout from 5s to 8s. Made-with: Cursor
Made-with: Cursor
agents
@cloudflare/ai-chat
@cloudflare/codemode
hono-agents
@cloudflare/shell
@cloudflare/think
@cloudflare/voice
@cloudflare/worker-bundler
commit: |
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.
Summary
Fiber recovery (
_checkRunFibers) previously only ran via the alarm path —alarm()→_onAlarmHousekeeping()→_checkRunFibers(). If the DO woke from a fetch/WebSocket (not an alarm), recovery was deferred until the next alarm fired. This created a gap where clients could see stale partial responses with no continuation scheduled.This PR adds a single line to
onStart()so fiber recovery runs eagerly on the first request after wake:The alarm path remains as a fallback. A re-entrancy guard (
_runFiberRecoveryInProgress) prevents double recovery if bothonStartandalarmrun close together.Why this is safe
_checkRunFibers()is idempotent — fiber rows are deleted after recovery, so the second call is a no-oponStart()runs exactly once per DO wake (PartyServer's#ensureInitializedguarantees this)runFiber, not justAIChatAgentChanges
packages/agents/src/index.tsawait this._checkRunFibers()inonStartwrapperpackages/ai-chat/src/tests/durable-chat-recovery.test.ts_checkRunFibersproduces exactly one recoveryexperimental/forever.mdonStartpathexperimental/forever-fibers/README.mdTest plan
triggerFiberRecovery()twice, verifies recovery fires exactly once and message is persisted onceMade with Cursor