Cancel runs
Request cancellation and help a running task stop safely.
Use cancellation when queued or active work is no longer wanted. Cancellation is cooperative for a running handler; Runlane cannot undo an external side effect that already happened.
Make the handler cancellable
Put cancellation handling inside the task, for example in src/tasks/rebuild-index.ts:
import { task } from '@runlane/core'
import * as z from 'zod'
const env = z.object({ SEARCH_API_URL: z.url() }).parse(process.env)
export const rebuildIndex = task({
id: 'search.rebuild-index',
schema: z.object({ indexId: z.string() }),
async run({ indexId }, context) {
const response = await fetch(new URL(`/indexes/${encodeURIComponent(indexId)}/rebuild`, env.SEARCH_API_URL), {
method: 'POST',
signal: context.signal,
})
if (!response.ok) throw new Error(`Search API returned ${response.status}`)
},
})Pass context.signal to I/O APIs that support AbortSignal and let errors propagate. Check context.isCancellationRequested() between chunks of long work if useful. It becomes true only after the worker sees the request, which may take until its next lease refresh.
A thrown error is kept on the attempt, available through runs.attempts(). An oversized return records failure.code: storage_limit_exceeded instead of output.
Without a cancel request, worker shutdown requeues an interrupted attempt without spending a retry. See Stop crash loops for the interruption allowance. An attempt deadline records task_timed_out. If the worker loses its lease, Runlane discards that attempt's result and recovery finishes the run.
Request cancellation
Call runs.cancel() from the application or operator module that authorizes the request:
import { type RunId, type RunlaneRuntime } from '@runlane/core'
export async function cancelRun(runlane: RunlaneRuntime, runId: RunId) {
return runlane.runs.cancel(runId, {
reason: 'customer_deleted_project',
})
}A waiting run can become terminal cancelled immediately. A running run first becomes cancellation_requested; the owning worker observes durable state, aborts context.signal, and records the eventual result.
Cancellation wins when the attempt settles, whether the handler returns, throws, releases, or waits. No retry is scheduled after a cancel request.
Cancel a parent waiting on children
Cancelling a parent parked on a child join also cancels unfinished children it created, unless another registered waiter or another parent's named binding protects them. Unjoined children keep running. Reused children from a different creator keep running too.
A raw join on another run's child does not guarantee protection for later group members. Such a join receives a cancelled child's result normally. Use a named step trigger or an idempotent child of your own for shared-child protection. See awaited-child cancellation.
If immediate child cancellation fails after the parent is saved, the returned run includes deliveryError. Keep the saved result and let maintenance retry; do not repeat the cancellation.
Keep MaintenancePhase.RunFinalization enabled. It retries saved child cancellations after a crash or a failed eager pass. A successful parent cancellation does not mean every running child has stopped.
Verify and recover
Read the returned run and then runlane.runs.get(run.id). For active work, expect the intermediate cancellation_requested status before terminal completion. Test that the task observes the signal and stops starting new side effects.
A non-cooperative handler can remain active until it returns or loses its lease. Maintenance can finalize the cancellation after the owner lease expires.
If a run stays cancellation_requested, check worker lease refresh, handler signal use, and maintenance. Repeating the cancel request will not fix those paths.
Cancellation does not compensate completed work. Put refunds, deletes, and other compensation in explicit domain tasks.
Use retry and rerun when the goal is a new run rather than stopping the current one.
If the handler returns a value after a cancel request, Runlane keeps it on the cancelled attempt in runs.attempts(). The run itself has no output, and joins still see a cancelled child.