#8489·elsa-core

Drain force-cancel never reaches running activities and leaves unrecoverable Finished/Cancelled instances (FR-019)

Author: sfmskywalkerCreated Sep 26, 2026Updated Sep 27, 2026
Labelsbugprio highelsa 3triaged

Summary

When drain force-cancels (deadline breach or operator force), the cancellation never reaches the running activity. The in-flight activity runs to completion, and work items that were already queued keep executing. If the cycle then finishes on its own, it persists as a terminal Finished/Cancelled, which drain has left alone since #8426. The instance is never recovered, which contradicts FR-019.

Split out of #8467 (the Architect comment there), so that #8467 can close on its own fix. Found while de-flaking DrainRecoveryEndToEndTests on #8426.

Evidence (main @ 24349a958; all files identical on release/3.9.0)

  • Elsa.Workflows.Runtime/Models/ExecutionCycleHandle.cs L51-54, L73-74: the cycle CTS is a standalone CTS that is only fed by the caller's token. CancellationToken has no production reader; ExecutionCycleRegistry, DrainOrchestrator, ExecutionCycleAwareCommitStateHandler and the middleware never read it. The ctor XML doc (L29-37) acknowledges this.
  • Elsa.Workflows.Runtime/Middleware/Workflows/ExecutionCycleTrackingMiddleware.cs L47-51: the handle is created after the WorkflowExecutionContext exists. Its source is context.CancellationToken (caller → cycle), and force-cancel reaches the workflow only through cancelCallback: context.Cancel.
  • Elsa.Workflows.Core/Contexts/WorkflowExecutionContext.Cancel.cs L14-36: Cancel() cancels the private linked CTS, whose only listener is CancelWorkflow. That sets Cancelled and clears bookmarks and completion callbacks.
  • Elsa.Workflows.Core/Contexts/WorkflowExecutionContext.cs L80, L84-86: CancellationToken stays the caller's token, and the linked CTS token is not exposed. L588: every ActivityExecutionContext copies that caller token.
  • So the checks in DefaultActivityInvokerMiddleware (L34, L42), activity code, and the scheduler loop guard in DefaultActivitySchedulerMiddleware (L34-38) never observe the force-cancel. Already-queued work items keep running after Cancel().
  • DrainOrchestrator.cs L388-420 (Phase A) calls handle.TryCancel(). That runs the callback above and cancels the unread cycle CTS, then waits for the cycle to settle (L447-465).

Contract

  • FR-019 (specs/002-graceful-shutdown/spec.md L123): the instance "remains resumable".
  • specs/002-graceful-shutdown/contracts/drain-orchestrator.md L57, spec.md L166 and plan.md L33: cancellation reaches activities through the existing ActivityExecutionContext.CancellationToken.

Why simply linking the token is not enough

  • The activity-level ExceptionHandlingMiddleware (Elsa.Workflows.Core/Middleware/Activities/ExceptionHandlingMiddleware.cs L33-35) faults on any exception, including OperationCanceledException. A prompt throw on cancellation becomes a fault and an incident.
  • A clean exit after Cancel() persists Finished/Cancelled, and drain has left those rows alone since #8426 (#8419), so the instance is never recovered. Faster cancellation would push more instances down this path.

Fix direction (3.10; a redesign, not patch-safe)

  • Drain interrupts instead of cancelling. Skip the terminal Cancelled transition and its commit, so the instance ends Running + Interrupted at its last commit. It is then recovered through the re-run path fixed in #8467.
  • Cooperative cancellation reaches activities through the token: use the linked CTS for activity execution (the ActivityExecutionContext token and the scheduler guard). Keep commits and notifications on the caller's token. Let OperationCanceledException from a cancelled execution through instead of faulting.
  • The docs state that activities must honour context.CancellationToken. For CPU-bound code, force-cancel is best-effort.
  • User cancellation (CancelWorkflowMiddleware, the Alterations CancelHandler) must still persist Finished/Cancelled.

Tests

test/integration/Elsa.Workflows.IntegrationTests/GracefulShutdown/DrainRecoveryEndToEndTests.cs currently asserts the known-wrong behaviour: the drain-cancelled instance stays Finished/Cancelled (L107-110, L136-139; the gap is noted at L89 and L180-182). Keep these tests in place with comments pointing to this issue; the fix flips their assertions. New tests:

  • A long-running activity that honours the token stops within the settle timeout. The instance ends Running/Interrupted and is recovered with that activity re-run once.
  • A user cancel still persists Finished/Cancelled.

Scope

  • 3.9.0 ships with this as a known limitation.
  • Related: #8467 (re-run of interrupted activities; this fix depends on it), #8426 (drain no longer promotes Finished/Cancelled rows), #8420 (tenant-blind drain).

Source: elsa-workflows/elsa-core