{"record":{"id":"9472aa9b0c8135c2","repo":"Hmbown/CodeWhale","slug":"runtime-engine-shutdown-failed-error","errorCode":null,"errorMessage":"Runtime engine shutdown failed: {error}","messagePattern":"Runtime engine shutdown failed: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/tui/src/runtime_threads.rs","lineNumber":5217,"sourceCode":"                failure = Some(anyhow!(\"Runtime turn interruption failed: {error}\"));\n            }\n        }\n        let workers = self.engine_workers.lock().clone();\n        for engine in engines\n            .iter()\n            .chain(workers.iter().map(|(engine, _)| engine))\n        {\n            engine.cancel_with_reason(crate::core::engine::CancelReason::Internal);\n        }\n        for engine in engines\n            .iter()\n            .chain(workers.iter().map(|(engine, _)| engine))\n        {\n            let _ = engine.send(Op::Shutdown).await;\n        }\n        for (_, worker) in workers {\n            if let Err(error) = await_retained_completion(&worker).await {\n                failure = Some(anyhow!(\"Runtime engine shutdown failed: {error}\"));\n            }\n        }\n        // Recovery reads can publish receipts too. Flushes that already passed\n        // their admission check retain this mutex through all receipt writes;\n        // later readers see the closed latch under the same mutex.\n        {\n            let _recovery = self.recovery_flush.lock().await;\n        }\n        loop {\n            let monitors = self.turn_monitors.lock().clone();\n            for monitor in monitors {\n                if let Err(error) = await_retained_completion(&monitor).await {\n                    failure = Some(anyhow!(\"Runtime terminal receipt monitor failed: {error}\"));\n                }\n            }\n            let mut monitors = self.turn_monitors.lock();\n            monitors.retain(|monitor| !completion_finished(monitor));\n            if monitors.is_empty() {","sourceCodeStart":5199,"sourceCodeEnd":5235,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/73e0f67d83c59909b571efdfc88c4bc28c309cb1/crates/tui/src/runtime_threads.rs#L5199-L5235","documentation":"During RuntimeThreadManager shutdown, after sending Op::Shutdown to every engine (main engines and workers), the manager awaits each retained worker completion via await_retained_completion. If any worker reports an error while finishing, the last error is recorded as 'Runtime engine shutdown failed'. This means an engine task failed to shut down cleanly, not that shutdown itself was refused.","triggerScenarios":"Calling shutdown on RuntimeThreadManager while engine workers exist; sending Op::Shutdown succeeds on the channel but the worker's retained completion future resolves with Err, e.g. the worker panicked or its join/completion returned an error.","commonSituations":"Stopping the TUI runtime with background engine workers still processing ops; a worker that crashed mid-turn; resource exhaustion (task abort) during process teardown.","solutions":["Read the wrapped {error} for the specific worker's shutdown failure","Ensure all in-flight turns complete or are interrupted before shutdown so workers exit normally","Check engine worker task logs for panics or aborts during teardown","Retain the error from shutdown and handle it at the caller instead of ignoring the Result"],"exampleFix":"// before\nmanager.shutdown().await.expect(\"shutdown\");\n// after\nif let Err(e) = manager.shutdown().await {\n    tracing::error!(\"engine workers failed to shut down: {e:#}\");\n}","handlingStrategy":"try-catch","validationCode":"let workers = manager.engine_workers.lock().clone();\nlet all_idle = workers.iter().all(|(_, w)| completion_finished(w));","typeGuard":"fn worker_is_idle(worker: &RuntimeCompletion) -> bool { completion_finished(worker) }","tryCatchPattern":"if let Err(e) = manager.shutdown().await {\n    if e.to_string().contains(\"engine shutdown failed\") {\n        tracing::error!(\"a worker failed to shut down cleanly: {e:#}\");\n    } else { return Err(e); }\n}","preventionTips":["Drain all in-flight ops/turns before shutdown","Avoid cancelling engine worker tasks mid-turn","Log worker completion errors at their source so the shutdown error is not the first signal"],"tags":["shutdown","engine","async"],"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"}