Skip to content

Commit a9cfcd6

Browse files
authored
Prevent races between orchestration-invoker and user code (#26)
1 parent c53ede6 commit a9cfcd6

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

src/DurableEngine/SharedMemory.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,15 @@ public bool YieldToUserCodeThread(WaitHandle completionHandle)
6565
var shouldStop = index == 0;
6666
return shouldStop;
6767
}
68+
69+
/// <summary>
70+
/// Blocks user code thread if the orchestrator-invoker thread is currently running.
71+
/// This guarantees that the user-code thread and the orchestration-invoker thread run one
72+
/// at a time after this point.
73+
/// </summary>
74+
public void GuaranteeUserCodeTurn()
75+
{
76+
userCodeThreadTurn.WaitOne();
77+
}
6878
}
6979
}

src/DurableEngine/Tasks/DurableTask.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ internal OrchestrationAction GetOrCreateAction()
4949
/// <param name="writeErr">Function to write an exception to the pipeline.</param>
5050
public void Execute(Action<object> write, Action<ErrorRecord> writeErr)
5151
{
52+
// Ensure that a DurableTask in the usercode thread
53+
// only executes while the orchestration-invoker thread is blocked.
54+
OrchestrationContext.SharedMemory.GuaranteeUserCodeTurn();
55+
5256
DurableTask task = this;
5357

5458
if (NoWait)

0 commit comments

Comments
 (0)