|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * Minimal reproduction of llama.cpp issue ggml-org/llama.cpp#22593: |
| 4 | + * |
| 5 | + * ggml-metal-device.m:612: GGML_ASSERT([rsets->data count] == 0) failed |
| 6 | + * |
| 7 | + * Root cause (per the upstream issue and proposed fix PR #22595): |
| 8 | + * `ggml_metal_buffer_rset_free` releases the per-buffer residency set object |
| 9 | + * but does NOT call the symmetric `ggml_metal_device_rsets_rm`. So the |
| 10 | + * device's `rsets->data` array accumulates dangling references. When the |
| 11 | + * process exits and libc fires the process-static `ggml_metal_device` |
| 12 | + * destructor in `__cxa_finalize_ranges`, the destructor asserts the |
| 13 | + * array is empty — and it isn't. |
| 14 | + * |
| 15 | + * Observed downstream behavior: |
| 16 | + * - With EXPLICIT `dispose()` of every JS handle in order, the assertion |
| 17 | + * does NOT fire. node-llama-cpp's dispose path tears the Metal buffers |
| 18 | + * down before the static dtor runs, so the device's rsets array is |
| 19 | + * empty by exit time. (Tested locally — clean exit.) |
| 20 | + * - With NO dispose (the typical real-world case: synchronous `exit()`, |
| 21 | + * `--watch` mode, `process.exit()` after results are written, or any |
| 22 | + * code path where GC + finalizers race with libc exit), the rset |
| 23 | + * references linger until the static dtor fires, and the assertion |
| 24 | + * trips. |
| 25 | + * |
| 26 | + * What this script does: |
| 27 | + * 1. Load node-llama-cpp + a small GGUF model on the Metal backend. |
| 28 | + * This allocates at least one Metal buffer → calls rsets_add internally. |
| 29 | + * 2. Run an inference (creating an embedding context populates buffers |
| 30 | + * that the dispose path would normally clean up). |
| 31 | + * 3. Skip explicit dispose. Just let the process exit. |
| 32 | + * |
| 33 | + * Expected behavior on macOS 15+ with Apple Silicon, current llama.cpp |
| 34 | + * (bundled in node-llama-cpp 3.18.1, llama.cpp tag b8390): |
| 35 | + * - Without GGML_METAL_NO_RESIDENCY: |
| 36 | + * Script writes "ok" and main() returns, then ggml_abort fires the |
| 37 | + * assertion, prints a multi-kB backtrace, and the process exits with |
| 38 | + * SIGABRT (exit code 134). |
| 39 | + * - With GGML_METAL_NO_RESIDENCY=1: |
| 40 | + * Clean exit code 0. Residency-set code path is skipped entirely. |
| 41 | + * - With --dispose flag (manual cleanup): |
| 42 | + * Clean exit code 0 even without the env var, as long as JS dispose() |
| 43 | + * runs successfully before libc exit. |
| 44 | + * |
| 45 | + * Usage: |
| 46 | + * # Reproduce the crash (no dispose, no env var) |
| 47 | + * node scripts/repro-metal-rsets-crash.mjs |
| 48 | + * |
| 49 | + * # Verify the documented workaround |
| 50 | + * GGML_METAL_NO_RESIDENCY=1 node scripts/repro-metal-rsets-crash.mjs |
| 51 | + * |
| 52 | + * # Verify that explicit dispose also avoids the crash |
| 53 | + * node scripts/repro-metal-rsets-crash.mjs --dispose |
| 54 | + * |
| 55 | + * Refs: |
| 56 | + * https://github.com/ggml-org/llama.cpp/issues/22593 (root-cause analysis) |
| 57 | + * https://github.com/ggml-org/llama.cpp/pull/22595 (one-line fix, open) |
| 58 | + * https://github.com/tobi/qmd/issues/368 (downstream report) |
| 59 | + * https://github.com/tobi/qmd/issues/674 (downstream, current) |
| 60 | + * https://github.com/tobi/qmd/pull/600 (downstream workaround PR) |
| 61 | + */ |
| 62 | + |
| 63 | +import { existsSync } from "node:fs"; |
| 64 | +import { homedir } from "node:os"; |
| 65 | +import { resolve } from "node:path"; |
| 66 | + |
| 67 | +const DEFAULT_MODEL = resolve( |
| 68 | + homedir(), |
| 69 | + ".cache/qmd/models/hf_ggml-org_embeddinggemma-300M-Q8_0.gguf", |
| 70 | +); |
| 71 | + |
| 72 | +const args = process.argv.slice(2); |
| 73 | +const wantsDispose = args.includes("--dispose"); |
| 74 | +const modelPath = args.find((a) => !a.startsWith("--")) ?? DEFAULT_MODEL; |
| 75 | + |
| 76 | +if (!existsSync(modelPath)) { |
| 77 | + console.error(`Model not found: ${modelPath}`); |
| 78 | + console.error("Pass a path to any local GGUF as argv[1], or run `qmd embed` once to populate the default cache path."); |
| 79 | + process.exit(2); |
| 80 | +} |
| 81 | + |
| 82 | +console.error( |
| 83 | + `[repro] GGML_METAL_NO_RESIDENCY=${process.env.GGML_METAL_NO_RESIDENCY ?? "(unset)"}`, |
| 84 | +); |
| 85 | +console.error(`[repro] dispose=${wantsDispose}`); |
| 86 | +console.error(`[repro] loading: ${modelPath}`); |
| 87 | + |
| 88 | +const { getLlama } = await import("node-llama-cpp"); |
| 89 | + |
| 90 | +const llama = await getLlama(); |
| 91 | +const model = await llama.loadModel({ modelPath }); |
| 92 | +const context = await model.createEmbeddingContext(); |
| 93 | + |
| 94 | +console.error(`[repro] backend: ${llama.gpu}`); |
| 95 | + |
| 96 | +// Run actual inference so the buffer-allocation path is hit. |
| 97 | +await context.getEmbeddingFor("repro text"); |
| 98 | + |
| 99 | +if (wantsDispose) { |
| 100 | + console.error("[repro] explicit dispose…"); |
| 101 | + await context.dispose(); |
| 102 | + await model.dispose(); |
| 103 | + await llama.dispose(); |
| 104 | +} |
| 105 | + |
| 106 | +console.error("[repro] main() returning via process.exit(0)"); |
| 107 | +console.log("ok"); |
| 108 | + |
| 109 | +// CRITICAL: use process.exit(), not `return`. node-llama-cpp registers a |
| 110 | +// `process.once('beforeExit', …)` hook that auto-disposes WeakRef'd Llama |
| 111 | +// instances when the event loop empties naturally. `process.exit()` skips |
| 112 | +// `beforeExit`, so the rsets stay populated until libc's `exit()` fires the |
| 113 | +// static dtor — which is when the upstream assertion bug trips. |
| 114 | +// |
| 115 | +// CLI tools (qmd query, qmd vsearch, qmd embed, etc.) all call process.exit() |
| 116 | +// after writing results, which is why every real downstream report crashes |
| 117 | +// even though the minimal "let main return" version does not. |
| 118 | +process.exit(0); |
0 commit comments