fix(llama-cpp): keep failed cleanup terminal

This commit is contained in:
Vincent Koc
2026-08-04 22:57:59 +08:00
parent 6788bf51da
commit 889cd316c7
2 changed files with 7 additions and 27 deletions
@@ -947,17 +947,15 @@ describe("llama.cpp inference provider", () => {
mocks.contextDispose.mockRejectedValueOnce(new Error("context cleanup failed"));
const firstDisposal = inferenceRuntime.dispose();
await expect(firstDisposal).rejects.toThrow(
"llama.cpp runtime cleanup failed: Error: context cleanup failed",
);
expect(mocks.modelDispose).toHaveBeenCalledOnce();
expect(mocks.llamaDispose).toHaveBeenCalledOnce();
await expect(firstDisposal).rejects.toThrow("context cleanup failed");
expect(mocks.modelDispose).not.toHaveBeenCalled();
expect(mocks.llamaDispose).not.toHaveBeenCalled();
const repeatedDisposal = inferenceRuntime.dispose();
expect(repeatedDisposal).toBe(firstDisposal);
await expect(repeatedDisposal).rejects.toThrow("context cleanup failed");
expect(mocks.contextDispose).toHaveBeenCalledOnce();
expect(mocks.modelDispose).toHaveBeenCalledOnce();
expect(mocks.llamaDispose).toHaveBeenCalledOnce();
expect(mocks.modelDispose).not.toHaveBeenCalled();
expect(mocks.llamaDispose).not.toHaveBeenCalled();
});
it.each([
+2 -20
View File
@@ -312,32 +312,14 @@ function disposeLlamaCppInferenceRuntime(state: LlamaCppInferenceRuntimeState):
// Plugin services stop once, and node-llama-cpp disposers mark themselves
// disposed before awaiting cleanup. The first disposal attempt is authoritative.
state.disposePromise = serialize(state, async () => {
const errors: unknown[] = [];
const attemptDispose = async (dispose: () => Promise<void>) => {
try {
await dispose();
} catch (error) {
errors.push(error);
}
};
if (state.loadedModel) {
const previous = state.loadedModel;
await attemptDispose(() => previous.context.dispose());
await attemptDispose(() => previous.model.dispose());
if (state.loadedModel === previous) {
state.loadedModel = undefined;
}
}
await disposeLoadedModel(state);
if (state.llamaInstance) {
const previous = state.llamaInstance;
await attemptDispose(() => previous.dispose());
await previous.dispose();
if (state.llamaInstance === previous) {
state.llamaInstance = undefined;
}
}
if (errors.length > 0) {
throw new AggregateError(errors, `llama.cpp runtime cleanup failed: ${String(errors[0])}`);
}
}).finally(() => {
state.lifecycle = "closed";
});