Skip to content

A self-improving RLM agent for coding workflows and long-running autonomous tasks.

Rust

fix(coding-agent): force-kill unresponsive exec children (#458)

Sebastian Müller9cc944a8fe31
diff --git a/packages/coding-agent/CHANGELOG.md b/packages/coding-agent/CHANGELOG.md
index f5c41299de..2f75f212b8 100644
--- a/packages/coding-agent/CHANGELOG.md
+++ b/packages/coding-agent/CHANGELOG.md
@@ -2,6 +2,7 @@
## [Unreleased]
+- Fixed cancelled extension commands remaining alive when spawned processes ignored SIGTERM ([#458](https://github.com/PrimeIntellect-ai/prime-agent/pull/458) by [@snimu](https://github.com/snimu)).
- Fixed OAuth browser launch URLs being interpreted by the system shell.
- Changed long live session opens to render a bounded recent transcript tail while preserving full prompt history ([#343](https://github.com/PrimeIntellect-ai/prime-agent/pull/343) by [@sethkarten](https://github.com/sethkarten)).
diff --git a/packages/coding-agent/src/core/exec.ts b/packages/coding-agent/src/core/exec.ts
index a96ded7b20..bd62eb5a0e 100644
--- a/packages/coding-agent/src/core/exec.ts
+++ b/packages/coding-agent/src/core/exec.ts
@@ -71,14 +71,16 @@ export async function execCommand(
let stderr = "";
let killed = false;
let timeoutId: NodeJS.Timeout | undefined;
+ let forceKillTimeoutId: NodeJS.Timeout | undefined;
const killProcess = () => {
if (!killed) {
killed = true;
proc.kill("SIGTERM");
// Force kill after 5 seconds if SIGTERM doesn't work
- setTimeout(() => {
- if (!proc.killed) {
+ forceKillTimeoutId = setTimeout(() => {
+ forceKillTimeoutId = undefined;
+ if (proc.exitCode === null && proc.signalCode === null) {
proc.kill("SIGKILL");
}
}, 5000);
@@ -109,21 +111,23 @@ export async function execCommand(
stderr += data.toString();
});
+ const cleanup = () => {
+ if (timeoutId) clearTimeout(timeoutId);
+ if (forceKillTimeoutId) clearTimeout(forceKillTimeoutId);
+ if (options?.signal) {
+ options.signal.removeEventListener("abort", killProcess);
+ }
+ };
+
// Wait for process termination without hanging on inherited stdio handles
// held open by detached descendants.
waitForChildProcess(proc)
.then((code) => {
- if (timeoutId) clearTimeout(timeoutId);
- if (options?.signal) {
- options.signal.removeEventListener("abort", killProcess);
- }
+ cleanup();
resolve({ stdout, stderr, code: code ?? 0, killed });
})
.catch((_err) => {
- if (timeoutId) clearTimeout(timeoutId);
- if (options?.signal) {
- options.signal.removeEventListener("abort", killProcess);
- }
+ cleanup();
resolve({ stdout, stderr, code: 1, killed });
});
});
diff --git a/packages/coding-agent/test/exec.test.ts b/packages/coding-agent/test/exec.test.ts
new file mode 100644
index 0000000000..2f2ab6ea02
--- /dev/null
+++ b/packages/coding-agent/test/exec.test.ts
@@ -0,0 +1,52 @@
+import { existsSync, mkdtempSync, rmSync } from "node:fs";
+import { constants, tmpdir } from "node:os";
+import { join } from "node:path";
+import { describe, expect, it, vi } from "vitest";
+import { execCommand } from "../src/core/exec.js";
+
+const SIGKILL_EXIT_CODE = 128 + constants.signals.SIGKILL;
+
+async function waitForFile(path: string): Promise<void> {
+ const deadline = Date.now() + 10_000;
+ while (!existsSync(path)) {
+ if (Date.now() >= deadline) throw new Error("Child did not become ready");
+ await new Promise((resolve) => setTimeout(resolve, 10));
+ }
+}
+
+describe.skipIf(process.platform === "win32")("execCommand", () => {
+ it("force kills a process that ignores SIGTERM and cleans up the fallback timer", async () => {
+ const testDir = mkdtempSync(join(tmpdir(), "prime-agent-exec-test-"));
+ const readyFile = join(testDir, "ready");
+ const controller = new AbortController();
+ let resultPromise: Promise<Awaited<ReturnType<typeof execCommand>>> | undefined;
+ try {
+ resultPromise = execCommand(
+ process.execPath,
+ [
+ "-e",
+ `const { writeFileSync } = require("node:fs"); process.on("SIGTERM", () => {}); writeFileSync(process.argv[1], ""); setInterval(() => {}, 1000);`,
+ readyFile,
+ ],
+ process.cwd(),
+ { signal: controller.signal },
+ );
+ await waitForFile(readyFile);
+
+ vi.useFakeTimers();
+ controller.abort();
+
+ await vi.advanceTimersByTimeAsync(5000);
+ const result = await resultPromise;
+
+ expect(result.killed).toBe(true);
+ expect(result.code).toBe(SIGKILL_EXIT_CODE);
+ expect(vi.getTimerCount()).toBe(0);
+ } finally {
+ vi.useRealTimers();
+ controller.abort();
+ await resultPromise;
+ rmSync(testDir, { recursive: true, force: true });
+ }
+ });
+});