fix(linux): fail closed when the fd walk errors; detect a reused desktop pid; lock slack on Linux only

This commit is contained in:
Brennan Benson
2026-11-18 10:38:34 -08:00
parent 41908286b4
commit 2af38952da
4 changed files with 29 additions and 17 deletions
+1 -1
View File
@@ -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
+9 -3
View File
@@ -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;
}
+17 -12
View File
@@ -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<string | null> {
const stat = await readFile(`/proc/${pid}/stat`, 'utf8').catch(() => null)
return stat ? parseLinuxStartTicks(stat) : null
}
+2 -1
View File
@@ -80,7 +80,8 @@ function stop(): Promise<void> {
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'), {