fix: cancel pending timers on continue-as-new in InMemoryOrchestrationBackend - #328
Open
YunchuWang wants to merge 2 commits into
Open
fix: cancel pending timers on continue-as-new in InMemoryOrchestrationBackend#328YunchuWang wants to merge 2 commits into
YunchuWang wants to merge 2 commits into
Conversation
…nBackend When an orchestration calls continueAsNew(), the InMemoryOrchestrationBackend resets instance state (history, status, input) but does not cancel pending setTimeout handles from the previous iteration. These stale timers continue to fire and deliver TimerFired events into the new iteration. Because the new iteration resets the sequence counter, stale timer IDs can collide with task IDs in the new iteration, potentially completing the wrong task or causing unexpected-event warnings. This commit: - Adds per-instance timer tracking (instanceTimers map) - Cancels all pending timers for an instance on continue-as-new - Fixes carryover event ordering so OrchestratorStarted and ExecutionStarted come before carryover events, matching real sidecar behavior - Adds tests for both the timer cancellation and event ordering fixes Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a correctness issue in the in-memory testing backend where continueAsNew() did not cancel timers scheduled in the prior iteration, allowing stale TimerFired events to leak into the new execution. It also adjusts carryover external event ordering to align with real sidecar behavior.
Changes:
- Track timer handles per orchestration instance and cancel them when processing
continue-as-new. - Reorder continue-as-new “carryover events” so
OrchestratorStarted/ExecutionStartedprecede carried-over events. - Add regression tests covering timer cancellation and carryover event ordering scenarios.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| packages/durabletask-js/src/testing/in-memory-backend.ts | Adds per-instance timer tracking/cancellation and fixes carryover event ordering for continue-as-new. |
| packages/durabletask-js/test/in-memory-backend.spec.ts | Adds tests intended to validate timer cancellation and carryover ordering after continue-as-new. |
Comment on lines
+388
to
+399
| const orchestrator: TOrchestrator = async function* (ctx: OrchestrationContext, input: number): any { | ||
| if (input === 1) { | ||
| // First iteration: create a long timer, then immediately continue-as-new. | ||
| // The timer should be cancelled and NOT fire into the second iteration. | ||
| ctx.createTimer(60); // 60-second timer — should be cancelled | ||
| ctx.continueAsNew(2, false); | ||
| } else { | ||
| // Second iteration: create a short timer and wait for it. | ||
| // If the stale timer from iteration 1 leaks, it could complete | ||
| // the wrong task or cause unexpected events. | ||
| yield ctx.createTimer(0.05); // 50ms timer | ||
| iteration2TimerFired = true; |
Comment on lines
+442
to
+445
| expect(state).toBeDefined(); | ||
| expect(state?.runtimeStatus).toEqual(OrchestrationStatus.COMPLETED); | ||
| expect(state?.serializedOutput).toEqual(JSON.stringify("carried-over")); | ||
| }); |
kaibocai
previously approved these changes
Jul 27, 2026
Most of this branch had already landed on main independently: the instanceTimers registry with addInstanceTimer/removeInstanceTimer/cancelInstanceTimers, and the carryover event ordering fix. Resolved the conflict by keeping main's versions and reducing this branch to the one thing still missing: the cancelInstanceTimers call in the continue-as-new path. Dropped the carryover-ordering test as a duplicate of main's 'should deliver carryover events after ExecutionStarted during continue-as-new'. Rewrote the timer test, which passed with and without the fix because its stale timer was 60s and could never fire during the test. It now uses a 50ms stale timer against a 1.5s timer in iteration 2, so the ID collision is observable: 56ms without the fix, ~1.5s with it.
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 #207.
InMemoryOrchestrationBackendcontinue-as-new did not cancel pending timers, leaking stale timer events into the new iteration. This cancels pending timers on continue-as-new.Opens the existing fix branch
copilot-finds/bug/fix-continue-as-new-timer-leakas a PR (automated triage of active [copilot-finds] issues that had a ready fix branch but no open PR).