Skip to content

Fix LocalOrchestrationService WaitForOrchestrationAsync deadlock - #1440

Open
Risheek Rajolu (golddove) wants to merge 1 commit into
Azure:mainfrom
golddove:fix/emulator-wait-deadlock
Open

Risheek Rajolu (golddove) wants to merge 1 commit into
Azure:mainfrom
golddove:fix/emulator-wait-deadlock

Conversation

@golddove

Copy link
Copy Markdown
Member

Fixes a deadlock in LocalOrchestrationService when a caller awaits WaitForOrchestrationAsync without a SynchronizationContext and immediately calls back into LocalOrchestrationService on the continuation.

Problem

  1. WaitForOrchestrationAsync creates its waiter TaskCompletionSource<OrchestrationState> without TaskCreationOptions.RunContinuationsAsynchronously.
  2. CompleteTaskOrchestrationWorkItemAsync acquires lock (this.thisLock) and blocks synchronously on CommitState(...).GetAwaiter().GetResult().
  3. CommitState, while still holding this.thisLock, schedules Task.Run(() => tcs.TrySetResult(state)) and blocks in Task.WaitAll(...).

When SynchronizationContext.Current is null (for example in console apps, ASP.NET Core, or xUnit v3 tests), tcs.TrySetResult(state) can inline the caller's await continuation synchronously on that worker task before TrySetResult returns. If that continuation calls any LocalOrchestrationService method that acquires this.thisLock (such as GetOrchestrationStateAsync or CreateOrchestrationInstanceAsync), the continuation blocks waiting for this.thisLock while the orchestration dispatcher thread holds this.thisLock waiting in Task.WaitAll for TrySetResult to return.

Fix

  • Create waiter TaskCompletionSource<OrchestrationState> instances with TaskCreationOptions.RunContinuationsAsynchronously.
  • Make CommitState synchronous (void, as it performs only in-memory dictionary updates), removing .GetAwaiter().GetResult(), Task.Run, and Task.WaitAll, and invoke TrySetResult on collected waiters only after releasing this.thisLock.

Validation

  • Added WaitForOrchestrationAsync_ContinuationReenteringService_DoesNotDeadlock in EmulatorFunctionalTests:
    • Reproduces the deadlock deterministically against main (net8.0).
    • Verifies both GetOrchestrationStateAsync and CreateOrchestrationInstanceAsync on the WaitForOrchestrationAsync continuation with SynchronizationContext.Current == null.
  • Passed all DurableTask.Emulator.Tests in Release on both net8.0 (6/6) and net48 (6/6).
  • git diff --check passed.

Fixes #447

…deadlock (Azure#447)

LocalOrchestrationService.CompleteTaskOrchestrationWorkItemAsync previously invoked CommitState(...).GetAwaiter().GetResult() while holding thisLock, and CommitState signaled orchestrationWaiters via Task.Run(() => tcs.TrySetResult(state)) followed by Task.WaitAll while still holding thisLock. Because the waiter TaskCompletionSource was created without TaskCreationOptions.RunContinuationsAsynchronously, awaiting WaitForOrchestrationAsync without a SynchronizationContext could inline the caller continuation onto the TrySetResult thread and deadlock as soon as the continuation re-entered LocalOrchestrationService (e.g., GetOrchestrationStateAsync or CreateTaskOrchestrationAsync).

Fix this by:
- Creating waiter TaskCompletionSource instances with TaskCreationOptions.RunContinuationsAsynchronously.
- Making CommitState synchronous (removing GetAwaiter().GetResult(), Task.Run, and Task.WaitAll) and signaling waiter TaskCompletionSources only after releasing thisLock.
- Adding a deterministic regression test covering GetOrchestrationStateAsync and CreateOrchestrationInstanceAsync on the continuation of WaitForOrchestrationAsync.

Fixes Azure#447

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings November 18, 2026 17:23
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

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.

🟢 Approval recommended

No active review issues block approval of the emulator deadlock fix.

No open issues 🎉

What changed in this PR

Fixes the emulator deadlock described in #447, where a WaitForOrchestrationAsync continuation re-enters LocalOrchestrationService while its internal lock is held.

Changes:

  • Runs waiter continuations asynchronously and signals completion after releasing the service lock.
  • Makes state commits synchronous, removing blocking task coordination.
  • Adds a regression test covering state queries and duplicate-instance creation from the completion continuation.
File Description
test/​DurableTask.Emulator.Tests/​EmulatorFunctionalTests.cs Adds a gated regression test for continuation re-entry without a synchronization context.
src/​DurableTask.Emulator/​LocalOrchestrationService.cs Moves waiter notification outside the lock and removes blocking completion tasks.

Review effort: Balanced

@golddove

Copy link
Copy Markdown
Member Author

/azp run

@azure-pipelines

Copy link
Copy Markdown
Commenter does not have sufficient privileges for PR 1440 in repo Azure/durabletask

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.

🟢 Approval recommended

No active review issues are reported for the emulator’s reentrancy-deadlock fix.

No open issues 🎉

Review effort: Max

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Emulator hangs indefinitely instead of throwing expected error

2 participants