{"record":{"id":"08bdfd55bbfdcda0","repo":"mastra-ai/mastra","slug":"cannot-restart-task-in-status-task-status-ex","errorCode":null,"errorMessage":"Cannot restart task in status '${task.status}' (expected 'running')","messagePattern":"Cannot restart task in status '(.+?)' \\(expected 'running'\\)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/core/src/background-tasks/manager.ts","lineNumber":480,"sourceCode":"   *\n   */\n  async restart(taskId: string, context?: TaskContext): Promise<BackgroundTask> {\n    if (this.shuttingDown) {\n      throw new Error('BackgroundTaskManager is shutting down, cannot restart tasks');\n    }\n    if (!this.#mastra) {\n      throw new Error('Mastra is not registered with this manager');\n    }\n\n    if (this.initPromise) await this.initPromise;\n\n    const storage = await this.getStorage();\n    const task = await storage.getTask(taskId);\n    if (!task) {\n      throw new Error(`Task not found: ${taskId}`);\n    }\n    if (task.status !== 'running') {\n      throw new Error(`Cannot restart task in status '${task.status}' (expected 'running')`);\n    }\n\n    if (context) {\n      this.registerTaskContext(task.id, context);\n    }\n\n    const canRun = await this.checkConcurrency(task.agentId);\n    if (!canRun) {\n      // Restart sits outside the queue/fallback-sync paths — there's no\n      // synchronous caller to fall back to, and silently leaving the task\n      // running hides the failure from the caller. Throw and let the\n      // caller retry once a slot frees.\n      throw new Error(`Concurrency limit reached, cannot restart task \"${taskId}\" — retry once a slot is available`);\n    }\n\n    await this.dispatch(task, true);\n\n    return task;","sourceCodeStart":462,"sourceCodeEnd":498,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/core/src/background-tasks/manager.ts#L462-L498","documentation":"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'.","triggerScenarios":"Calling manager.restart(taskId) when the task's stored status is 'completed', 'failed', 'suspended', or 'cancelled' rather than 'running'.","commonSituations":"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.","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."],"exampleFix":"// before\nawait manager.restart(taskId); // task already 'failed'\n// after\nconst task = await manager.getTask(taskId);\nif (task.status === 'running') await manager.restart(taskId);\nelse await manager.start(task.input); // fresh run for non-running tasks","handlingStrategy":"type-guard","validationCode":"async function assertRunning(manager, taskId) {\n  const task = await manager.getTask(taskId);\n  if (task?.status !== 'running') throw new Error(`Refusing restart: task status is ${task?.status}`);\n}","typeGuard":"function isRunningTask(task) {\n  return !!task && task.status === 'running';\n}","tryCatchPattern":"try {\n  await manager.restart(taskId);\n} catch (e) {\n  const m = /Cannot restart task in status '(.+)'/.exec(e.message);\n  if (m) {\n    // route by actual status: suspended -> resume, else start new\n  } else throw e;\n}","preventionTips":["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."],"tags":["background-tasks","state-machine","invalid-state"],"backgroundTag":"task-not-restartable","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}