{"record":{"id":"19d6efa3a3b3ff17","repo":"Hmbown/CodeWhale","slug":"shell-manager-lock-poisoned","errorCode":null,"errorMessage":"shell manager lock poisoned","messagePattern":"shell manager lock poisoned","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"crates/tui/src/tools/shell.rs","lineNumber":4519,"sourceCode":"    command: &str,\n    heavy_permit: Option<HeavyCommandPermit>,\n    working_dir: Option<String>,\n    timeout_ms: Option<u64>,\n    stdin_data: Option<&str>,\n    tty: bool,\n    policy_override: Option<ExecutionSandboxPolicy>,\n    extra_env: HashMap<String, String>,\n    direct_argv: bool,\n    timeout_bounds_ms: (u64, u64),\n) -> Result<ShellResult> {\n    let timeout_ms =\n        timeout_ms.map(|timeout| timeout.clamp(timeout_bounds_ms.0, timeout_bounds_ms.1));\n    let spawn_timeout_ms = timeout_ms.unwrap_or(timeout_bounds_ms.1);\n    let spawned = {\n        let mut manager = context\n            .shell_manager\n            .lock()\n            .map_err(|_| anyhow!(\"shell manager lock poisoned\"))?;\n        manager.clear_foreground_background_request();\n        let owner = shell_job_owner_from_context(context);\n        let lifecycle = shell_work_lifecycle_from_context(context);\n        manager.execute_with_options_env_for_owner_and_work(\n            command,\n            working_dir.as_deref(),\n            spawn_timeout_ms,\n            true,\n            stdin_data,\n            tty,\n            policy_override,\n            extra_env,\n            owner,\n            context.state_namespace.clone(),\n            context.origin_tool_call_id.clone(),\n            context.origin_turn_id.clone(),\n            lifecycle,\n            direct_argv.then_some(context.workspace.as_path()),","sourceCodeStart":4501,"sourceCodeEnd":4537,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/73e0f67d83c59909b571efdfc88c4bc28c309cb1/crates/tui/src/tools/shell.rs#L4501-L4537","documentation":"Before spawning a foreground shell command the tool locks the shared ShellManager mutex; if another thread panicked while holding that lock, the mutex is poisoned and `.lock()` returns Err. The tool converts this into an anyhow error rather than running shell work against a manager in an inconsistent state. This is a symptom of an earlier panic in shell-management code, not a problem with the command being run.","triggerScenarios":"Calling the foreground shell execute path when any prior panic occurred while a thread held the shell_manager Mutex (e.g. a panic inside execute_with_options_env_for_owner_and_work, poll_status, kill, or output snapshotting).","commonSituations":"A background/async shell task panicked earlier in the session; a kill-during-timeout race hit an unexpected state and panicked; a bug in ShellManager bookkeeping (eviction while reading results) panicked under contention in long-running TUI sessions.","solutions":["Find and fix the originating panic — scan earlier logs for the panic that poisoned the mutex","Restart the session/process; a poisoned std::sync::Mutex stays poisoned until recreated","If recovery is required, switch the field to parking_lot::Mutex or handle PoisonError with into_inner() after auditing invariants","Reduce concurrent shell-manager access (cancel in-flight background shells before foreground runs) to shrink the race window"],"exampleFix":"// before\ncontext.shell_manager.lock().map_err(|_| anyhow!(\"shell manager lock poisoned\"))?\n// after (audited recovery)\ncontext.shell_manager.lock().unwrap_or_else(|p| p.into_inner())","handlingStrategy":"retry","validationCode":"// detect poisoned state before use\nfn manager_healthy(m: &Mutex<ShellManager>) -> bool { m.try_lock().is_ok() || poisoned_ok(m) }","typeGuard":null,"tryCatchPattern":"match context.shell_manager.lock() {\n    Ok(mut m) => { /* proceed */ }\n    Err(_) => return Err(anyhow!(\"shell manager unavailable (poisoned); restart session\")),\n}","preventionTips":["Never panic while holding the shell manager lock","Keep lock scopes minimal and free of unwraps","Use parking_lot::Mutex if lock poisoning recovery is desired","Monitor logs for the first panic — it precedes all poisoning errors"],"tags":["concurrency","mutex-poisoned","panic"],"backgroundTag":"internal-invariant-violation","analyzedSha":"73e0f67d83c59909b571efdfc88c4bc28c309cb1","analyzedAt":"2026-09-22T01:30:00.501Z","contentChangedAt":"2026-09-22T01:30:00.501Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}