Skip to content

Free remaining llama.cpp objects at process exit before ggml-metal teardown (Flutter quit, hot restart) #96

Closed

Description

@leehack

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:

Constraints

  • The free must run before ggml-metal's static destructor. An atexit handler registered after the first backend init runs first, since handlers run in LIFO order.
  • When exit runs, a Dart worker thread may still be inside a llama.cpp call such as llama_decode or 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 NativeFinalizer callbacks if useful.

Possible shape (for discussion)

  • llama_dart_exit_track(void * object, void (*free_fn)(void *), int stage) and llama_dart_exit_untrack(void * object): a mutex-protected registry.
  • On its first use, the wrapper registers an atexit handler 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

  1. leehack commented on Oct 1, 2026

    @leehack
    OwnerAuthor

    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_model and 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 loadModel aborts with GGML_ASSERT even when the hold is the statement right after the FFI call. A probe confirms it: a worker that attaches a NativeFinalizer immediately after a 1 s usleep FFI 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.

  2. added
    bug
    priority:P1
    on Oct 5, 2026
  3. leehack commented on Oct 7, 2026

    @leehack
    OwnerAuthor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugpriority:P1

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions