{"record":{"id":"8fc79e87ac2732bd","repo":"t8y2/dbx","slug":"failed-to-create-agent-runtime","errorCode":null,"errorMessage":"failed to create agent runtime","messagePattern":"failed to create agent runtime","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/dbx-web/src/routes/ai.rs","lineNumber":473,"sourceCode":"        schema: body.schema,\n        db_type: parsed_db_type,\n        cli_mcp_server_command: None,\n        sql_permissions,\n        max_agent_turns,\n    };\n\n    let sid = session_id.clone();\n    let mut req_config = request.config;\n    dbx_core::ai::merge_global_max_retries(&mut req_config, max_retries);\n    let req_system_prompt = request.system_prompt;\n    let req_messages = request.messages;\n    let req_task_contract = request.task_contract;\n    let req_max_tokens = request.max_tokens;\n    let is_agent_mode = body.mode == \"agent\";\n    let tx2 = tx.clone();\n    tokio::task::spawn_blocking(move || {\n        let rt =\n            tokio::runtime::Builder::new_current_thread().enable_all().build().expect(\"failed to create agent runtime\");\n        rt.block_on(async move {\n            let result = run_agent_loop(\n                &req_config,\n                &req_system_prompt,\n                &req_messages,\n                &agent_ctx,\n                move |event: AgentEvent| {\n                    let json = serde_json::to_string(&event).unwrap_or_default();\n                    let _ = tx2.send(json);\n                },\n                &cancelled,\n                req_max_tokens,\n                req_task_contract.as_ref(),\n                is_agent_mode,\n            )\n            .await;\n\n            if let Err(e) = result {","sourceCodeStart":455,"sourceCodeEnd":491,"githubUrl":"https://github.com/t8y2/dbx/blob/c0390bff16418b651f4728520d99adf8ce48829a/crates/dbx-web/src/routes/ai.rs#L455-L491","documentation":"In the AI agent streaming route, a blocking task builds a fresh single-threaded tokio runtime via tokio::runtime::Builder::new_current_thread().enable_all().build() and expects success. If the runtime cannot be constructed, the panic happens inside the spawn_blocking worker for the agent stream.","triggerScenarios":"Runtime::build() fails during OS resource acquisition — typically thread/timer/io-driver setup failing because process limits are exhausted (fds, memory) or the builder configuration is invalid.","commonSituations":"Heavy load exhausting file descriptors or memory so the io driver cannot be created; embedding in environments with restricted thread creation (seccomp, low RLIMIT_NPROC); many concurrent agent streams each spawning runtimes and hitting limits.","solutions":["Raise process limits (ulimit -n for fds, RLIMIT_NPROC) and available memory for the server process.","Reduce concurrent agent streams or pool/reuse a dedicated blocking runtime instead of building one per request.","Inspect the underlying io::Error from build() by logging it; enable tokio's runtime worker metrics if resource exhaustion is suspected.","Handle the error in the stream: send an SSE error event to the client instead of panicking the blocking worker."],"exampleFix":"// before\nlet rt = tokio::runtime::Builder::new_current_thread().enable_all().build().expect(\"failed to create agent runtime\");\n// after\nlet rt = tokio::runtime::Builder::new_current_thread()\n    .enable_all()\n    .build()\n    .map_err(|e| {\n        let _ = tx2.send(Err(format!(\"failed to create agent runtime: {e}\")));\n        e\n    })?;","handlingStrategy":"retry","validationCode":"fn can_spawn_runtime() -> bool {\n    tokio::runtime::Builder::new_current_thread().enable_all().build().is_ok()\n}","typeGuard":null,"tryCatchPattern":"// inside spawn_blocking, map the error into the stream instead of panicking\nlet rt = match tokio::runtime::Builder::new_current_thread().enable_all().build() {\n    Ok(rt) => rt,\n    Err(e) => {\n        let _ = tx2.send(Err(format!(\"failed to create agent runtime: {e}\")));\n        return;\n    }\n};","preventionTips":["Raise fd/memory/thread limits on hosts running many agent streams.","Cap concurrent agent requests with a semaphore.","Reuse a shared blocking runtime rather than one per request.","Send errors to the SSE channel so clients see a failure instead of a dropped stream."],"tags":["tokio","rust","runtime","ai","streaming"],"backgroundTag":"runtime-creation-failed","analyzedSha":"c0390bff16418b651f4728520d99adf8ce48829a","analyzedAt":"2026-09-05T23:05:10.900Z","contentChangedAt":"2026-09-05T23:05:10.900Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}