diff --git a/psalm-baseline.xml b/psalm-baseline.xml
index f6746e47..25246b10 100644
--- a/psalm-baseline.xml
+++ b/psalm-baseline.xml
@@ -1171,6 +1171,14 @@
+
+ context?->destroy()]]>
+ scopeContext?->destroy()]]>
+
+
+ context]]>
+ scopeContext]]>
+
diff --git a/src/Internal/Transport/Router/DestroyWorkflow.php b/src/Internal/Transport/Router/DestroyWorkflow.php
index 6d5d804a..2f703119 100644
--- a/src/Internal/Transport/Router/DestroyWorkflow.php
+++ b/src/Internal/Transport/Router/DestroyWorkflow.php
@@ -17,6 +17,7 @@
use Temporal\Internal\Support\GarbageCollector;
use Temporal\Internal\Workflow\Process\Process;
use Temporal\Internal\Workflow\ProcessCollection;
+use Temporal\Worker\FeatureFlags;
use Temporal\Worker\LoopInterface;
use Temporal\Worker\Transport\Command\ServerRequestInterface;
@@ -29,12 +30,14 @@ class DestroyWorkflow extends WorkflowProcessAwareRoute
private const GC_TIMEOUT_SECONDS = 30;
private GarbageCollector $gc;
+ private bool $throwDestructException;
public function __construct(
ProcessCollection $running,
protected LoopInterface $loop,
) {
$this->gc = new GarbageCollector(self::GC_THRESHOLD, self::GC_TIMEOUT_SECONDS);
+ $this->throwDestructException = FeatureFlags::$throwDestructMemorizedInstanceException;
parent::__construct($running);
}
@@ -51,7 +54,7 @@ public function kill(string $runId): array
$process = $this->running
->pull($runId, "Unable to kill workflow because workflow process #$runId was not found");
- $process->cancel(new DestructMemorizedInstanceException());
+ $this->throwDestructException and $process->cancel(new DestructMemorizedInstanceException());
$this->loop->once(
LoopInterface::ON_FINALLY,
function () use ($process): void {
diff --git a/src/Internal/Workflow/Process/Scope.php b/src/Internal/Workflow/Process/Scope.php
index 3c357c10..22d2832d 100644
--- a/src/Internal/Workflow/Process/Scope.php
+++ b/src/Internal/Workflow/Process/Scope.php
@@ -293,9 +293,16 @@ public function onAwait(Deferred $deferred): void
public function destroy(): void
{
- $this->scopeContext->destroy();
- $this->context->destroy();
- unset($this->coroutine);
+ $this->context?->destroy();
+ $this->scopeContext?->destroy();
+ unset(
+ $this->context,
+ $this->scopeContext,
+ $this->deferred,
+ $this->services,
+ $this->onCancel,
+ $this->onClose,
+ );
}
/**
diff --git a/src/Worker/FeatureFlags.php b/src/Worker/FeatureFlags.php
index 2c874a6e..724298f4 100644
--- a/src/Worker/FeatureFlags.php
+++ b/src/Worker/FeatureFlags.php
@@ -4,6 +4,7 @@
namespace Temporal\Worker;
+use Temporal\Exception\DestructMemorizedInstanceException;
use Temporal\Workflow;
/**
@@ -48,4 +49,11 @@ final class FeatureFlags
* @since SDK 2.16.0
*/
public static bool $cancelAbandonedChildWorkflows = true;
+
+ /**
+ * Throw {@see DestructMemorizedInstanceException} when a Workflow instance is destructed.
+ *
+ * @since SDK 2.16.0
+ */
+ public static bool $throwDestructMemorizedInstanceException = true;
}