From 2ebdea525b00212a08e093958e7458c074ca2b2c Mon Sep 17 00:00:00 2001 From: David Justo Date: Fri, 6 Jan 2023 13:57:51 -0800 Subject: [PATCH] enforce that only one of the two threads (user code and orchestration invoker) perform DF management logic at a time. --- src/DurableEngine/SharedMemory.cs | 10 ++++++++++ src/DurableEngine/Tasks/DurableTask.cs | 4 ++++ 2 files changed, 14 insertions(+) diff --git a/src/DurableEngine/SharedMemory.cs b/src/DurableEngine/SharedMemory.cs index 66093d8..20d02a1 100644 --- a/src/DurableEngine/SharedMemory.cs +++ b/src/DurableEngine/SharedMemory.cs @@ -65,5 +65,15 @@ public bool YieldToUserCodeThread(WaitHandle completionHandle) var shouldStop = index == 0; return shouldStop; } + + /// + /// Blocks user code thread if the orchestrator-invoker thread is currently running. + /// This guarantees that the user-code thread and the orchestration-invoker thread run one + /// at a time after this point. + /// + public void GuaranteeUserCodeTurn() + { + userCodeThreadTurn.WaitOne(); + } } } \ No newline at end of file diff --git a/src/DurableEngine/Tasks/DurableTask.cs b/src/DurableEngine/Tasks/DurableTask.cs index e9b3b0b..d05eadd 100644 --- a/src/DurableEngine/Tasks/DurableTask.cs +++ b/src/DurableEngine/Tasks/DurableTask.cs @@ -38,6 +38,10 @@ public DurableTask(SwitchParameter noWait, Hashtable privateData) /// Function to write an exception to the pipeline. public void Execute(Action write, Action writeErr) { + // Ensure that a DurableTask in the usercode thread + // only executes while the orchestration-invoker thread is blocked. + OrchestrationContext.SharedMemory.GuaranteeUserCodeTurn(); + DurableTask task = this; if (NoWait)