Limit task runtime
Give each attempt a fixed deadline and stop timed-out work cooperatively.
Use maxAttemptDuration when one task attempt must not run forever. The deadline starts when the attempt is claimed.
Set the task deadline
import { task } from '@runlane/core'
import * as z from 'zod'
const env = z.object({ SEARCH_API_URL: z.url() }).parse(process.env)
export const rebuildSearchIndex = task({
id: 'search.rebuild',
schema: z.object({ indexId: z.string() }),
maxAttemptDuration: '10m',
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}`)
},
})The deadline covers the handler and asynchronous output validation. When it expires, Runlane aborts context.signal and records task_timed_out.
A timeout follows the task's retry policy. If a policy is set and retries remain, the run becomes retrying. Otherwise it ends as failed with task_timed_out. Omit retry when a timeout should require an explicit operator or application decision before starting the work again.
Budget remaining work
context.run.attemptDeadlineAt is the current attempt's absolute deadline when an effective maxAttemptDuration applies. It is undefined otherwise. Runlane sets it from the runtime clock when recording the attempt start, after storage claim admission. Lease heartbeats do not move it. Each retry or resumed attempt gets its own deadline.
Budget a dependency call inside the handler, leaving time for settlement:
const deadline = context.run.attemptDeadlineAt
const remainingMs = deadline === undefined ? undefined : Math.max(0, deadline.getTime() - Date.now())
if (remainingMs !== undefined && remainingMs < 1000) {
throw new Error('Not enough attempt time remains for another request.')
}
const response = await fetch(url, { signal: context.signal })This uses the normal wall clock. In a test with a custom runtime clock, subtract that same clock's now().getTime() instead of Date.now(). Keep passing context.signal; reading the deadline alone does not stop work.
Cooperate with the signal
JavaScript cannot be stopped safely from the outside. Pass context.signal to HTTP clients, database calls, child processes, and other APIs that support AbortSignal.
Check cancellation before starting another side effect. Use your own resource limits for dependencies that ignore signals.
Override at an execution boundary
runNow(), delivery handlers, and workers accept a maxAttemptDuration override. The execution override takes precedence over the task value.
Use an override to replace the task duration for one execution boundary. It can shorten or lengthen the saved duration. Keep the task value for the normal business limit.
Understand dead owners
The fixed deadline is stored with the active attempt. If a worker disappears or ignores the abort, maintenance can finalize the timeout after ownership expires.
When both the lease and fixed deadline are due, timeout finalization wins. Reacquiring the run first would erase the durable timeout outcome.
Verify the boundary
- Use a controlled clock instead of sleeping.
- Confirm the task signal aborts at the deadline.
- Confirm the stored failure code is
task_timed_out. - Test a handler that ignores abort and let maintenance finalize it.
- Test output validation that crosses the deadline.
Use cancellation for operator-requested stops and retries for retryable dependency failures.