mastra-ai/mastra · error · Error
Cannot restart task in status '${task.status}' (expected 'ru
Error message
Cannot restart task in status '${task.status}' (expected 'running') What it means
restart() is only valid for tasks currently in the 'running' state; the manager throws this error for any other stored status. Restart semantics mean 'kill and re-run a running task', not 're-run an old finished task'.
Source
Thrown at packages/core/src/background-tasks/manager.ts:480
*
*/
async restart(taskId: string, context?: TaskContext): Promise<BackgroundTask> {
if (this.shuttingDown) {
throw new Error('BackgroundTaskManager is shutting down, cannot restart tasks');
}
if (!this.#mastra) {
throw new Error('Mastra is not registered with this manager');
}
if (this.initPromise) await this.initPromise;
const storage = await this.getStorage();
const task = await storage.getTask(taskId);
if (!task) {
throw new Error(`Task not found: ${taskId}`);
}
if (task.status !== 'running') {
throw new Error(`Cannot restart task in status '${task.status}' (expected 'running')`);
}
if (context) {
this.registerTaskContext(task.id, context);
}
const canRun = await this.checkConcurrency(task.agentId);
if (!canRun) {
// Restart sits outside the queue/fallback-sync paths — there's no
// synchronous caller to fall back to, and silently leaving the task
// running hides the failure from the caller. Throw and let the
// caller retry once a slot frees.
throw new Error(`Concurrency limit reached, cannot restart task "${taskId}" — retry once a slot is available`);
}
await this.dispatch(task, true);
return task;View on GitHub (pinned to 75dd419e61)
Solutions
- Check task.status === 'running' before calling restart; use resume() for suspended tasks.
- For failed/completed tasks, start a new task with the same parameters instead of restart().
- Refresh task state immediately before restart to avoid acting on stale status in UIs.
Example fix
// before await manager.restart(taskId); // task already 'failed' // after const task = await manager.getTask(taskId); if (task.status === 'running') await manager.restart(taskId); else await manager.start(task.input); // fresh run for non-running tasks
Defensive patterns
Strategy: type-guard
Validate before calling
async function assertRunning(manager, taskId) {
const task = await manager.getTask(taskId);
if (task?.status !== 'running') throw new Error(`Refusing restart: task status is ${task?.status}`);
} Type guard
function isRunningTask(task) {
return !!task && task.status === 'running';
} Try / catch
try {
await manager.restart(taskId);
} catch (e) {
const m = /Cannot restart task in status '(.+)'/.exec(e.message);
if (m) {
// route by actual status: suspended -> resume, else start new
} else throw e;
} Prevention
- Re-read task status immediately before restart in dashboards/automation.
- Map operations to states: restart only for 'running', resume only for 'suspended'.
- Debounce status-refresh + action pairs to avoid stale-state races.
When it happens
Trigger: Calling manager.restart(taskId) when the task's stored status is 'completed', 'failed', 'suspended', or 'cancelled' rather than 'running'.
Common situations: Restarting a task after it already finished (race between a status refresh and the restart call); attempting to retry a failed task with restart(); operator dashboards offering 'Restart' on finished tasks.
Related errors
- Cannot resume task in status '${task.status}' (expected 'sus
- tool_result must be preceded by a tool_call with the same to
- Unsupported Factory stage: ${stage}
- Mode "${mode.id}" transitionsTo cannot reference itself
- Mode "${mode.id}" transitionsTo references unknown mode "${m
AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30).
Data as JSON: /api/errors/08bdfd55bbfdcda0.
Report an issue: GitHub.