Repository navigation
Fix LocalOrchestrationService WaitForOrchestrationAsync deadlock - #1440
Open
Risheek Rajolu (golddove) wants to merge 1 commit into
Open
Risheek Rajolu (golddove) wants to merge 1 commit into
Risheek Rajolu (golddove) wants to merge 1 commit into
Conversation
…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>
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
Contributor
There was a problem hiding this comment.
🟢 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
Member
Author
|
/azp run |
|
Commenter does not have sufficient privileges for PR 1440 in repo Azure/durabletask |
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 a deadlock in
LocalOrchestrationServicewhen a caller awaitsWaitForOrchestrationAsyncwithout aSynchronizationContextand immediately calls back intoLocalOrchestrationServiceon the continuation.Problem
WaitForOrchestrationAsynccreates its waiterTaskCompletionSource<OrchestrationState>withoutTaskCreationOptions.RunContinuationsAsynchronously.CompleteTaskOrchestrationWorkItemAsyncacquireslock (this.thisLock)and blocks synchronously onCommitState(...).GetAwaiter().GetResult().CommitState, while still holdingthis.thisLock, schedulesTask.Run(() => tcs.TrySetResult(state))and blocks inTask.WaitAll(...).When
SynchronizationContext.Currentisnull(for example in console apps, ASP.NET Core, or xUnit v3 tests),tcs.TrySetResult(state)can inline the caller'sawaitcontinuation synchronously on that worker task beforeTrySetResultreturns. If that continuation calls anyLocalOrchestrationServicemethod that acquiresthis.thisLock(such asGetOrchestrationStateAsyncorCreateOrchestrationInstanceAsync), the continuation blocks waiting forthis.thisLockwhile the orchestration dispatcher thread holdsthis.thisLockwaiting inTask.WaitAllforTrySetResultto return.Fix
TaskCompletionSource<OrchestrationState>instances withTaskCreationOptions.RunContinuationsAsynchronously.CommitStatesynchronous (void, as it performs only in-memory dictionary updates), removing.GetAwaiter().GetResult(),Task.Run, andTask.WaitAll, and invokeTrySetResulton collected waiters only after releasingthis.thisLock.Validation
WaitForOrchestrationAsync_ContinuationReenteringService_DoesNotDeadlockinEmulatorFunctionalTests:main(net8.0).GetOrchestrationStateAsyncandCreateOrchestrationInstanceAsyncon theWaitForOrchestrationAsynccontinuation withSynchronizationContext.Current == null.DurableTask.Emulator.TestsinReleaseon bothnet8.0(6/6) andnet48(6/6).git diff --checkpassed.Fixes #447