Repository navigation
Fix: inference engine teardown Tokio runtime panic on shutdown/hot-reload - #1098
Conversation
…load (closes #1082) Root cause: reset_engine_sync() called tokio::runtime::Builder::new_current_thread() from within the Tauri run-event handler, which already runs inside the Tokio runtime. Tokio panics with "Cannot start a runtime from within a runtime" when attempting to create a nested runtime. The GGML_ASSERT failure was a Metal GPU residency leak from llama.cpp caused by Tokio panic aborting cleanup before GPU resources released. Fix: - Delete reset_engine_sync() which used the broken nested-runtime pattern - Store ManagedAgentState as Arc<ManagedAgentState> in Tauri managed state so it can be cloned and sent across thread boundaries during shutdown - Update release_gpu_resources() (step 1) to use the same std::thread::spawn + block_on pattern already used for steps 2a and 2b (chat model unload and embedding GPU release): spawn a dedicated OS thread with no existing runtime context, then call block_on safely from that thread - Update all Tauri command signatures and chat_models.rs to use Arc<ManagedAgentState> Tests added: - test_reset_to_noop_engine_from_async_context: verifies calling reset_to_noop_engine from within a #[tokio::test] async context does not panic (regresses the bug) - test_reset_to_noop_engine_from_dedicated_thread: verifies the thread-spawn pattern used in release_gpu_resources works correctly Co-Authored-By: Claude <[email protected]>
Code Review — Initial ReviewReview Type: Initial Review Requirements CheckFrom issue #1082:
Code Review Findings🟢 Suggestion: Test diverges from production pattern📁
This is an acceptable trade-off since 🟢 Suggestion: Minor style inconsistency in
|
Co-Authored-By: Claude <[email protected]>
…load (#1098) * Fix: inference engine teardown Tokio runtime panic on shutdown/hot-reload (closes #1082) Root cause: reset_engine_sync() called tokio::runtime::Builder::new_current_thread() from within the Tauri run-event handler, which already runs inside the Tokio runtime. Tokio panics with "Cannot start a runtime from within a runtime" when attempting to create a nested runtime. The GGML_ASSERT failure was a Metal GPU residency leak from llama.cpp caused by Tokio panic aborting cleanup before GPU resources released. Fix: - Delete reset_engine_sync() which used the broken nested-runtime pattern - Store ManagedAgentState as Arc<ManagedAgentState> in Tauri managed state so it can be cloned and sent across thread boundaries during shutdown - Update release_gpu_resources() (step 1) to use the same std::thread::spawn + block_on pattern already used for steps 2a and 2b (chat model unload and embedding GPU release): spawn a dedicated OS thread with no existing runtime context, then call block_on safely from that thread - Update all Tauri command signatures and chat_models.rs to use Arc<ManagedAgentState> Tests added: - test_reset_to_noop_engine_from_async_context: verifies calling reset_to_noop_engine from within a #[tokio::test] async context does not panic (regresses the bug) - test_reset_to_noop_engine_from_dedicated_thread: verifies the thread-spawn pattern used in release_gpu_resources works correctly Co-Authored-By: Claude <[email protected]> * Fix: use imported Arc alias in chat_models.rs (style consistency) Co-Authored-By: Claude <[email protected]> --------- Co-authored-by: Claude <[email protected]>
Summary
Fixes the
Cannot start a runtime from within a runtimepanic that occurred on app close and hot-reload during inference engine teardown (closes #1082).Root cause:
reset_engine_sync()calledtokio::runtime::Builder::new_current_thread()from within the Tauri run-event handler, which already runs inside the Tokio runtime. Tokio panics when attempting to create a nested runtime. The secondaryGGML_ASSERT([rsets->data count] == 0)failure was a Metal GPU residency leak from llama.cpp caused by the Tokio panic aborting cleanup before GPU resources were released.Changes
reset_engine_sync()— the broken method that created a nested Tokio runtimeManagedAgentStateasArc<ManagedAgentState>in Tauri managed state so it can be cloned and sent across thread boundaries during shutdownrelease_gpu_resources()step 1 to use the samestd::thread::spawn+block_onpattern already used for steps 2a (chat model unload) and 2b (embedding GPU release): spawn a dedicated OS thread with no existing runtime context, then callblock_onsafely from that threadchat_models.rsto useArc<ManagedAgentState>Acceptance Criteria
GGML_ASSERTfailures in llama.cpp during shutdownTests
Two regression tests added:
test_reset_to_noop_engine_from_async_context: Verifiesreset_to_noop_engine()can be called from within a Tokio async context (simulating the bug scenario) without panickingtest_reset_to_noop_engine_from_dedicated_thread: Verifies the thread-spawn pattern used inrelease_gpu_resourcesworks correctlyTest Results
🤖 Generated with Claude Code