Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -591,13 +591,19 @@ export const OpenCodeMemPlugin: Plugin = async (ctx: PluginInput) => {
if (memoryClient) await memoryClient.close();
};

const shutdownHandler = async () => {
const shutdownHandler = async (signal: NodeJS.Signals) => {
try {
await cleanupPlugin();
} catch (error) {
log("Shutdown error", { error: String(error) });
process.exitCode = 1;
}
// Registering listeners disables the runtime's default termination.
// After cleanup, remove ours and re-raise so the host can exit (#408).
// Avoid forced process exit here — that crashes onnxruntime-node finalizers (#114).
process.off("SIGINT", shutdownHandler);
process.off("SIGTERM", shutdownHandler);
process.kill(process.pid, signal);
};

const beforeExitHandler = () => {
Expand Down
10 changes: 10 additions & 0 deletions tests/plugin-shutdown.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ describe("plugin shutdown", () => {
expect(source).not.toContain("process.exit(");
});

it("re-raises SIGINT/SIGTERM after cleanup so the host can exit", () => {
const source = readFileSync(new URL("../src/index.ts", import.meta.url), "utf-8");

// Listeners suppress default termination; after cleanup we must drop them
// and re-deliver the signal (#408) without process.exit (#114).
expect(source).toContain('process.off("SIGINT", shutdownHandler)');
expect(source).toContain('process.off("SIGTERM", shutdownHandler)');
expect(source).toContain("process.kill(process.pid, signal)");
});

it("clears pending idle auto-capture work during cleanup", () => {
const source = readFileSync(new URL("../src/index.ts", import.meta.url), "utf-8");

Expand Down
Loading