Skip to content

fix: clear stale in-memory purge queue entries#285

Open
YunchuWang wants to merge 1 commit into
mainfrom
yunchuwang-purge-queue-cleanup-fix
Open

fix: clear stale in-memory purge queue entries#285
YunchuWang wants to merge 1 commit into
mainfrom
yunchuwang-purge-queue-cleanup-fix

Conversation

@YunchuWang

Copy link
Copy Markdown
Member

Summary

Fixes a stale work-queue entry bug in the in-memory testing backend purge path.

InMemoryOrchestrationBackend.purge() previously removed the orchestration instance state, state waiters, and instance timers, but it did not remove the instance ID from orchestrationQueue or orchestrationQueueSet. Because enqueueOrchestration() uses orchestrationQueueSet as a deduplication guard, a purged instance could leave behind a stale set entry that caused a newly created orchestration with the same instance ID to silently skip enqueueing.

Why this matters

The trigger scenario is:

  1. An orchestration completes.
  2. A later event or completion queues that same completed instance ID.
  3. The completed instance is purged.
  4. A new orchestration is created with the same ID.
  5. The stale orchestrationQueueSet entry makes enqueueOrchestration() a no-op, so the new instance never gets processed and appears to hang.

This affects the in-memory testing infrastructure and can make tests silently stall when they purge and recreate an orchestration with the same ID.

Fix

purge() now removes the instance ID from both queue structures before deleting waiters/timers:

  • Deletes the ID from orchestrationQueueSet.
  • Removes the ID from orchestrationQueue if present.

This mirrors the queue cleanup behavior already present in reset() while preserving the rest of the purge semantics.

Tests

Added a regression test that:

  1. Completes an orchestration.
  2. Stops the worker and raises a late event to leave the completed ID in the queue/set.
  3. Purges the completed instance and verifies both queue structures are cleaned.
  4. Recreates the same instance ID and verifies it is processed to completion.

Validation run:

  • npm run build:core
  • npx eslint packages\durabletask-js\src\testing\in-memory-backend.ts packages\durabletask-js\test\in-memory-backend.spec.ts
  • npx jest --runInBand --runTestsByPath .\test\in-memory-backend.spec.ts --testNamePattern "should clear stale queued work"
  • npx jest --runInBand

Fixes #249

InMemory purge removed instance state but left queued IDs in orchestrationQueue and orchestrationQueueSet. Clear both structures during purge so re-created instances with the same ID can enqueue and process.

Fixes #249

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings July 7, 2026 21:43

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes a correctness issue in the in-memory testing backend where purge() could leave behind stale orchestration work-queue bookkeeping, preventing a re-created orchestration with the same instance ID from ever being enqueued/processed.

Changes:

  • Updated InMemoryOrchestrationBackend.purge() to remove the instance ID from orchestrationQueueSet and orchestrationQueue.
  • Added a regression test that reproduces the “late enqueue → purge → recreate same ID hangs” scenario and verifies the fix.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
packages/durabletask-js/src/testing/in-memory-backend.ts Ensures purge() also cleans up the in-memory orchestration queue/set to avoid stale dedupe entries.
packages/durabletask-js/test/in-memory-backend.spec.ts Adds a regression test covering purge queue cleanup and successful re-creation + completion of the same instance ID.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants