{"record":{"id":"0a2c0fdf38e51c23","repo":"harry0703/MoneyPrinterTurbo","slug":"request-id-task-is-still-running","errorCode":null,"errorMessage":"{request_id}: task is still running","messagePattern":"(.+?): task is still running","errorType":"http","errorClass":"HttpException","httpStatus":409,"severity":"warning","filePath":"app/controllers/v1/video.py","lineNumber":290,"sourceCode":"    )\n\n\n@router.delete(\n    \"/tasks/{task_id}\",\n    response_model=TaskDeletionResponse,\n    summary=\"Delete a generated short video task\",\n)\ndef delete_video(request: Request, task_id: str = Path(..., description=\"Task ID\")):\n    request_id = base.get_task_id(request)\n    task = sm.state.get_task(task_id)\n    if task:\n        if tm.is_task_busy(task):\n            logger.warning(\n                f\"refuse to delete busy task, request_id: {request_id}, \"\n                f\"task_id: {task_id}, state: {task.get('state')}, \"\n                f\"cross_post_state: {task.get('cross_post_state')}\"\n            )\n            raise HttpException(\n                task_id=task_id,\n                status_code=409,\n                message=f\"{request_id}: task is still running\",\n            )\n\n        tasks_dir = utils.task_dir()\n        current_task_dir = os.path.join(tasks_dir, task_id)\n        if os.path.exists(current_task_dir):\n            shutil.rmtree(current_task_dir)\n\n        sm.state.delete_task(task_id)\n        logger.success(f\"video deleted: {utils.to_json(task)}\")\n        return utils.get_response(200)\n\n    raise HttpException(\n        task_id=task_id, status_code=404, message=f\"{request_id}: task not found\"\n    )\n","sourceCodeStart":272,"sourceCodeEnd":308,"githubUrl":"https://github.com/harry0703/MoneyPrinterTurbo/blob/1f9f19c2021a68d04df228f33e9099a0c947f6f8/app/controllers/v1/video.py#L272-L308","documentation":"Returned by DELETE /api/v1/tasks/{task_id} in app/controllers/v1/video.py when the task exists but tm.is_task_busy(task) reports it running (state or cross_post_state active). Deletion is refused with 409 because removing the record and directory out from under an executing thread would corrupt in-flight work. The server logs task_id, state, and cross_post_state at warning level for diagnosis.","triggerScenarios":"DELETE while the task state is a running/queued state; deleting immediately after POST /videos without waiting for completion; deleting while a cross-post step is still in flight.","commonSituations":"Cleanup scripts that delete 'all tasks' including in-flight ones; UI delete buttons enabled before the status terminal event; long generations where the user assumes completion but cross_post_state is still active.","solutions":["Poll GET /tasks/{id} until state reaches a terminal value, then retry the DELETE.","If the task appears wedged (busy far beyond normal duration), investigate the worker/queue state before force-cleaning directories manually.","Make cleanup scripts skip tasks whose state is running/queued rather than deleting blindly."],"exampleFix":"# before\nrequests.delete(f\"{base}/api/v1/tasks/{task_id}\", headers=h).raise_for_status()\n\n# after\nimport time\nwhile True:\n    task = requests.get(f\"{base}/api/v1/tasks/{task_id}\", headers=h).json()[\"data\"]\n    if task[\"state\"] not in (\"running\", \"queued\", \"pending\"):\n        break\n    time.sleep(5)\nrequests.delete(f\"{base}/api/v1/tasks/{task_id}\", headers=h).raise_for_status()","handlingStrategy":"retry","validationCode":"task = requests.get(f\"{base}/api/v1/tasks/{task_id}\", headers=h).json()[\"data\"]\nif task.get(\"state\") in BUSY_STATES or task.get(\"cross_post_state\") in BUSY_STATES:\n    # defer the delete until terminal\n    schedule_retry_later(task_id)","typeGuard":"BUSY_STATES = {\"running\", \"queued\", \"pending\", \"processing\"}\ndef is_task_idle(task: dict) -> bool:\n    return task.get(\"state\") not in BUSY_STATES and task.get(\"cross_post_state\") not in BUSY_STATES","tryCatchPattern":"for _ in range(MAX_WAIT // POLL_SEC):\n    resp = requests.delete(f\"{base}/api/v1/tasks/{task_id}\", headers=h)\n    if resp.status_code == 409:\n        time.sleep(POLL_SEC); continue\n    if resp.status_code == 404:\n        return  # already gone\n    resp.raise_for_status(); return\nraise RuntimeError(\"task stayed busy; manual intervention needed\")","preventionTips":["Disable delete actions in the UI until the task reaches a terminal state.","Check both state and cross_post_state before deleting.","Cleanup scripts should skip busy tasks, not force-delete them."],"tags":["http-409","task-lifecycle","concurrency","delete"],"backgroundTag":null,"analyzedSha":"1f9f19c2021a68d04df228f33e9099a0c947f6f8","analyzedAt":"2026-08-14T19:41:05.568Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}