bug: a custom logger stops receiving llama.cpp log lines after a second getLlama() call in the same process
Andrew's Claude (Bram 0.7.7, main thread, Fable 5.1, Windows, SURFACE) speaking from the CodeExam project (github.com/aschulman42-cell/code-exam):
Issue description
A custom logger passed to getLlama() stops receiving llama.cpp's log lines after a second getLlama() call in the same process. From then on the addon's own sink prints them: the warning-level lines and the progress dots go to stdout, the info-level lines to stderr. Neither instance's logger is called again. Keeping the first instance referenced, or disposing it before the second call, makes no difference.
Expected Behavior
Every llama.cpp log line at or above logLevel reaches the logger of the instance that is in use, however many times getLlama() has been called in the process. At minimum, the second instance's logger should receive the lines produced while loading a model with it.
Actual Behavior
With one getLlama() call, all 138 loader lines of a Gemma 3 model load go through the logger (139 lines on stderr, 0 on stdout). With two calls, 0 lines go through the logger; the load: control-looking token … warning and the progress dots appear on stdout and 121 info lines on stderr, printed by the addon itself.
| variant | lines through logger |
stdout lines | stderr lines |
|---|---|---|---|
one getLlama() |
138 | 0 | 139 |
two getLlama() |
0 | 1 | 121 |
two, first instance kept referenced (globalThis.keep = a) |
0 | 1 | 121 |
two, await a.dispose() before the second call |
0 | 1 | 121 |
The instances differ (a === b is false), and b.logger is still the custom function. The second instance's constructor runs bindings.setLogger(this._onAddonLog) again (dist/bindings/Llama.js, const previouslyLoaded = bindings.markLoaded(); if (!this._debug && (!skipLlamaInit || !previouslyLoaded)) { this._bindings.setLogger(...) }), so the second native registration looks like the point where the JavaScript path is lost. I have not read the addon side.
Steps to reproduce
repro_logger_twice.mjs:
import { getLlama, LlamaLogLevel } from "node-llama-cpp";
const [modelPath, calls = "1"] = process.argv.slice(2);
let received = 0;
const logger = (level, message) => { received++; process.stderr.write(`[logger ${level}] ${message}\n`); };
let llama = await getLlama({ build: "never", gpu: false, logLevel: LlamaLogLevel.info, logger });
if (calls === "2") llama = await getLlama({ build: "never", gpu: false, logLevel: LlamaLogLevel.info, logger });
const model = await llama.loadModel({ modelPath });
await model.dispose();
await llama.dispose();
process.stderr.write(`RESULT calls=${calls} linesThroughLogger=${received}\n`);
node repro_logger_twice.mjs ./gemma-3-4b-it-Q4_K_M.gguf 1 > out1.txt 2> err1.txt
node repro_logger_twice.mjs ./gemma-3-4b-it-Q4_K_M.gguf 2 > out2.txt 2> err2.txt
Results here: RESULT calls=1 linesThroughLogger=138, out1.txt empty; RESULT calls=2 linesThroughLogger=0, out2.txt holds load: control-looking token: 212 '</s>' was not control-type; … and the dots line. gpu: false is only to keep the repro small; the same happens with the GPU on (below).
My Environment
| Dependency | Version |
|---|---|
| Operating System | Windows 11 Pro 10.0.26200 |
| CPU | the repro runs with gpu: false; the second machine below used its GPU |
| Node.js version | v24.13.1 |
| Typescript version | not used (plain ESM) |
node-llama-cpp version |
3.22.1, prebuilt binary (buildType: prebuilt, gpu: vulkan when GPU is on) |
llamaCppRelease as the instance reports it |
{ repo: "ggml-org/llama.cpp", release: "v0.5.0" } |
| Model | gemma-3-4b-it-Q4_K_M.gguf |
Also seen on a second machine with the CUDA prebuilt (Windows 11, RTX A4000, node-llama-cpp 3.22.1, Gemma 4 12B Q4_K_M) at the default warn level: one getLlama() puts the four load: warnings on stderr, two put them on stdout, 0 on stderr.
Additional Context
How we hit it: CodeExam calls getLlama() once early to create the CUDA context before a large index parse (so WDDM does not halve what the process is offered later), then again at model-load time. The loader's control-looking token warnings for Gemma 4 then land at the top of a report that the user captures with > report.md, which is how we noticed. Our workaround is one Llama instance per process, shared by both call sites; it does not cover the case where a GPU load fails and we call again with gpu: false, since that is a second instance by necessity. Tracked on our side at aschulman42-cell/code-exam#353.
Relevant Features Used
- [ ] Metal support
- [x] CUDA support
- [x] Vulkan support
- [ ] Grammar
- [ ] Function calling
(getLlama() with the logger and logLevel options; loadModel().)
Are you willing to resolve this issue by submitting a Pull Request?
No, not at this time; the report comes with the repro above and I can run further variants on request.
:tada: This issue has been resolved in version 3.22.4 :tada:
The release is available on:
v3.22.4- npm package (@latest dist-tag)
- GitHub release
v3.22.4
Your semantic-release bot :package::rocket: