Hmbown/CodeWhale · error

Thread already has an active turn

Error message

Thread already has an active turn

What it means

Before starting a turn, the runtime checks under the active-engines lock whether the thread's engine state already has an active_turn. Rejecting concurrent turns prevents interleaved turn state, goal mutations, and duplicated streaming output on one thread.

Solutions

  1. Wait for the current turn to complete (or call interrupt_turn with the active turn_id) before starting a new one.
  2. Track turn lifecycle events to disable the send path while a turn is active.
  3. If a stale turn is stuck, restart/reload the thread engine to clear active_turn.

Example fix

// before
start_turn(thread_id, req)?;
start_turn(thread_id, req2)?; // panics: active turn
// after
if !thread_has_active_turn(thread_id) {
    start_turn(thread_id, req)?;
}
Defensive patterns

Strategy: try-catch

Validate before calling

fn can_start_turn(active: &ActiveEngines, thread_id: &str) -> bool {
    active.engines.get(thread_id).map(|s| s.active_turn.is_none()).unwrap_or(false)
}

Try / catch

match submit_turn(thread_id, req).await {
    Err(e) if e.to_string().contains("active turn") => queue_for_retry(thread_id, req),
    other => other?,
}

Prevention

When it happens

Trigger: Calling the start-turn API twice for the same thread_id before the first turn finishes; firing a turn while a previous turn is still streaming; a stuck active_turn left by a crashed/interrupted turn that was never cleared.

Common situations: Double-clicking a send button; an event handler re-entering on streaming updates; a previous turn hung on a provider call so active_turn was never released.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of Hmbown/CodeWhale@73e0f67d83 (2026-09-22). Data as JSON: /api/errors/c4981557be20dfd9. Report an issue: GitHub.

Appendix: source

Thrown at crates/tui/src/runtime_threads.rs:9930

        }
        if !image_blocks.is_empty() || req.max_output_tokens.is_some() {
            let identity = self.provider_identity_for_thread(&cfg_snapshot, &thread)?;
            let route = resolve_runtime_thread_route_for_identity(&cfg_snapshot, &identity, Some(&requested_model))?;
            if !image_blocks.is_empty() && route.candidate.capabilities().image_input != codewhale_config::route::CapabilityState::Supported {
                bail!("image inputs require a model with explicitly supported image input");
            }
            if req.max_output_tokens.is_some() && !crate::route_budget::route_supports_output_token_limit(route.identity.provider, route.candidate.protocol()) {
                bail!("maxOutputTokens is unsupported by the selected provider transport");
            }
        }
        let engine = self.ensure_engine_loaded(&thread).await?;

        let client_preflight_required = {
            let active = self.active.lock().await;
            if let Some(active_thread) = active.engines.get(thread_id)
                && active_thread.active_turn.is_some()
            {
                bail!("Thread already has an active turn");
            }
            active
                .engines
                .get(thread_id)
                .is_none_or(|state| state.client_preflight_required)
        };

        // Resolve the concrete provider/model before persisting a turn. Auto
        // routing can fail, and such a failure must not leave a zombie
        // in-progress record behind.
        let identity = self.provider_identity_for_thread(&cfg_snapshot, &thread)?;
        let mut thread_config = cfg_snapshot.clone();
        thread_config.scope_to_provider_identity(&identity);
        let verbosity = thread_config.verbosity.clone();
        let (
            route,
            reasoning_effort,
            auto_controls_reasoning,

View on GitHub (pinned to 73e0f67d83)