Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

fix(tools): prevent AbortSignal listener leak in exec and process tools

yushenc24fafadeb02
diff --git a/src/agent/tools/exec.ts b/src/agent/tools/exec.ts
index 826795f63f..41b51550a5 100644
--- a/src/agent/tools/exec.ts
+++ b/src/agent/tools/exec.ts
@@ -164,9 +164,22 @@ export function createExecTool(
// Don't reject, let close event handle
});
+ // Signal handling: don't kill if already backgrounded
+ const onAbort = signal ? () => {
+ if (yielded) return; // Already backgrounded, ignore abort
+ if (timeout) clearTimeout(timeout);
+ if (yieldTimer) clearTimeout(yieldTimer);
+ child.kill("SIGTERM");
+ } : undefined;
+
+ if (signal && onAbort) {
+ signal.addEventListener("abort", onAbort, { once: true });
+ }
+
child.on("close", (code) => {
if (timeout) clearTimeout(timeout);
if (yieldTimer) clearTimeout(yieldTimer);
+ if (signal && onAbort) signal.removeEventListener("abort", onAbort);
// If already backgrounded, don't resolve again
if (yielded) return;
@@ -202,16 +215,6 @@ export function createExecTool(
},
});
});
-
- // Signal handling: don't kill if already backgrounded
- if (signal) {
- signal.addEventListener("abort", () => {
- if (yielded) return; // Already backgrounded, ignore abort
- if (timeout) clearTimeout(timeout);
- if (yieldTimer) clearTimeout(yieldTimer);
- child.kill("SIGTERM");
- });
- }
});
},
};
diff --git a/src/agent/tools/process.ts b/src/agent/tools/process.ts
index 962b6a1384..ad0fecb070 100644
--- a/src/agent/tools/process.ts
+++ b/src/agent/tools/process.ts
@@ -112,7 +112,7 @@ export function createProcessTool(defaultCwd?: string): AgentTool<typeof Process
if (signal) {
signal.addEventListener("abort", () => {
child.kill("SIGTERM");
- });
+ }, { once: true });
}
resolve({ success: true });