Skip to content

Linger is An Open-Source Personal Agent That Runs Fully on Your PC. We aim to break Big Tech’s grip on AI by bringing powerful local AI to everyday computers through efficient model–harness co-design, so everyone can run their own.

fix(local-installation): contain process output callback failures

Afloatfb2d055dd20e
diff --git a/packages/local-installation/AGENTS.md b/packages/local-installation/AGENTS.md
index 58ba75be..a81b9293 100644
--- a/packages/local-installation/AGENTS.md
+++ b/packages/local-installation/AGENTS.md
@@ -155,6 +155,8 @@ lock never frees) is logged and returned as `details.byokGateway`
`runProcess` decodes stdout and stderr as separate UTF-8 streams before
capture and output callbacks, preserving characters split across pipe reads.
+An output callback failure stops the owned command and rejects its promise
+after the process closes; later output is not delivered to a failed callback.
Each native service runs under a detached supervisor
(`native-process-worker.mjs`) whose record holds its PID, birth and an
diff --git a/packages/local-installation/src/process-output-failure.test.ts b/packages/local-installation/src/process-output-failure.test.ts
new file mode 100644
index 00000000..ed3ff796
--- /dev/null
+++ b/packages/local-installation/src/process-output-failure.test.ts
@@ -0,0 +1,51 @@
+import { spawnSync } from "node:child_process";
+import { mkdtemp, readFile, rm } from "node:fs/promises";
+import { tmpdir } from "node:os";
+import path from "node:path";
+import { expect, it } from "vitest";
+
+it.each(["stdout", "stderr"])(
+ "rejects an output sink failure on %s without crashing or leaving the command running",
+ async (stream) => {
+ const root = await mkdtemp(path.join(tmpdir(), "linger-output-sink-"));
+ const pidFile = path.join(root, "child.pid");
+ try {
+ const childSource = `
+ require('node:fs').writeFileSync(process.argv[1], String(process.pid));
+ process[process.argv[2]].write('diagnostic');
+ setTimeout(() => process.exit(0), 2000);
+ `;
+ const harness = `
+ import { runProcess } from ${JSON.stringify(new URL("./process.ts", import.meta.url).href)};
+ import { writeFileSync } from 'node:fs';
+ const result = await runProcess(process.execPath, ['-e', ${JSON.stringify(childSource)}, ${JSON.stringify(pidFile)}, ${JSON.stringify(stream)}], {
+ onOutput() { writeFileSync(${JSON.stringify(root)}, 'diagnostic'); }
+ }).then(() => ({resolved:true}), error => ({code:error.code, message:error.message}));
+ process.stdout.write(JSON.stringify(result));
+ `;
+ const result = spawnSync(
+ process.execPath,
+ ["--input-type=module", "-e", harness],
+ {
+ encoding: "utf8",
+ timeout: 5000,
+ },
+ );
+ expect(result.status, result.stderr).toBe(0);
+ expect(JSON.parse(result.stdout)).toMatchObject({ code: "EISDIR" });
+ expect(result.stderr).toBe("");
+ const pid = Number(await readFile(pidFile, "utf8"));
+ expect(() => process.kill(pid, 0)).toThrow();
+ } finally {
+ const pid = Number(await readFile(pidFile, "utf8").catch(() => "0"));
+ if (pid > 0) {
+ try {
+ process.kill(pid, "SIGKILL");
+ } catch {
+ /* Already gone. */
+ }
+ }
+ await rm(root, { recursive: true, force: true });
+ }
+ },
+);
diff --git a/packages/local-installation/src/process.ts b/packages/local-installation/src/process.ts
index 09901325..47b4e6f9 100644
--- a/packages/local-installation/src/process.ts
+++ b/packages/local-installation/src/process.ts
@@ -2,6 +2,7 @@ import type { Readable } from "node:stream";
import { pipeline } from "node:stream/promises";
import path from "node:path";
import { spawn } from "node:child_process";
+import { asError } from "@linger/service-kit";
export function windowsTreeTermination(
child: {
pid?: number;
@@ -119,6 +120,7 @@ export async function runProcess(
)
: undefined;
const output = (value: string, stream: "stdout" | "stderr") => {
+ if (failure) return;
if (
options.capture !== false &&
stdout.length + stderr.length + value.length > 2_000_000
@@ -130,7 +132,11 @@ export async function runProcess(
if (stream === "stdout") stdout += value;
else stderr += value;
}
- options.onOutput?.(value);
+ try {
+ options.onOutput?.(value);
+ } catch (error) {
+ stop(asError(error));
+ }
};
child
.stdout!.setEncoding("utf8")