Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

Stop login-shell PATH probe leaking stuck shells (PRD-9510) (#6609)

Andrew Mikofalvye78d0cf7f46f
diff --git a/.changeset/login-shell-probe-no-leak.md b/.changeset/login-shell-probe-no-leak.md
new file mode 100644
index 000000000..02405607d
--- /dev/null
+++ b/.changeset/login-shell-probe-no-leak.md
@@ -0,0 +1,5 @@
+---
+'@inkeep/open-knowledge': patch
+---
+
+OpenKnowledge no longer leaves stuck `zsh` processes behind when it checks your login shell's PATH.
diff --git a/packages/server/src/acp/launch.ts b/packages/server/src/acp/launch.ts
index 773030483..2050835f4 100644
--- a/packages/server/src/acp/launch.ts
+++ b/packages/server/src/acp/launch.ts
@@ -15,9 +15,8 @@ import { z } from 'zod';
import {
tracedMkdir,
tracedMkdirSync,
- tracedOpenSync,
+ tracedOpenUnlinkedSync,
tracedRm,
- tracedUnlinkSync,
tracedWriteFile,
tracedWriteFileSync,
} from '../fs-traced.ts';
@@ -541,7 +540,7 @@ async function runFileBackedInstall(
const stderrPath = join(acpNpxIsolatedCwd(), `.install-stderr-${process.pid}-${randomUUID()}`);
let fd: number;
try {
- fd = openUnlinkedFile(stderrPath);
+ fd = tracedOpenUnlinkedSync(stderrPath);
} catch (err) {
return {
code: null,
@@ -565,17 +564,6 @@ async function runFileBackedInstall(
}
}
-function openUnlinkedFile(path: string): number {
- const fd = tracedOpenSync(path, 'wx+', 0o600);
- try {
- tracedUnlinkSync(path);
- return fd;
- } catch (err) {
- closeSync(fd);
- throw err;
- }
-}
-
async function replayInstallStderr(fd: number, capture: DiagnosticStderrCapture): Promise<void> {
try {
for await (const chunk of createReadStream('', {
diff --git a/packages/server/src/acp/login-shell-path.test.ts b/packages/server/src/acp/login-shell-path.test.ts
index 19bc165a0..e5b0a218c 100644
--- a/packages/server/src/acp/login-shell-path.test.ts
+++ b/packages/server/src/acp/login-shell-path.test.ts
@@ -1,7 +1,10 @@
-import { mkdtempSync, rmSync, writeFileSync } from 'node:fs';
+import { type ChildProcess, spawn } from 'node:child_process';
+import { existsSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { delimiter, join } from 'node:path';
-import { afterEach, describe, expect, test } from 'vitest';
+import { setTimeout as delay } from 'node:timers/promises';
+import { fileURLToPath } from 'node:url';
+import { afterEach, describe, expect, test, vi } from 'vitest';
import { getLogger } from '../logger.ts';
import {
createLoginShellPathProvider,
@@ -19,7 +22,13 @@ function tmp(): string {
dirs.push(d);
return d;
}
-afterEach(() => {
+let spawners: Array<{ child: ChildProcess; closed: Promise<void> }> = [];
+afterEach(async () => {
+ for (const { child, closed } of spawners) {
+ if (child.exitCode === null && child.signalCode === null) child.kill('SIGKILL');
+ await closed;
+ }
+ spawners = [];
for (const d of dirs) rmSync(d, { recursive: true, force: true });
dirs = [];
});
@@ -33,6 +42,64 @@ function fakeShell(body: string): string {
const wrap = (path: string): string => `__OK_PATH_BEGIN__${path}__OK_PATH_END__`;
+const spawnerScript = `
+const { createLoginShellPathProvider } = await import(process.env.PROBE_MODULE);
+createLoginShellPathProvider({
+ log: { debug() {} },
+ platform: 'darwin',
+ shell: process.env.PROBE_SHELL,
+ timeoutMs: 60_000,
+})();
+process.stdout.write('spawned');
+if (process.env.EXIT_AFTER_MS) setTimeout(() => process.exit(0), Number(process.env.EXIT_AFTER_MS));
+`;
+
+async function startProbeSpawner(
+ shell: string,
+ exitAfterMs?: number,
+): Promise<{ child: ChildProcess; closed: Promise<void> }> {
+ const child = spawn(
+ process.execPath,
+ ['--import', 'tsx', '--input-type=module', '-e', spawnerScript],
+ {
+ cwd: fileURLToPath(new URL('../..', import.meta.url)),
+ stdio: ['ignore', 'pipe', 'pipe'],
+ env: {
+ ...process.env,
+ PROBE_MODULE: new URL('./login-shell-path.ts', import.meta.url).href,
+ PROBE_SHELL: shell,
+ EXIT_AFTER_MS: exitAfterMs === undefined ? '' : String(exitAfterMs),
+ },
+ },
+ );
+ const closed = new Promise<void>((resolveClosed) => {
+ child.once('close', () => resolveClosed());
+ });
+ spawners.push({ child, closed });
+ let stderr = '';
+ child.stderr?.on('data', (chunk: Buffer) => {
+ stderr += chunk.toString();
+ });
+ await new Promise<void>((resolveSpawned, rejectSpawned) => {
+ child.stdout?.on('data', () => resolveSpawned());
+ void closed.then(() => rejectSpawned(new Error(`probe spawner exited early: ${stderr}`)));
+ });
+ return { child, closed };
+}
+
+function forkingStallingShell(heartbeat: string): string {
+ return fakeShell(
+ `( i=0; while [ $i -lt 300 ]; do touch '${heartbeat}'; sleep 0.1; i=$((i + 1)); done ) &\nsleep 30`,
+ );
+}
+
+async function expectHeartbeatStopped(heartbeat: string): Promise<void> {
+ expect(existsSync(heartbeat)).toBe(true);
+ rmSync(heartbeat);
+ await delay(1_000);
+ expect(existsSync(heartbeat)).toBe(false);
+}
+
describe('parseLoginShellPath', () => {
test('extracts the fenced PATH', () => {
expect(parseLoginShellPath(wrap('/a:/b'))).toBe('/a:/b');
@@ -313,6 +380,45 @@ describe.skipIf(process.platform === 'win32')(
expect(Date.now() - started).toBeLessThan(5_000);
});
+ test('a timed-out probe kills the subshells its shell forked', async () => {
+ const heartbeat = join(tmp(), 'heartbeat');
+ const provider = createLoginShellPathProvider({
+ log,
+ platform: 'darwin',
+ shell: forkingStallingShell(heartbeat),
+ timeoutMs: 2_000,
+ });
+ await expect(provider()).resolves.toBeNull();
+ await expectHeartbeatStopped(heartbeat);
+ }, 15_000);
+
+ test('a spawner that exits mid-probe takes the whole probe tree with it', async () => {
+ const heartbeat = join(tmp(), 'heartbeat');
+ const { closed } = await startProbeSpawner(forkingStallingShell(heartbeat), 2_000);
+ await closed;
+ await expectHeartbeatStopped(heartbeat);
+ }, 15_000);
+
+ test('a probe outliving a killed spawner never writes into a dead pipe', async () => {
+ const dir = tmp();
+ const shell = fakeShell(
+ [
+ `trap "touch '${join(dir, 'sigpipe')}'" PIPE`,
+ 'sleep 1',
+ 'echo "Last login: today"',
+ `touch '${join(dir, 'finished')}'`,
+ ].join('\n'),
+ );
+ const { child, closed } = await startProbeSpawner(shell);
+ child.kill('SIGKILL');
+ await closed;
+ await vi.waitFor(() => expect(existsSync(join(dir, 'finished'))).toBe(true), {
+ timeout: 10_000,
+ interval: 50,
+ });
+ expect(existsSync(join(dir, 'sigpipe'))).toBe(false);
+ }, 15_000);
+
test('a shell that cannot be executed yields no verdict', async () => {
const provider = createLoginShellPathProvider({
log,
diff --git a/packages/server/src/acp/login-shell-path.ts b/packages/server/src/acp/login-shell-path.ts
index 17a808bf0..2f530e4d9 100644
--- a/packages/server/src/acp/login-shell-path.ts
+++ b/packages/server/src/acp/login-shell-path.ts
@@ -1,5 +1,9 @@
-import { spawn } from 'node:child_process';
-import { delimiter } from 'node:path';
+import { type ChildProcess, spawn } from 'node:child_process';
+import { randomUUID } from 'node:crypto';
+import { closeSync, fstatSync, readSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { delimiter, join } from 'node:path';
+import { tracedOpenUnlinkedSync } from '../fs-traced.ts';
import type { PinoLogger } from '../logger.ts';
const LOGIN_SHELL_PROBE_TIMEOUT_MS = 5_000;
@@ -71,38 +75,91 @@ export interface LoginShellPathDeps {
now?: () => number;
}
+const liveProbes = new Set<ChildProcess>();
+let killLiveProbesOnExit = false;
+
+function hasExited(child: ChildProcess): boolean {
+ return child.exitCode !== null || child.signalCode !== null;
+}
+
+function killProbeTree(child: ChildProcess): void {
+ const pid = child.pid;
+ if (pid === undefined || hasExited(child)) return;
+ if (process.platform !== 'win32') {
+ try {
+ process.kill(-pid, 'SIGKILL');
+ return;
+ } catch {}
+ }
+ child.kill('SIGKILL');
+}
+
+function killLiveProbes(): void {
+ for (const child of liveProbes) killProbeTree(child);
+}
+
+function trackLiveProbe(child: ChildProcess): void {
+ liveProbes.add(child);
+ const forget = (): void => {
+ liveProbes.delete(child);
+ };
+ child.once('exit', forget);
+ child.once('error', forget);
+ if (killLiveProbesOnExit) return;
+ killLiveProbesOnExit = true;
+ process.on('exit', killLiveProbes);
+}
+
+function readProbeOutput(fd: number): string | null {
+ try {
+ const size = Math.min(fstatSync(fd).size, MAX_PROBE_STDOUT_BYTES);
+ const buffer = Buffer.alloc(size);
+ const read = readSync(fd, buffer, 0, size, 0);
+ return buffer.toString('utf8', 0, read);
+ } catch {
+ return null;
+ }
+}
+
const spawnLoginShellProbe: RunLoginShellProbe = (shell, args, timeoutMs) =>
new Promise((resolvePromise) => {
- let child: ReturnType<typeof spawn>;
+ let output: number;
+ try {
+ output = tracedOpenUnlinkedSync(join(tmpdir(), `ok-login-shell-path-${randomUUID()}`));
+ } catch {
+ resolvePromise(null);
+ return;
+ }
+ let child: ChildProcess;
try {
child = spawn(shell, [...args], {
- stdio: ['ignore', 'pipe', 'ignore'],
+ stdio: ['ignore', output, 'ignore'],
+ detached: process.platform !== 'win32',
windowsHide: true,
});
} catch {
+ closeSync(output);
resolvePromise(null);
return;
}
- let out = '';
+ trackLiveProbe(child);
let settled = false;
- const finish = (value: string | null): void => {
+ const finish = (result: () => string | null): void => {
if (settled) return;
settled = true;
clearTimeout(timer);
- resolvePromise(value);
+ resolvePromise(result());
+ try {
+ closeSync(output);
+ } catch {}
};
const timer = setTimeout(() => {
- child.kill('SIGKILL');
- finish(null);
+ killProbeTree(child);
+ finish(() => null);
}, timeoutMs);
timer.unref?.();
- child.stdout?.setEncoding('utf8');
- child.stdout?.on('data', (chunk: string) => {
- if (out.length >= MAX_PROBE_STDOUT_BYTES) return;
- out += chunk;
- });
- child.on('error', () => finish(null));
- child.on('close', () => finish(out));
+ child.on('error', () => finish(() => null));
+ child.on('close', () => finish(() => readProbeOutput(output)));
});
const FAILED_PROBE_RETRY_MS = 60_000;
diff --git a/packages/server/src/fs-traced.ts b/packages/server/src/fs-traced.ts
index af58f2fc0..592a9494d 100644
--- a/packages/server/src/fs-traced.ts
+++ b/packages/server/src/fs-traced.ts
@@ -2,6 +2,7 @@ import type { RmOptions, WriteFileOptions } from 'node:fs';
import {
appendFileSync,
chmodSync,
+ closeSync,
cpSync,
linkSync,
mkdirSync,
@@ -178,7 +179,7 @@ export function tracedCpSync(
);
}
-export function tracedOpenSync(path: string, flags: string, mode: number): number {
+function tracedOpenSync(path: string, flags: string, mode: number): number {
return withSpanSync('fs.openSync', { attributes: buildAttrs('openSync', path) }, () =>
openSync(path, flags, mode),
);
@@ -190,6 +191,17 @@ export function tracedUnlinkSync(path: string): void {
});
}
+export function tracedOpenUnlinkedSync(path: string): number {
+ const fd = tracedOpenSync(path, 'wx+', 0o600);
+ try {
+ tracedUnlinkSync(path);
+ return fd;
+ } catch (err) {
+ closeSync(fd);
+ throw err;
+ }
+}
+
export function tracedLinkSync(existingPath: string, newPath: string): void {
withSpanSync(
'fs.linkSync',