Drain force-cancel never reaches running activities and leaves unrecoverable Finished/Cancelled instances (FR-019)
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.csL51-54, L73-74: the cycle CTS is a standalone CTS that is only fed by the caller's token.CancellationTokenhas no production reader;ExecutionCycleRegistry,DrainOrchestrator,ExecutionCycleAwareCommitStateHandlerand the middleware never read it. The ctor XML doc (L29-37) acknowledges this.Elsa.Workflows.Runtime/Middleware/Workflows/ExecutionCycleTrackingMiddleware.csL47-51: the handle is created after theWorkflowExecutionContextexists. Its source iscontext.CancellationToken(caller → cycle), and force-cancel reaches the workflow only throughcancelCallback: context.Cancel.Elsa.Workflows.Core/Contexts/WorkflowExecutionContext.Cancel.csL14-36:Cancel()cancels the private linked CTS, whose only listener isCancelWorkflow. That setsCancelledand clears bookmarks and completion callbacks.Elsa.Workflows.Core/Contexts/WorkflowExecutionContext.csL80, L84-86:CancellationTokenstays the caller's token, and the linked CTS token is not exposed. L588: everyActivityExecutionContextcopies that caller token.- So the checks in
DefaultActivityInvokerMiddleware(L34, L42), activity code, and the scheduler loop guard inDefaultActivitySchedulerMiddleware(L34-38) never observe the force-cancel. Already-queued work items keep running afterCancel(). DrainOrchestrator.csL388-420 (Phase A) callshandle.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.mdL123): the instance "remains resumable". specs/002-graceful-shutdown/contracts/drain-orchestrator.mdL57,spec.mdL166 andplan.mdL33: cancellation reaches activities through the existingActivityExecutionContext.CancellationToken.
Why simply linking the token is not enough
- The activity-level
ExceptionHandlingMiddleware(Elsa.Workflows.Core/Middleware/Activities/ExceptionHandlingMiddleware.csL33-35) faults on any exception, includingOperationCanceledException. A prompt throw on cancellation becomes a fault and an incident. - A clean exit after
Cancel()persistsFinished/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+Interruptedat 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
ActivityExecutionContexttoken and the scheduler guard). Keep commits and notifications on the caller's token. LetOperationCanceledExceptionfrom 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 AlterationsCancelHandler) must still persistFinished/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/Interruptedand 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