mirror of
https://github.com/Ark0N/Codeman.git
synced 2026-11-27 07:59:33 +01:00
fix(tmux): closing a session never signals pid 0 or 1
tmux answers 0 for the pid of a pane without a process (`split-window ''`). The pane list read at server start recorded that 0 on the session, and the next close of the tab walked the children of pid 0, which is the whole machine: every process of the account was signalled, all sessions, tmux itself and the systemd user manager included. killSession() now decides once whether there is a process to signal (isSignalablePid(), an integer above 1) and otherwise leaves the close to `tmux kill-session`. The descendant walk of the kill path returns nothing for such a root, and signalProcess(), the only place in tmux-manager.ts that sends a signal, refuses it for the process group form as well. proc-walk-bounds used pid 1 as the root of its fixture tree, which the kill path now refuses on purpose, so the fixture is rooted at a real pid. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
5548c17c4a
commit
babb55e4d9
6 files changed
+212
-17
No files matched your search
@@ -32,6 +32,23 @@ export const PROC_WALK_MAX_DEPTH = 10;
|
||||
/** Hard ceiling on collected descendants. A backstop, not an expected limit. */
|
||||
export const PROC_WALK_MAX_NODES = 500;
|
||||
|
||||
/**
|
||||
* Whether a session kill may signal `pid`, or walk the tree below it: a real process of
|
||||
* the user's, so an integer above 1.
|
||||
*
|
||||
* On 2026-11-25 a kill that got pid 0 took down every process of the account: all of the
|
||||
* user's sessions, tmux itself and the systemd user manager. The children of pid 0 are
|
||||
* the whole machine, so the bounds above do not help (500 processes are most of one), and
|
||||
* `kill(-1)` means every process one may signal while `kill(0)` means one's own group.
|
||||
*
|
||||
* tmux answers 0 for a pane with no process (`split-window ''`), and that 0 reaches the
|
||||
* session record through the pane list read at server start. So 0 is an ordinary answer
|
||||
* here, not a corrupt one, and it means "nothing to signal".
|
||||
*/
|
||||
export function isSignalablePid(pid: number | null | undefined): pid is number {
|
||||
return typeof pid === 'number' && Number.isInteger(pid) && pid > 1;
|
||||
}
|
||||
|
||||
export interface WalkOptions {
|
||||
maxDepth?: number;
|
||||
maxNodes?: number;
|
||||
|
||||
+30
-10
@@ -22,7 +22,7 @@
|
||||
*/
|
||||
|
||||
import { EventEmitter } from 'node:events';
|
||||
import { collectDescendants, descendantsInPaneGroup } from './proc-tree.js';
|
||||
import { collectDescendants, descendantsInPaneGroup, isSignalablePid } from './proc-tree.js';
|
||||
import {
|
||||
foreignViewSessionName,
|
||||
buildForeignDockerViewKillCommand,
|
||||
@@ -3148,6 +3148,8 @@ export class TmuxManager extends EventEmitter implements TerminalMultiplexer {
|
||||
* its process-group and tmux fallbacks.
|
||||
*/
|
||||
private async getChildPidsFresh(pid: number): Promise<number[]> {
|
||||
// The children of pid 0 or 1 are the whole machine (see isSignalablePid).
|
||||
if (!isSignalablePid(pid)) return [];
|
||||
let byParent: ReadonlyMap<number, readonly number[]>;
|
||||
try {
|
||||
byParent = await Promise.race([
|
||||
@@ -3205,6 +3207,19 @@ export class TmuxManager extends EventEmitter implements TerminalMultiplexer {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The one way this class signals a process. Refuses what is not a session's process
|
||||
* (see {@link isSignalablePid}); a negative `pid` is the process group, so it is judged
|
||||
* by its absolute value and `-1` (everything) and `-0` (our own group) are refused too.
|
||||
*/
|
||||
private signalProcess(pid: number, signal: 'SIGTERM' | 'SIGKILL'): void {
|
||||
if (!isSignalablePid(Math.abs(pid))) {
|
||||
console.error(`[TmuxManager] Refusing to send ${signal} to pid ${pid}`);
|
||||
return;
|
||||
}
|
||||
process.kill(pid, signal);
|
||||
}
|
||||
|
||||
// Check if a process is still alive. Signal decisions only: an unreaped zombie
|
||||
// counts here, so its process group still gets the SIGKILL below. The WAITS use
|
||||
// waitForProcessesExit(), which counts a zombie as exited.
|
||||
@@ -3348,10 +3363,15 @@ export class TmuxManager extends EventEmitter implements TerminalMultiplexer {
|
||||
|
||||
// Get current PID (may have changed)
|
||||
const currentPid = (await this.getPanePidAsync(session.muxName)) || session.pid;
|
||||
// An empty pane has no process: tmux says 0, and so does the record once a server
|
||||
// start has read it. Nothing is signalled then, and only tmux is asked to close it.
|
||||
const hasProcess = isSignalablePid(currentPid);
|
||||
|
||||
console.log(`[TmuxManager] Killing session ${session.muxName} (PID ${currentPid})`);
|
||||
console.log(
|
||||
`[TmuxManager] Killing session ${session.muxName} (${hasProcess ? `PID ${currentPid}` : `no process, pane pid ${currentPid}`})`
|
||||
);
|
||||
|
||||
const allPids: number[] = [currentPid];
|
||||
const allPids: number[] = hasProcess ? [currentPid] : [];
|
||||
|
||||
// Strategy 1: Kill all child processes recursively (but never a shared daemon the
|
||||
// pane's CLI started for every session of that CLI: see killableDescendants)
|
||||
@@ -3363,7 +3383,7 @@ export class TmuxManager extends EventEmitter implements TerminalMultiplexer {
|
||||
for (const childPid of [...childPids].reverse()) {
|
||||
if (this.isProcessAlive(childPid)) {
|
||||
try {
|
||||
process.kill(childPid, 'SIGTERM');
|
||||
this.signalProcess(childPid, 'SIGTERM');
|
||||
} catch {
|
||||
// Process may already be dead
|
||||
}
|
||||
@@ -3378,7 +3398,7 @@ export class TmuxManager extends EventEmitter implements TerminalMultiplexer {
|
||||
for (const childPid of childPids) {
|
||||
if (this.isProcessAlive(childPid)) {
|
||||
try {
|
||||
process.kill(childPid, 'SIGKILL');
|
||||
this.signalProcess(childPid, 'SIGKILL');
|
||||
} catch {
|
||||
// Process already terminated
|
||||
}
|
||||
@@ -3387,12 +3407,12 @@ export class TmuxManager extends EventEmitter implements TerminalMultiplexer {
|
||||
}
|
||||
|
||||
// Strategy 2: Kill the entire process group
|
||||
if (this.isProcessAlive(currentPid)) {
|
||||
if (hasProcess && this.isProcessAlive(currentPid)) {
|
||||
try {
|
||||
process.kill(-currentPid, 'SIGTERM');
|
||||
this.signalProcess(-currentPid, 'SIGTERM');
|
||||
await waitForProcessesExit([currentPid], { timeoutMs: GRACEFUL_SHUTDOWN_WAIT_MS });
|
||||
if (this.isProcessAlive(currentPid)) {
|
||||
process.kill(-currentPid, 'SIGKILL');
|
||||
this.signalProcess(-currentPid, 'SIGKILL');
|
||||
}
|
||||
} catch {
|
||||
// Process group may not exist or already terminated
|
||||
@@ -3439,9 +3459,9 @@ export class TmuxManager extends EventEmitter implements TerminalMultiplexer {
|
||||
}
|
||||
|
||||
// Strategy 4: Direct kill by PID as final fallback
|
||||
if (this.isProcessAlive(currentPid)) {
|
||||
if (hasProcess && this.isProcessAlive(currentPid)) {
|
||||
try {
|
||||
process.kill(currentPid, 'SIGKILL');
|
||||
this.signalProcess(currentPid, 'SIGKILL');
|
||||
} catch {
|
||||
// Already dead
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user