Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 22 additions & 16 deletions src/DurableTask.Emulator/LocalOrchestrationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ public async Task<OrchestrationState> WaitForOrchestrationAsync(

if (!this.orchestrationWaiters.TryGetValue(key, out TaskCompletionSource<OrchestrationState> tcs))
{
tcs = new TaskCompletionSource<OrchestrationState>();
tcs = new TaskCompletionSource<OrchestrationState>(TaskCreationOptions.RunContinuationsAsynchronously);

if (!this.orchestrationWaiters.TryAdd(key, tcs))
{
Expand Down Expand Up @@ -417,6 +417,8 @@ public Task CompleteTaskOrchestrationWorkItemAsync(
TaskMessage continuedAsNewMessage,
OrchestrationState state)
{
List<KeyValuePair<TaskCompletionSource<OrchestrationState>, OrchestrationState>> waitersToSignal = null;

lock (this.thisLock)
{
byte[] newSessionState;
Expand Down Expand Up @@ -462,19 +464,30 @@ public Task CompleteTaskOrchestrationWorkItemAsync(
if (workItem.OrchestrationRuntimeState != newOrchestrationRuntimeState)
{
var oldState = Utils.BuildOrchestrationState(workItem.OrchestrationRuntimeState);
CommitState(workItem.OrchestrationRuntimeState, oldState).GetAwaiter().GetResult();
CommitState(workItem.OrchestrationRuntimeState, oldState, ref waitersToSignal);
}

if (state != null)
{
CommitState(newOrchestrationRuntimeState, state).GetAwaiter().GetResult();
CommitState(newOrchestrationRuntimeState, state, ref waitersToSignal);
}
}

if (waitersToSignal != null)
{
foreach (KeyValuePair<TaskCompletionSource<OrchestrationState>, OrchestrationState> waiter in waitersToSignal)
{
waiter.Key.TrySetResult(waiter.Value);
}
}

return Task.FromResult(0);
}

Task CommitState(OrchestrationRuntimeState runtimeState, OrchestrationState state)
void CommitState(
OrchestrationRuntimeState runtimeState,
OrchestrationState state,
ref List<KeyValuePair<TaskCompletionSource<OrchestrationState>, OrchestrationState>> waitersToSignal)
{
if (!this.instanceStore.TryGetValue(runtimeState.OrchestrationInstance.InstanceId, out Dictionary<string, OrchestrationState> mapState))
{
Expand All @@ -489,35 +502,28 @@ Task CommitState(OrchestrationRuntimeState runtimeState, OrchestrationState stat
if (state.OrchestrationStatus == OrchestrationStatus.Running
|| state.OrchestrationStatus == OrchestrationStatus.Pending)
{
return Task.FromResult(0);
return;
}

string key = runtimeState.OrchestrationInstance.InstanceId + "_" +
runtimeState.OrchestrationInstance.ExecutionId;

string key1 = runtimeState.OrchestrationInstance.InstanceId + "_";

var tasks = new List<Task>();

if (this.orchestrationWaiters.TryGetValue(key, out TaskCompletionSource<OrchestrationState> tcs))
{
tasks.Add(Task.Run(() => tcs.TrySetResult(state)));
(waitersToSignal ??= new List<KeyValuePair<TaskCompletionSource<OrchestrationState>, OrchestrationState>>())
.Add(new KeyValuePair<TaskCompletionSource<OrchestrationState>, OrchestrationState>(tcs, state));
}

// for instance id level waiters, we will not consider ContinueAsNew as a terminal state because
// the high level orchestration is still ongoing
if (state.OrchestrationStatus != OrchestrationStatus.ContinuedAsNew
&& this.orchestrationWaiters.TryGetValue(key1, out TaskCompletionSource<OrchestrationState> tcs1))
{
tasks.Add(Task.Run(() => tcs1.TrySetResult(state)));
}

if (tasks.Count > 0)
{
Task.WaitAll(tasks.ToArray());
(waitersToSignal ??= new List<KeyValuePair<TaskCompletionSource<OrchestrationState>, OrchestrationState>>())
.Add(new KeyValuePair<TaskCompletionSource<OrchestrationState>, OrchestrationState>(tcs1, state));
}

return Task.FromResult(0);
}

/// <inheritdoc />
Expand Down
80 changes: 80 additions & 0 deletions test/DurableTask.Emulator.Tests/EmulatorFunctionalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,62 @@ await worker.AddTaskOrchestrations(typeof(TestInheritedTasksOrchestration))
Assert.AreEqual(InheritedTestOrchestrationTasksB.JuggleResult, TestInheritedTasksOrchestration.JuggleResultB);
}

[TestMethod]
public async Task WaitForOrchestrationAsync_ContinuationReenteringService_DoesNotDeadlock()
{
using var orchestrationService = new LocalOrchestrationService();
var gateActivity = new GatedActivity();

var worker = new TaskHubWorker(orchestrationService);
await worker.AddTaskOrchestrations(typeof(GatedOrchestration))
.AddTaskActivities(gateActivity)
.StartAsync();

try
{
var client = new TaskHubClient(orchestrationService);
OrchestrationInstance instance = await client.CreateOrchestrationInstanceAsync(typeof(GatedOrchestration), null);

await gateActivity.Entered.Task;

using var waiterRegistered = new ManualResetEventSlim(false);
Task continuationTask = Task.Run(() =>
{
SynchronizationContext.SetSynchronizationContext(null);
Task flow = WaitAndReenterServiceAsync(client, instance);
waiterRegistered.Set();
return flow;
});

Assert.IsTrue(waiterRegistered.Wait(TimeSpan.FromSeconds(5)));
gateActivity.Release.TrySetResult(true);

Task finished = await Task.WhenAny(continuationTask, Task.Delay(TimeSpan.FromSeconds(5)));
Assert.AreSame(continuationTask, finished, "WaitForOrchestrationAsync continuation deadlocked re-entering LocalOrchestrationService.");
await continuationTask;
}
finally
{
await worker.StopAsync(true);
}

static async Task WaitAndReenterServiceAsync(TaskHubClient client, OrchestrationInstance instance)
{
OrchestrationState completed = await client.WaitForOrchestrationAsync(
instance,
TimeSpan.FromSeconds(10),
CancellationToken.None);
Assert.AreEqual(OrchestrationStatus.Completed, completed.OrchestrationStatus);

OrchestrationState state = await client.GetOrchestrationStateAsync(instance);
Assert.IsNotNull(state);
Assert.AreEqual(OrchestrationStatus.Completed, state.OrchestrationStatus);

await Assert.ThrowsExceptionAsync<OrchestrationAlreadyExistsException>(
() => client.CreateOrchestrationInstanceAsync(typeof(GatedOrchestration), instance.InstanceId, null));
}
}

private static void AssertTagsEqual(IDictionary<string, string> expectedTags, IDictionary<string, string> actualTags)
{
Assert.IsNotNull(actualTags);
Expand Down Expand Up @@ -472,5 +528,29 @@ public override async Task<string> RunTask(OrchestrationContext context, string
return string.Empty;
}
}

public sealed class GatedActivity : AsyncTaskActivity<string, string>
{
public TaskCompletionSource<bool> Entered { get; } =
new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);

public TaskCompletionSource<bool> Release { get; } =
new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);

protected override async Task<string> ExecuteAsync(TaskContext context, string input)
{
this.Entered.TrySetResult(true);
await this.Release.Task;
return "done";
}
}

public sealed class GatedOrchestration : TaskOrchestration<string, string>
{
public override Task<string> RunTask(OrchestrationContext context, string input)
{
return context.ScheduleTask<string>(typeof(GatedActivity), input);
}
}
}
}
Loading