Repository navigation
Fix ggml_metal crash on app exit (llama.cpp Metal cleanup) #1063
Copy link
Copy link
Closed
Labels
Description
Activity
Baseline: 128 test files passed, 3918 tests passed
- added 4 commits that reference this issue
on Apr 7, 2026 ✅ Issue #1063 resolved and merged to main via PR #1065
Summary
Fixed the ggml_metal crash on app exit by implementing proper cleanup ordering:
- Reset ManagedAgentState inference engine to NoOp (drops ChatEngine Arc references)
- Unload chat model via GgufModelManager
- Release embedding GPU resources
- Release global llama backend
Verification
- ✅ All requirements from Fix ggml_metal crash on app exit (llama.cpp Metal cleanup) #1063 implemented
- ✅ Code review findings addressed
- ✅ Tests passing: 128 files, 3918 tests
- ✅ Merged to main as commit 4a8f9fe
The fix ensures ChatEngine Arc is explicitly dropped before Metal backend teardown, preventing use-after-free crashes during shutdown.
- added 4 commits that reference this issue
on Apr 7, 2026 - added 3 commits that reference this issue
on Jun 22, 2026
Problem
The app crashes on exit with a
ggml_metal_device_init/ggml_metal_device_freebacktrace from llama.cpp's Metal backend cleanup. This happens consistently during app shutdown:The crash occurs in the C++ destructor chain during process exit (
__cxa_finalize_ranges), when llama.cpp's static Metal device vector is destroyed. The Metal context has already been torn down by the time the static destructor runs, causing a use-after-free abort.Impact
Likely Root Cause
llama.cpp stores Metal devices in a static
vector<unique_ptr<ggml_metal_device>>. During process exit, C++ static destructors run in reverse order. If the Metal framework has already been cleaned up by macOS before the static vector destructor runs,ggml_metal_device_freeaccesses freed Metal resources and aborts.This is a known pattern in llama.cpp: ggml-org/llama.cpp#17869
Proposed Solution
LlamaModel,LlamaContext, and related objects are dropped in our Rust shutdown sequence, before the process exits and C++ static destructors run