mirror of
https://github.com/Ark0N/Codeman.git
synced 2026-11-30 16:39:33 +01:00
fix(tmux): a pane pid of 0 falls back to the recorded pid on close
Review follow-up (owner's merge-time items, applied here).
- killSession() used `pane.pid ?? session.pid`, which kept tmux's 0 for
an empty active pane (`split-window ''`: `1|0|` up to tmux 3.6, `0|0|`
on 3.7) beside a running agent, so the close signalled nothing where
master signals the agent's tree. `||` restores that; isSignalablePid()
still refuses 0 or 1.
- kill-pid-guard.test.ts answered the old pid-only read, which the
three-field parser read as no pid, so no case gave killSession() tmux's
own 0. Its mock now answers the kill read, with cases for `1|0|`,
`0|0|` and `0||` against a recorded agent pid and a recorded 0.
- Tests pin the whole kill read format, and the reconcile rule that a
pane without a pid never overwrites a live pane's pid. Dropping
`#{pane_dead_time}`, using `??` or `pane.pid` alone, or a plain
`active.set(...)` each fail now.
- parsePaneKillRead() cases for `1|0|`, `0|0|` and `1|-1|`.
- Comments and docs: the blank pid with a late dead flag is tmux 3.8;
on 3.4 only the seconds to collect the status were measured. A pid
below 2 is no pid. The recorded pid is unreaped in the closing window
only when it is the pane's start pid (`respawn-pane` breaks that).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
95918b80b4
commit
9309500d40
5 files changed
+113
-17
No files matched your search
@@ -22,6 +22,9 @@ vi.mock('node:child_process', async () => {
|
||||
return { ...actual, exec: vi.fn(), execFile: vi.fn(), execSync: vi.fn(), spawn: vi.fn() };
|
||||
});
|
||||
|
||||
/** The kill path's pane read, as killSession() sends it. */
|
||||
const KILL_READ = "'#{pane_dead}|#{pane_pid}|#{pane_dead_time}'";
|
||||
|
||||
/** A machine: init, kthreadd, the user's own programs, and one agent (4242) with a child. */
|
||||
const PS = ['1 0', '2 0', '900 1', '901 900', '902 900', '4242 901', '4243 4242'].join('\n') + '\n';
|
||||
|
||||
@@ -36,12 +39,18 @@ describe('isSignalablePid', () => {
|
||||
|
||||
describe('killSession and a pane without a process', () => {
|
||||
let panePid: string;
|
||||
/**
|
||||
* The kill path's own read, `#{pane_dead}|#{pane_pid}|#{pane_dead_time}`. Unset, it
|
||||
* answers a live pane with `panePid`, which is what tmux prints for one.
|
||||
*/
|
||||
let paneRead: string | undefined;
|
||||
let commands: string[];
|
||||
let kill: MockInstance<typeof process.kill>;
|
||||
let signals: [number, string | number | undefined][];
|
||||
|
||||
beforeEach(() => {
|
||||
panePid = '4242\n';
|
||||
paneRead = undefined;
|
||||
commands = [];
|
||||
signals = [];
|
||||
vi.mocked(execSync).mockImplementation(((cmd: string) => {
|
||||
@@ -51,7 +60,11 @@ describe('killSession and a pane without a process', () => {
|
||||
vi.mocked(exec).mockImplementation(((cmd: string, options: unknown, maybeCallback?: unknown) => {
|
||||
commands.push(cmd);
|
||||
const callback = (typeof options === 'function' ? options : maybeCallback) as (e: null, out: object) => void;
|
||||
const stdout = cmd.includes('display-message') ? panePid : '';
|
||||
const stdout = !cmd.includes('display-message')
|
||||
? ''
|
||||
: cmd.includes(KILL_READ)
|
||||
? (paneRead ?? `0|${panePid.trim()}|\n`)
|
||||
: panePid;
|
||||
setImmediate(() => callback(null, { stdout, stderr: '' }));
|
||||
return { on: vi.fn(), kill: vi.fn(), pid: 12345 };
|
||||
}) as unknown as typeof exec);
|
||||
@@ -97,6 +110,8 @@ describe('killSession and a pane without a process', () => {
|
||||
return m;
|
||||
}
|
||||
const closedByTmux = () => commands.some((c) => c.includes('kill-session -t "codeman-feed1234"'));
|
||||
/** The close read the pane through the three-field kill read, and not the old pid-only one. */
|
||||
const readTheKillFormat = () => commands.some((c) => c.includes(KILL_READ));
|
||||
|
||||
// The control: without it, a guard that refused every kill would pass the rest.
|
||||
it('still signals the process tree of a real pane', async () => {
|
||||
@@ -106,9 +121,36 @@ describe('killSession and a pane without a process', () => {
|
||||
expect(signals).toContainEqual([-4242, 'SIGTERM']);
|
||||
expect(signals.every(([pid]) => [4242, 4243].includes(Math.abs(pid)))).toBe(true);
|
||||
expect(closedByTmux()).toBe(true);
|
||||
expect(readTheKillFormat()).toBe(true);
|
||||
m.destroy();
|
||||
});
|
||||
|
||||
// tmux's own 0, for an empty active pane beside the agent: `1|0|` up to tmux 3.6,
|
||||
// `0|0|` on 3.7, and `0||` is tmux 3.8's closing window (pty closed, exit not
|
||||
// collected). Each falls back to the recorded agent pid, as master does, and
|
||||
// nothing outside that pid's tree is signalled.
|
||||
for (const answer of ['1|0|', '0|0|', '0||']) {
|
||||
it(`falls back to the recorded pid when the kill read answers ${answer}`, async () => {
|
||||
paneRead = `${answer}\n`;
|
||||
const m = await manager(4242);
|
||||
expect(await m.killSession('feed1234')).toBe(true);
|
||||
expect(readTheKillFormat()).toBe(true);
|
||||
expect(signals).toContainEqual([-4242, 'SIGTERM']);
|
||||
expect(signals.every(([pid]) => [4242, 4243].includes(Math.abs(pid)))).toBe(true);
|
||||
expect(closedByTmux()).toBe(true);
|
||||
m.destroy();
|
||||
});
|
||||
|
||||
it(`signals nothing for ${answer} when the record holds 0 too`, async () => {
|
||||
paneRead = `${answer}\n`;
|
||||
const m = await manager(0);
|
||||
expect(await m.killSession('feed1234')).toBe(true);
|
||||
expect(signals).toEqual([]);
|
||||
expect(closedByTmux()).toBe(true);
|
||||
m.destroy();
|
||||
});
|
||||
}
|
||||
|
||||
for (const pid of [0, 1]) {
|
||||
it(`signals nothing for pid ${pid}, and tmux still closes the session`, async () => {
|
||||
panePid = `${pid}\n`;
|
||||
|
||||
@@ -1309,6 +1309,36 @@ describe('TmuxManager (unit)', () => {
|
||||
}
|
||||
});
|
||||
|
||||
// A split session: the live pane's pid is what the record follows, whichever
|
||||
// order tmux lists the panes in. A pane without a pid never overwrites it.
|
||||
it('keeps the pid of the live pane when another pane of the session has none', async () => {
|
||||
mockedExecSync.mockImplementation((cmd: string) => {
|
||||
if (typeof cmd === 'string' && cmd.includes(' list-panes ')) {
|
||||
return ['codeman-abcd5555|505|0||||/srv', 'codeman-abcd5555||1|0||1795091616|'].join('\n');
|
||||
}
|
||||
return '';
|
||||
});
|
||||
const NonTestTmuxManager = await importWithTmuxCommandsEnabled();
|
||||
const nonTestManager = new NonTestTmuxManager();
|
||||
nonTestManager.registerSession({
|
||||
sessionId: 'abcd5555',
|
||||
muxName: 'codeman-abcd5555',
|
||||
pid: 50,
|
||||
createdAt: Date.now(),
|
||||
workingDir: '/tmp',
|
||||
mode: 'claude',
|
||||
attached: false,
|
||||
});
|
||||
|
||||
try {
|
||||
const { alive } = await nonTestManager.reconcileSessions();
|
||||
expect(alive).toEqual(['abcd5555']);
|
||||
expect(nonTestManager.getSession('abcd5555')?.pid).toBe(505);
|
||||
} finally {
|
||||
nonTestManager.destroy();
|
||||
}
|
||||
});
|
||||
|
||||
it('does not rediscover an untracked session whose only pane is dead and has no pid', async () => {
|
||||
// A made-up pid would reach killSession()'s process-tree walk.
|
||||
mockedExecSync.mockImplementation((cmd: string) => {
|
||||
@@ -1362,7 +1392,12 @@ describe('TmuxManager (unit)', () => {
|
||||
expect(await nonTestManager.killSession('dead5555')).toBe(true);
|
||||
expect(kill.mock.calls.filter(([pid]) => Math.abs(Number(pid)) === 4242)).toEqual([]);
|
||||
const commands = mockedExec.mock.calls.map(([cmd]) => String(cmd));
|
||||
expect(commands.some((cmd) => cmd.includes('display-message') && cmd.includes('#{pane_dead}'))).toBe(true);
|
||||
// The whole kill read: dropping `#{pane_dead_time}` would trust a pre-3.7 dead flag again.
|
||||
expect(
|
||||
commands.some(
|
||||
(cmd) => cmd.includes('display-message') && cmd.includes("'#{pane_dead}|#{pane_pid}|#{pane_dead_time}'")
|
||||
)
|
||||
).toBe(true);
|
||||
expect(commands.some((cmd) => cmd.includes('kill-session -t "codeman-dead5555"'))).toBe(true);
|
||||
expect(nonTestManager.getSession('dead5555')).toBeUndefined();
|
||||
} finally {
|
||||
@@ -1417,7 +1452,12 @@ describe('TmuxManager (unit)', () => {
|
||||
expect(kill.mock.calls.some(([pid]) => Math.abs(Number(pid)) === 4343)).toBe(true);
|
||||
expect(kill.mock.calls.filter(([pid]) => Math.abs(Number(pid)) === 4242)).toEqual([]);
|
||||
const commands = mockedExec.mock.calls.map(([cmd]) => String(cmd));
|
||||
expect(commands.some((cmd) => cmd.includes('display-message') && cmd.includes('#{pane_dead}'))).toBe(true);
|
||||
// The whole kill read: dropping `#{pane_dead_time}` would trust a pre-3.7 dead flag again.
|
||||
expect(
|
||||
commands.some(
|
||||
(cmd) => cmd.includes('display-message') && cmd.includes("'#{pane_dead}|#{pane_pid}|#{pane_dead_time}'")
|
||||
)
|
||||
).toBe(true);
|
||||
expect(commands.some((cmd) => cmd.includes('kill-session -t "codeman-beef5555"'))).toBe(true);
|
||||
expect(nonTestManager.getSession('beef5555')).toBeUndefined();
|
||||
} finally {
|
||||
@@ -1572,9 +1612,10 @@ describe('parsePaneRows', () => {
|
||||
expect(row.currentPath).toBeUndefined();
|
||||
});
|
||||
|
||||
// Measured on tmux 3.4: the pid goes blank when the pty closes, and
|
||||
// `#{pane_dead}` turns 1 only once tmux has collected the exit status, which
|
||||
// took seconds for some exits. A restart in that window must keep the session.
|
||||
// tmux 3.8 blanks the pid when the pty closes, and `#{pane_dead}` turns 1 only
|
||||
// once tmux has collected the exit status. Collecting it took seconds for some
|
||||
// exits (measured on tmux 3.4, where the pid stays and only that delay was
|
||||
// seen). A restart in that window must keep the session.
|
||||
it('keeps a pane whose pid went blank before tmux marked it dead', () => {
|
||||
const [row] = parsePaneRows('codeman-aaaa||0||||/home/user/project');
|
||||
expect(row).toBeDefined();
|
||||
@@ -1621,6 +1662,15 @@ describe('parsePaneKillRead', () => {
|
||||
expect(parsePaneKillRead('1|4242')).toEqual({ dead: false, pid: 4242 });
|
||||
});
|
||||
|
||||
// A pid below 2 is no pid. An empty active pane prints 0 before tmux 3.8, and
|
||||
// killSession() falls back to the recorded pid for it; it is never dead here,
|
||||
// because 0 is not a blank pid and there is no exit time.
|
||||
it('reads a pid of 0 or below as no exit, whatever the flag says', () => {
|
||||
expect(parsePaneKillRead('1|0|')).toEqual({ dead: false, pid: 0 });
|
||||
expect(parsePaneKillRead('0|0|')).toEqual({ dead: false, pid: 0 });
|
||||
expect(parsePaneKillRead('1|-1|')).toEqual({ dead: false, pid: -1 });
|
||||
});
|
||||
|
||||
it('treats an unreadable answer as not dead, so the recorded pid is still used', () => {
|
||||
expect(parsePaneKillRead('')).toEqual({ dead: false, pid: null });
|
||||
expect(parsePaneKillRead('|')).toEqual({ dead: false, pid: null });
|
||||
|
||||
Reference in new issue
Block a user