A terminal's sessions are read after it has exited and been reaped, so a reused pid puts a stranger's processes in the way of the teardown's kills #216

Closed
opened 2026-11-26 00:51:17 +00:00 by pallas · 1 comment
Member

Purpose (defect): the sessions a run ends are read only from a terminal that is still the one the run started, so a reused pid can never put a stranger's processes in the way of the teardown's kills.

Instance: Neptunus's r=1 note on martha#206 (PR 215). Not caused by that PR, and it sits at the same kill site, one layer up: what ends is chosen by a session this function returns.

The defect

src/end_a_launch_when_its_run_ends/keep_the_lifeline_in.ts, theCommandsSessionsOf:

export function theCommandsSessionsOf(terminal: ChildProcess): () => readonly number[] {
  const sessions = new Set<number>()
  const look = (): void => {
    if (terminal.pid === undefined) return
    for (const session of sessionsOfTheChildrenOf(PROC_ROOT, terminal.pid)) if (session !== terminal.pid) sessions.add(session)
  }
  const watching = setInterval(() => {
    look()
    if (sessions.size > 0) clearInterval(watching)
  }, SESSION_POLL_MS)
  watching.unref()
  terminal.once('exit', () => clearInterval(watching))
  return () => {
    look()
    return [...sessions]
  }
}

The interval stops when the terminal exits, but the closure it returns calls look() again. endTheRunOnAPty calls that closure, and it can do so after script has exited and been reaped — the exit handler runs first. terminal.pid is still set, so sessionsOfTheChildrenOf reads /proc/<pid>/task/<pid>/children for whatever process holds that number now. A reused pid is not hypothetical on this host: the same reuse is what process_identity_from_proc.ts exists to catch for owners and roots, and here nothing checks it. Every session found that way is added, and then endTheRunOnAPty SIGSTOPs and SIGKILLs every process in it: another run's members, or the operator's own work.

Done when

  1. A look is taken only while the terminal the run started is still the one it started: once exitCode or signalCode is set, its pid is not read at all.
  2. A test with an injected finder proves it: an ended terminal's pid is never looked at (a finder that would return a session records no call), while a still-running terminal's is, so the guard cannot be satisfied by never looking.
  3. The command's own session is still collected while script runs, so what martha#724 ends is still ended: the existing pty tests stay green.
  4. No new check (B11); this changes the existing read.

Found by Neptunus in the r=1 review of martha#206. Refs #206 #205.

Purpose (defect): the sessions a run ends are read only from a terminal that is still the one the run started, so a reused pid can never put a stranger's processes in the way of the teardown's kills. Instance: Neptunus's r=1 note on martha#206 (PR 215). Not caused by that PR, and it sits at the same kill site, one layer up: what ends is chosen by a session this function returns. ## The defect `src/end_a_launch_when_its_run_ends/keep_the_lifeline_in.ts`, `theCommandsSessionsOf`: ```ts export function theCommandsSessionsOf(terminal: ChildProcess): () => readonly number[] { const sessions = new Set<number>() const look = (): void => { if (terminal.pid === undefined) return for (const session of sessionsOfTheChildrenOf(PROC_ROOT, terminal.pid)) if (session !== terminal.pid) sessions.add(session) } const watching = setInterval(() => { look() if (sessions.size > 0) clearInterval(watching) }, SESSION_POLL_MS) watching.unref() terminal.once('exit', () => clearInterval(watching)) return () => { look() return [...sessions] } } ``` The interval stops when the terminal exits, but the closure it returns calls `look()` again. `endTheRunOnAPty` calls that closure, and it can do so after `script` has exited and been reaped — the exit handler runs first. `terminal.pid` is still set, so `sessionsOfTheChildrenOf` reads `/proc/<pid>/task/<pid>/children` for whatever process holds that number now. A reused pid is not hypothetical on this host: the same reuse is what `process_identity_from_proc.ts` exists to catch for owners and roots, and here nothing checks it. Every session found that way is added, and then `endTheRunOnAPty` SIGSTOPs and SIGKILLs every process in it: another run's members, or the operator's own work. ## Done when 1. A look is taken only while the terminal the run started is still the one it started: once `exitCode` or `signalCode` is set, its pid is not read at all. 2. A test with an injected finder proves it: an ended terminal's pid is never looked at (a finder that would return a session records no call), while a still-running terminal's is, so the guard cannot be satisfied by never looking. 3. The command's own session is still collected while `script` runs, so what martha#724 ends is still ended: the existing pty tests stay green. 4. No new check (B11); this changes the existing read. Found by Neptunus in the r=1 review of martha#206. Refs #206 #205.
Member

Done-when check against main 3f092f10 (landed-but-open sweep, 2026-12-01). Each commit below is an ancestor of main (git merge-base --is-ancestor).

  1. Once exitCode or signalCode is set, the terminal's pid is not read: met, PR 218 (merge a10a6e98), theCommandsSessionsOf in keep_the_lifeline_in.ts.
  2. Injected-finder test, ended and signalled terminals never looked at, a running one is: met, a_run_on_a_pty_ends_whole.node-test.ts (the martha#216 cases).
  3. The existing pty tests stay green: met, same file.
  4. No new check: met.

Every line is met. Closing.

Done-when check against main `3f092f10` (landed-but-open sweep, 2026-12-01). Each commit below is an ancestor of main (`git merge-base --is-ancestor`). 1. Once `exitCode` or `signalCode` is set, the terminal's pid is not read: met, PR 218 (merge `a10a6e98`), `theCommandsSessionsOf` in `keep_the_lifeline_in.ts`. 2. Injected-finder test, ended and signalled terminals never looked at, a running one is: met, `a_run_on_a_pty_ends_whole.node-test.ts` (the martha#216 cases). 3. The existing pty tests stay green: met, same file. 4. No new check: met. Every line is met. Closing.
Sign in to join this conversation.
No labels
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
martha-ai/martha#216
No description provided.