Can I send an abortSignal to a workflow?
can i send an abortSignal to a workflow?
Now I have a comprehensive understanding. Here's the answer:
No, you cannot send an AbortSignal to a workflow run itself — but there are other mechanisms for cancellation, and AbortSignal is supported in specific sub-contexts.
###Cancelling a Workflow Run
Workflows don't accept an AbortSignal when started. The start() function (packages/core/src/runtime/start.ts) only accepts these options:
export interface StartOptions {
deploymentId?: string;
world?: World;
specVersion?: number;
}
Instead, cancellation is done after the fact via the event-sourced run_cancelled event. You have two ways to cancel a running workflow:
-
Programmatically using the
Runobject (packages/core/src/runtime/run.ts:69-73):const run = await start(myWorkflow, args); // later... await run.cancel(); -
Via the
cancelRunfunction (packages/core/src/runtime/runs.ts:88):import { cancelRun } from '@workflow/core/runtime'; await cancelRun(world, runId); -
Via the CLI:
npx workflow cancel <run_id>
This sets the run's status to 'cancelled' and throws a WorkflowRunCancelledError if you're awaiting the returnValue.
###Why No AbortSignal?
Workflows are durable and event-sourced — they survive across serverless invocations and can be suspended/resumed. A JavaScript AbortSignal is an in-process, in-memory construct that doesn't survive across those boundaries. The cancellation model is instead based on persisted events (run_cancelled), which fits the durable execution model.
###Where AbortSignal is Supported
-
@workflow/aiDurable Agent (packages/ai/src/agent/durable-agent.ts:197): ThedurableAgent()function accepts anabortSignaloption to cancel AI generation within a step. It also has anonAbortcallback. -
@workflow/aiChat Transport (packages/ai/src/workflow-chat-transport.ts:18): Accepts anabortSignalto cancel HTTP requests to the workflow backend. -
Client-side stream reading (e.g.,
packages/web/app/lib/client/workflow-streams.ts:20): You can pass asignalwhen fetching a workflow's output stream, but this only cancels the read — not the workflow itself.
###Summary
| What you want to do | How |
|---|---|
| Cancel a workflow run | run.cancel(), cancelRun(world, runId), or npx workflow cancel <id> |
| Abort an AI generation within a step | Pass abortSignal to durableAgent() |
| Cancel reading a workflow stream | Pass signal to the fetch/stream reader |
END OF POST