Repository navigation
Description
Release triage — 2026-10-05
Priority: P1 · production macOS lifecycle correctness. Flutter quit can reach Metal's static destructor with live buffers and abort. The existing Dart finalizer repair covers orderly VM isolate shutdown, not AppKit's direct C exit or hot restart. The documented onExitRequested disposal workaround mitigates cooperative app quit but does not satisfy this issue's full lifecycle acceptance. No new regression range or data-loss claim is established here.
Release disposition: resolve the production quit path, or record an explicit owner-approved release limitation with the integration workaround and its qualified scope; do not close this issue based on plain Dart shutdown tests. Debug hot restart remains a separate acceptance case.
Owner and implementation group: native wrapper cleanup/coordination in this repo, then the matching tracking/free call sites in llamadart. Keep it separate from Apple resource packaging (#97) and S24 GPU investigations. No upstream-source patch is required merely by this triage.
Acceptance: normal model disposal, idle loaded-model quit, quit during load/decode, repeated creation/free, and hot restart then quit. Require bounded coordination, correct context/session/model free order, no free during an active native call, no double free, and no hanging exit. Compare production Flutter paths with the ordinary Dart finalizer control. This triage reviewed existing reproduction/source evidence; it did not rerun the crash.
Need
A way for llamadart to free every llama.cpp object still alive, contexts and sessions before models, during C exit() and before ggml-metal's static device destructor runs, when the process exits without shutting its Dart isolates down.
Why
On macOS Metal, exit → __cxa_finalize_ranges → ggml_metal_device_free → ggml_metal_rsets_free aborts with GGML_ASSERT([rsets->data count] == 0) while any Metal buffer is allocated. Upstream treats this as intended (ggml-org/llama.cpp#19137).
leehack/llamadart#823 fixes plain Dart programs (leehack/llamadart#613). There the VM shuts every isolate down before exit and runs each isolate's NativeFinalizers, which now free what the worker still holds. Two paths never shut the isolates down, so the Dart side cannot cover them:
- Flutter macOS quit (Cmd-Q, closing the last window,
ServicesBinding.exitApplication): AppKit calls Cexitdirectly. A probe showed that no root or workerNativeFinalizerruns, whileatexithandlers do. Apps must dispose fromAppLifecycleListener.onExitRequestedtoday. - Flutter hot restart (macOS Metal: quitting after a Flutter hot restart aborts on models the discarded isolate loaded llamadart#813, debug only): the old isolates disappear without running finalizers or onExit listeners, so their models leak and the next quit aborts.
Constraints
- The free must run before ggml-metal's static destructor. An
atexithandler registered after the first backend init runs first, since handlers run in LIFO order. - When
exitruns, a Dart worker thread may still be inside a llama.cpp call such asllama_decodeor a model load. The handler must not free an object under it: it can wait for the call with a bound, or skip that object. - Objects freed normally must be removed, so they are never freed twice.
- Callable from Dart FFI: plain C functions taking at most one pointer argument, so they also work as
NativeFinalizercallbacks if useful.
Possible shape (for discussion)
llama_dart_exit_track(void * object, void (*free_fn)(void *), int stage)andllama_dart_exit_untrack(void * object): a mutex-protected registry.- On its first use, the wrapper registers an
atexithandler that frees the tracked objects in stage order (sessions, contexts, mtmd, models). - A per-object or global "in call" guard that llamadart sets around long calls, so the handler can wait for them or skip.
The Dart side (IsolateShutdownReleases in leehack/llamadart#823) already hooks every creation and free site, so it could call these functions at the same sites.
Activity
leehack commentedon Oct 1, 2026
Another path the Dart side cannot cover, found in the independent audit of leehack/llamadart#823: a Dart program that dies of an error while a model or context is being created. The VM kill arrives during
llama_model_load_from_file/llama_init_from_modeland is handled at the first Dart interrupt check after the call returns. That check is the entry of the very next Dart call, including the one that would register the new object for shutdown, so the object is never tracked.Measured on an M4 Max with gemma-3-1b: throwing 350–500 ms after
loadModelaborts with GGML_ASSERT even when the hold is the statement right after the FFI call. A probe confirms it: a worker that attaches aNativeFinalizerimmediately after a 1 susleepFFI call never attaches it when a kill arrived during the call.So the tracking must happen inside native code, before the creating call returns. For example,
llama_dart_model_load_tracked, or a registry the wrapper fills from its own creation paths.The same applies to a kill that lands between llamadart releasing its hold and calling the free function during
dispose(). This is rare (about 1 in 8 when an error races a dispose) and leaks one object. A native registry that removes the object inside the free call would close it.fix: close the exit teardown races found in review
ci: run the exit teardown scenarios on macOS
ci: give the macOS exit teardown scenarios a longer timeout
test: pin the backend wait inside the call in flight
ci: check the static destructor binding and Metal use on Apple
docs: state the limits of exit teardown for Dart callers
test: do not expect system info where no backend is loaded
test: pin that teardown frees contexts and models without Metal
ci: say what the Metal control shows and refuse universal files
fix: free tracked llama.cpp objects at exit before ggml-metal teardown (
fix: free llama.cpp objects at Apple process exit with native v0.5.0-2 (
leehack commentedon Oct 7, 2026
Fixed by #98 and #100, released in v0.5.0-2, adopted in llamadart by #943. Remaining limit: a direct C
exit()during an unguarded llama.cpp call, or a guarded call longer than 2 s, is not covered.