t8y2/dbx · error

failed to create agent runtime

Error message

failed to create agent runtime

What it means

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.

Source

Thrown at crates/dbx-web/src/routes/ai.rs:473

        schema: body.schema,
        db_type: parsed_db_type,
        cli_mcp_server_command: None,
        sql_permissions,
        max_agent_turns,
    };

    let sid = session_id.clone();
    let mut req_config = request.config;
    dbx_core::ai::merge_global_max_retries(&mut req_config, max_retries);
    let req_system_prompt = request.system_prompt;
    let req_messages = request.messages;
    let req_task_contract = request.task_contract;
    let req_max_tokens = request.max_tokens;
    let is_agent_mode = body.mode == "agent";
    let tx2 = tx.clone();
    tokio::task::spawn_blocking(move || {
        let rt =
            tokio::runtime::Builder::new_current_thread().enable_all().build().expect("failed to create agent runtime");
        rt.block_on(async move {
            let result = run_agent_loop(
                &req_config,
                &req_system_prompt,
                &req_messages,
                &agent_ctx,
                move |event: AgentEvent| {
                    let json = serde_json::to_string(&event).unwrap_or_default();
                    let _ = tx2.send(json);
                },
                &cancelled,
                req_max_tokens,
                req_task_contract.as_ref(),
                is_agent_mode,
            )
            .await;

            if let Err(e) = result {

View on GitHub (pinned to c0390bff16)

Solutions

  1. Raise process limits (ulimit -n for fds, RLIMIT_NPROC) and available memory for the server process.
  2. Reduce concurrent agent streams or pool/reuse a dedicated blocking runtime instead of building one per request.
  3. Inspect the underlying io::Error from build() by logging it; enable tokio's runtime worker metrics if resource exhaustion is suspected.
  4. Handle the error in the stream: send an SSE error event to the client instead of panicking the blocking worker.

Example fix

// before
let rt = tokio::runtime::Builder::new_current_thread().enable_all().build().expect("failed to create agent runtime");
// after
let rt = tokio::runtime::Builder::new_current_thread()
    .enable_all()
    .build()
    .map_err(|e| {
        let _ = tx2.send(Err(format!("failed to create agent runtime: {e}")));
        e
    })?;
Defensive patterns

Strategy: retry

Validate before calling

fn can_spawn_runtime() -> bool {
    tokio::runtime::Builder::new_current_thread().enable_all().build().is_ok()
}

Try / catch

// inside spawn_blocking, map the error into the stream instead of panicking
let rt = match tokio::runtime::Builder::new_current_thread().enable_all().build() {
    Ok(rt) => rt,
    Err(e) => {
        let _ = tx2.send(Err(format!("failed to create agent runtime: {e}")));
        return;
    }
};

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of t8y2/dbx@c0390bff16 (2026-09-05). Data as JSON: /api/errors/8fc79e87ac2732bd. Report an issue: GitHub.