{"record":{"id":"73a5b634da9c4a06","repo":"invoke-ai/InvokeAI","slug":"unexpected-error-while-getting-next-queue-item-e","errorCode":null,"errorMessage":"Unexpected error while getting next queue item: {e}","messagePattern":"Unexpected error while getting next queue item: (.+?)","errorType":"http","errorClass":"HTTPException","httpStatus":500,"severity":"error","filePath":"invokeai/app/api/routers/session_queue.py","lineNumber":506,"sourceCode":"@session_queue_router.get(\n    \"/{queue_id}/next\",\n    operation_id=\"get_next_queue_item\",\n    responses={\n        200: {\"model\": Optional[SessionQueueItem]},\n    },\n)\ndef get_next_queue_item(\n    current_user: CurrentUserOrDefault,\n    queue_id: str = Path(description=\"The queue id to perform this operation on\"),\n) -> Optional[SessionQueueItem]:\n    \"\"\"Gets the next queue item, without executing it\"\"\"\n    try:\n        item = ApiDependencies.invoker.services.session_queue.get_next(queue_id)\n        if item is not None:\n            item = sanitize_queue_item_for_user(item, current_user.user_id, current_user.is_admin)\n        return item\n    except Exception as e:\n        raise HTTPException(status_code=500, detail=f\"Unexpected error while getting next queue item: {e}\")\n\n\n@session_queue_router.get(\n    \"/{queue_id}/status\",\n    operation_id=\"get_queue_status\",\n    responses={\n        200: {\"model\": SessionQueueAndProcessorStatus},\n    },\n)\ndef get_queue_status(\n    current_user: CurrentUserOrDefault,\n    queue_id: str = Path(description=\"The queue id to perform this operation on\"),\n) -> SessionQueueAndProcessorStatus:\n    \"\"\"Gets the status of the session queue. Returns global counts; every user additionally gets\n    their own pending/in_progress counts (so the UI can show an X/Y badge and scope personal UI\n    like the progress bar to the user's own activity). Non-admin users cannot see the current\n    item's identifiers unless they own it.\"\"\"\n    try:","sourceCodeStart":488,"sourceCodeEnd":524,"githubUrl":"https://github.com/invoke-ai/InvokeAI/blob/0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06/invokeai/app/api/routers/session_queue.py#L488-L524","documentation":"Catch-all 500 for get_next_queue_item: exceptions from session_queue.get_next() (or sanitization of the pending item) are wrapped as HTTP 500. Returns the next pending queue item or null when nothing is pending.","triggerScenarios":"GET /api/v1/queue/{queue_id}/next when the service call raises (DB failure, unknown queue) or sanitize_queue_item_for_user fails on malformed pending item data.","commonSituations":"DB lock/down during heavy queue churn; queue_id typo; corrupted pending item row; aggressive polling scripts hammering a failing DB.","solutions":["Inspect the {e} detail and server traceback for the root cause.","Verify queue_id and database health before increasing poll rates.","Back off polling on 500s instead of retrying in a tight loop (this worsens DB contention).","Repair/upgrade InvokeAI if item rows are corrupted or the service is misbehaving."],"exampleFix":"// before: tight polling loop\nwhile True: requests.get(f\"{base}/api/v1/queue/{queue_id}/next\")\n// after: back off on failure\nresp = requests.get(f\"{base}/api/v1/queue/{queue_id}/next\")\nif resp.status_code == 500:\n    time.sleep(min(backoff *= 2, 60))\nelse:\n    backoff = 1","handlingStrategy":"retry","validationCode":"const status = await fetch(`${base}/api/v1/queue/${queueId}/status`).then(r => r.json());\nif (status.pending === 0) return null; // skip next-item poll entirely","typeGuard":"function isPendingQueueItem(v) {\n  return v !== null && typeof v === 'object' && 'item_id' in v && v.status === 'pending';\n}","tryCatchPattern":"async function pollNext() {\n  const r = await fetch(`${base}/api/v1/queue/${queueId}/next`);\n  if (r.status === 500) {\n    await sleep(Math.min((backoff *= 2), 60000));\n    return pollNext();\n  }\n  backoff = 1000;\n  return r.json(); // null when nothing pending\n}","preventionTips":["Use exponential backoff on 500s; never tight-loop","Check pending counts before polling for next item","Confirm queue_id validity at startup","Investigate DB health if 500s recur under load"],"tags":["http-500","fastapi","queue","polling"],"backgroundTag":"unhandled-exception-wrapped-as-500","analyzedSha":"0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06","analyzedAt":"2026-08-29T04:46:49.967Z","schemaVersion":2},"datasetVersion":"2026-08-29T07:17:48.351Z"}