{"record":{"id":"a22bf3ad3f54b0fb","repo":"invoke-ai/InvokeAI","slug":"unexpected-error-while-clearing-queue-e","errorCode":null,"errorMessage":"Unexpected error while clearing queue: {e}","messagePattern":"Unexpected error while clearing queue: (.+?)","errorType":"http","errorClass":"HTTPException","httpStatus":500,"severity":"error","filePath":"invokeai/app/api/routers/session_queue.py","lineNumber":444,"sourceCode":")\ndef clear(\n    current_user: CurrentUserOrDefault,\n    queue_id: str = Path(description=\"The queue id to perform this operation on\"),\n) -> ClearResult:\n    \"\"\"Clears the queue. Admin users clear (and cancel) all items; non-admin users clear only their\n    own items — other users' queued and running items are untouched.\"\"\"\n    try:\n        # The service cancels every in-progress item in scope itself (there can be several\n        # with multiple workers), so there is no per-item authorization to do here: a\n        # non-admin's scope is exactly their own items. The previous single get_current()\n        # check both 403'd users whose arbitrary selected row belonged to someone else and\n        # let a scoped clear interrupt another user's running generation.\n        user_id = None if current_user.is_admin else current_user.user_id\n        return ApiDependencies.invoker.services.session_queue.clear(queue_id, user_id=user_id)\n    except HTTPException:\n        raise\n    except Exception as e:\n        raise HTTPException(status_code=500, detail=f\"Unexpected error while clearing queue: {e}\")\n\n\n@session_queue_router.put(\n    \"/{queue_id}/prune\",\n    operation_id=\"prune\",\n    responses={\n        200: {\"model\": PruneResult},\n    },\n)\ndef prune(\n    current_user: CurrentUserOrDefault,\n    queue_id: str = Path(description=\"The queue id to perform this operation on\"),\n) -> PruneResult:\n    \"\"\"Prunes all completed or errored queue items. Non-admin users can only prune their own items.\"\"\"\n    try:\n        # Admin users can prune all items, non-admin users can only prune their own\n        user_id = None if current_user.is_admin else current_user.user_id\n        return ApiDependencies.invoker.services.session_queue.prune(queue_id, user_id=user_id)","sourceCodeStart":426,"sourceCodeEnd":462,"githubUrl":"https://github.com/invoke-ai/InvokeAI/blob/0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06/invokeai/app/api/routers/session_queue.py#L426-L462","documentation":"Catch-all 500 for the clear route: exceptions from session_queue.clear() (which deletes queued items, optionally scoped to a user) are wrapped as HTTP 500. HTTPExceptions pass through untouched, so this only fires for unexpected service/DB failures.","triggerScenarios":"PUT /api/v1/queue/{queue_id}/clear when the service call raises: DB failure, unknown queue, lock contention, or a bug in the clear implementation.","commonSituations":"Clearing a large queue causing long DB transactions/locks; SQLite file locked by another process (e.g., backup); queue_id typo; post-upgrade schema mismatch.","solutions":["Read the {e} detail and server traceback for the root cause.","Verify the DB is writable and not locked (stop backups/other processes holding the SQLite file).","Confirm queue_id is valid via GET status endpoint.","Retry after transient contention; run migrations after upgrades."],"exampleFix":"// before\nclear_all_ids = [42, 43]\n// after: distinguish truly unexpected failures\nresp = requests.put(f\"{base}/api/v1/queue/{queue_id}/clear\")\nif resp.status_code == 500:\n    logging.error(\"queue clear failed: %s\", resp.json().get('detail'))\n    raise SystemExit(1)","handlingStrategy":"try-catch","validationCode":"const s = await fetch(`${base}/api/v1/queue/${queueId}/status`);\nif (!s.ok) throw new Error(`queue ${queueId} not available for clear`);","typeGuard":"function isClearResult(v): v is { canceled: number } | { deleted: number } {\n  return typeof v === 'object' && v !== null && ('canceled' in v || 'deleted' in v);\n}","tryCatchPattern":"try {\n  const r = await fetch(`${base}/api/v1/queue/${queueId}/clear`, { method: 'PUT' });\n  if (!r.ok) {\n    const detail = (await r.json()).detail ?? '';\n    throw new Error(`clear failed: ${detail}`);\n  }\n} catch (e) {\n  logger.error('queue clear error', e); // check DB locks/writability\n}","preventionTips":["Ensure no process holds the SQLite file during clear","Confirm queue_id before destructive operations","Run DB migrations after every upgrade","Clear during idle periods to avoid lock contention"],"tags":["http-500","fastapi","queue","database"],"backgroundTag":"unhandled-exception-wrapped-as-500","analyzedSha":"0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06","analyzedAt":"2026-08-29T04:46:49.967Z","schemaVersion":2},"datasetVersion":"2026-08-29T07:17:48.351Z"}