From 2af38952da4f7e4bafcb2bd30e8aef689539d324 Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Wed, 18 Nov 2026 10:38:34 -0800 Subject: [PATCH] fix(linux): fail closed when the fd walk errors; detect a reused desktop pid; lock slack on Linux only --- config/recurring-timer-baseline.txt | 2 +- native/provider-exec/provider-exec.c | 12 ++++++-- .../backend/backend-desktop-owner-watch.ts | 29 +++++++++++-------- src/main/backend/backend-entry.ts | 3 +- 4 files changed, 29 insertions(+), 17 deletions(-) diff --git a/config/recurring-timer-baseline.txt b/config/recurring-timer-baseline.txt index 1d9ee1535c1..f5e36aa43dd 100644 --- a/config/recurring-timer-baseline.txt +++ b/config/recurring-timer-baseline.txt @@ -9,7 +9,7 @@ 1 src/main/agent-hooks/server/server-opencode-binder.ts 1 src/main/automations/observation/retained-run-reconciliation.ts 1 src/main/automations/automation-service.ts -1 src/main/backend/backend-desktop-owner-watch.ts # why: Linux backend polls main's pid every 2 s and stops once main is gone; no exit push signal exists +1 src/main/backend/backend-desktop-owner-watch.ts # why: Linux backend polls main's pid every 2 s and stops once main is gone; Node exposes no exit event for a non-child pid 1 src/main/bots/bot-access-request-watch.ts 1 src/main/browser/extensions/native-messaging/browser-extension-native-ports.ts 1 src/main/browser/extensions/web-store/browser-extension-webstore-updater.ts diff --git a/native/provider-exec/provider-exec.c b/native/provider-exec/provider-exec.c index f5f147fe729..28e92826171 100644 --- a/native/provider-exec/provider-exec.c +++ b/native/provider-exec/provider-exec.c @@ -128,10 +128,12 @@ static int parse_ms(const char *text, unsigned max, unsigned *out) { if (*c < '0' || *c > '9') { return -1; } - value = value * 10 + (unsigned)(*c - '0'); - if (value > max) { + unsigned digit = (unsigned)(*c - '0'); + // Checked before multiplying so a long input can't wrap back under `max`. + if (value > (max - digit) / 10) { return -1; } + value = value * 10 + digit; } *out = value; return 0; @@ -167,13 +169,17 @@ static int cloexec_inherited_fds(void) { } int failed = 0; struct dirent *entry; - while ((entry = readdir(dir)) != NULL) { + // readdir returns NULL for both the end and an error; only errno tells them apart. + for (errno = 0; (entry = readdir(dir)) != NULL; errno = 0) { unsigned fd; if (parse_ms(entry->d_name, INT_MAX, &fd) == 0 && fd > 2 && (int)fd != dirfd(dir) && set_cloexec((int)fd, 1) != 0 && errno != EBADF) { failed = 1; } } + if (errno != 0) { + failed = 1; + } closedir(dir); return failed ? -1 : 0; } diff --git a/src/main/backend/backend-desktop-owner-watch.ts b/src/main/backend/backend-desktop-owner-watch.ts index 1fc295ce864..fb62484a576 100644 --- a/src/main/backend/backend-desktop-owner-watch.ts +++ b/src/main/backend/backend-desktop-owner-watch.ts @@ -17,17 +17,18 @@ export function watchDesktopOwner(argv: readonly string[], onGone: () => void): if (process.platform !== 'linux' || !Number.isSafeInteger(pid) || pid <= 1) { return } - let started: string | null | undefined + // Main forked this backend, so a process on main's pid that started later is a reused pid. + const ownStart = readStartTicks('self') let checking = false const check = (): void => { if (checking) { return } checking = true - void desktopState(pid) - .then((state) => { - started ??= state?.startTicks ?? null - if (state === null || (started && state.startTicks && state.startTicks !== started)) { + void Promise.all([desktopState(pid), ownStart]) + .then(([state, own]) => { + const reused = Boolean(state?.startTicks && own && Number(state.startTicks) > Number(own)) + if (state === null || reused) { clearInterval(timer) onGone() } @@ -36,7 +37,7 @@ export function watchDesktopOwner(argv: readonly string[], onGone: () => void): checking = false }) } - // No push signal exists for another process's exit; this stops once main is gone. + // Node exposes no exit event for a process it didn't spawn; this stops once main is gone. const timer = setInterval(check, DESKTOP_OWNER_POLL_MS) timer.unref() check() @@ -51,11 +52,15 @@ async function desktopState(pid: number): Promise<{ startTicks: string | null } ? null : { startTicks: null } } - try { - const stat = await readFile(`/proc/${pid}/stat`, 'utf8') - const state = stat.slice(stat.lastIndexOf(')') + 2, stat.lastIndexOf(')') + 3) - return state === 'Z' || state === 'X' ? null : { startTicks: parseLinuxStartTicks(stat) } - } catch { - return { startTicks: null } + const stat = await readFile(`/proc/${pid}/stat`, 'utf8').catch(() => null) + const state = stat?.slice(stat.lastIndexOf(')') + 2, stat.lastIndexOf(')') + 3) + if (state === 'Z' || state === 'X') { + return null } + return { startTicks: stat ? parseLinuxStartTicks(stat) : null } +} + +async function readStartTicks(pid: number | 'self'): Promise { + const stat = await readFile(`/proc/${pid}/stat`, 'utf8').catch(() => null) + return stat ? parseLinuxStartTicks(stat) : null } diff --git a/src/main/backend/backend-entry.ts b/src/main/backend/backend-entry.ts index f2051f78daa..790a9778083 100644 --- a/src/main/backend/backend-entry.ts +++ b/src/main/backend/backend-entry.ts @@ -80,7 +80,8 @@ function stop(): Promise { async function acquireServiceLock(userDataPath: string) { // Room for an orphaned holder to notice its desktop is gone and finish stopping. - const deadline = Date.now() + ORCAD_SHUTDOWN_DEADLINE_MS + 2 * DESKTOP_OWNER_POLL_MS + const ownerWatchSlack = process.platform === 'linux' ? 2 * DESKTOP_OWNER_POLL_MS : 0 + const deadline = Date.now() + ORCAD_SHUTDOWN_DEADLINE_MS + ownerWatchSlack while (!abort.signal.aborted) { try { return acquireOrcadInstanceLock(join(userDataPath, 'backend-service'), {