Skip to content

Commit b3b33d2

Browse files
chelsealongbek91
andauthored
fix(agents): treat native hook relay records with a dead pid as absent (#128112)
* fix(agents): treat native hook relay records with a dead pid as absent readNativeHookRelayClientBridgeRecord's expiry check never consulted the recorded pid, so a bridge whose owning process died kept resolving as a live locator for its full TTL. Lookups then spent their deadline retrying a connection to an unreachable loopback port instead of falling back. Extract the ESRCH liveness check already used by registration-time pruning into a shared helper and consult it in the cold client lookup path before attempting a connection. * refactor(agents): reuse PID liveness helper --------- Co-authored-by: Bek <[email protected]>
1 parent 42d87c6 commit b3b33d2

3 files changed

Lines changed: 40 additions & 10 deletions

File tree

‎src/agents/harness/native-hook-relay-bridge.ts‎

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { randomUUID } from "node:crypto";
22
import { createServer, type IncomingMessage, type ServerResponse } from "node:http";
33
import { createSubsystemLogger } from "../../logging/subsystem.js";
4+
import { isPidDefinitelyDead } from "../../shared/pid-alive.js";
45
import {
56
isNativeHookRelayBridgeStaleRegistrationError,
67
NATIVE_HOOK_RELAY_BRIDGE_STALE_REGISTRATION_ERROR,
@@ -52,15 +53,6 @@ type NativeHookRelayBridgeRequestAuth = {
5253
invokeRelay: InvokeNativeHookRelay;
5354
};
5455

55-
function isNativeHookRelayBridgePidDead(pid: number): boolean {
56-
try {
57-
process.kill(pid, 0);
58-
return false;
59-
} catch (error) {
60-
return typeof error === "object" && error !== null && "code" in error && error.code === "ESRCH";
61-
}
62-
}
63-
6456
export function registerNativeHookRelayBridge(
6557
registration: ActiveNativeHookRelayRegistration,
6658
stateDbPath: string,
@@ -71,7 +63,7 @@ export function registerNativeHookRelayBridge(
7163
try {
7264
const pruned = pruneNativeHookRelayBridgeRecords({
7365
currentPid: process.pid,
74-
isPidDead: isNativeHookRelayBridgePidDead,
66+
isPidDead: isPidDefinitelyDead,
7567
stateDbPath,
7668
});
7769
for (const row of pruned) {

‎src/agents/harness/native-hook-relay-client.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { request as httpRequest } from "node:http";
22
import { toErrorObject } from "../../infra/errors.js";
3+
import { isPidDefinitelyDead } from "../../shared/pid-alive.js";
34
import { readNativeHookRelayClientBridgeRecord } from "./native-hook-relay-client-store.js";
45
import { DEFAULT_RELAY_TIMEOUT_MS } from "./native-hook-relay-constants.js";
56
import { codexNativeHookRelayResponseCodec } from "./native-hook-relay-response-codec.js";
@@ -41,6 +42,11 @@ export async function invokeNativeHookRelayBridge(
4142
if (!record) {
4243
throw new Error("native hook relay bridge not found");
4344
}
45+
// A dead owning process leaves an unreachable loopback port on record;
46+
// treat it as absent instead of spending the deadline on a dead connect.
47+
if (isPidDefinitelyDead(record.pid)) {
48+
throw new Error("native hook relay bridge not found");
49+
}
4450
if (Date.now() > record.expiresAtMs) {
4551
throw new Error("native hook relay bridge expired");
4652
}

‎src/agents/harness/native-hook-relay.test.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1944,6 +1944,38 @@ describe("native hook relay registry", () => {
19441944
expect(testing.getNativeHookRelayBridgeRecordForTests(liveRelayId)).toBeDefined();
19451945
});
19461946

1947+
it("treats direct bridge records with a dead owning pid as absent", async () => {
1948+
const relayId = await writeForeignNativeHookRelayBridgeRecordForTests(
1949+
uniqueNativeHookRelayIdForTests("codex-dead-pid-bridge"),
1950+
{
1951+
pid: 9_999_996,
1952+
expiresAtMs: Date.now() + 60_000,
1953+
},
1954+
);
1955+
const kill = vi.spyOn(process, "kill").mockImplementation((pid) => {
1956+
if (pid === 9_999_996) {
1957+
throw Object.assign(new Error("missing process"), { code: "ESRCH" });
1958+
}
1959+
return true;
1960+
});
1961+
1962+
await expect(
1963+
invokeNativeHookRelayBridge({
1964+
provider: "codex",
1965+
relayId,
1966+
event: "pre_tool_use",
1967+
registrationTimeoutMs: 1,
1968+
timeoutMs: 50,
1969+
rawPayload: {
1970+
hook_event_name: "PreToolUse",
1971+
tool_name: "Bash",
1972+
tool_input: { command: "pnpm test" },
1973+
},
1974+
}),
1975+
).rejects.toThrow("native hook relay bridge not found");
1976+
expect(kill).toHaveBeenCalledWith(9_999_996, 0);
1977+
});
1978+
19471979
it("accepts only loopback direct bridge records", async () => {
19481980
const relay = registerNativeHookRelay({
19491981
provider: "codex",

0 commit comments

Comments
 (0)