Skip to content

Commit 650510b

Browse files
committed
🔬 Sweep the pane terminal at every child's settlement (#726)
A worker's settlement is now the escalation followed by the terminal-holder sweep, on natural exit as well as cancel and shutdown, and `exited` is reported only after it. A new sequential-handoff regression starts a child whose descendant setsid()s, keeps the pane terminal and outlives its parent, and proves a launch sent during settlement is refused, the descendant is named and stopped before `exited`, and only the worker and the second child hold the terminal after admission. Measurements gain the handoff and relaunch latencies the sweep introduces. The setsid() + closed-terminal orphan remains the recorded unprovable boundary.
1 parent 824538a commit 650510b

4 files changed

Lines changed: 231 additions & 27 deletions

File tree

‎scripts/proofs/tmux-pane-workers/checks.ts‎

Lines changed: 166 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,7 @@ export function* checkNegativeChildren(check: Check, context: CheckContext): Ope
752752
{ ordinal: 4, mode: "escape-closed" },
753753
] as const;
754754
const orphanPids: number[] = [];
755+
const orphanExits: (PaneEvent & { type: "exited" })[] = [];
755756
for (const orphan of orphans) {
756757
const id = `orphan-${orphan.mode}`;
757758
yield* workspace.launch(orphan.ordinal, { id, argv: childCommand(evidence(id), orphan.mode) });
@@ -760,14 +761,21 @@ export function* checkNegativeChildren(check: Check, context: CheckContext): Ope
760761
const file = yield* readChildEvidence(evidence(id));
761762
const pid = file?.descendants[0]?.pid ?? -1;
762763
orphanPids.push(pid);
764+
const before = yield* processFacts(pid);
765+
check.expect(
766+
`${id}: the descendant left the session and process group while its parent lived`,
767+
before !== undefined && before.pgid === before.pid && before.tty === "??",
768+
before,
769+
);
763770
yield* workspace.keys(orphan.ordinal, "exit 0", "Enter");
764-
yield* workspace.waitFor(orphan.ordinal, isLaunchEvent("exited", id));
771+
const exited = yield* workspace.waitFor(orphan.ordinal, isLaunchEvent("exited", id));
772+
orphanExits.push(exited);
765773
const row = yield* processFacts(pid);
766-
check.fact(`${id}`, { pid, row });
774+
check.fact(`${id}`, { pid, before, afterParentExit: row, settlement: exited.proof });
767775
check.expect(
768-
`${id}: the escaped descendant outlived its parent and was reparented`,
769-
row !== undefined && row.ppid === 1 && row.tty === "??",
770-
row,
776+
`${id}: outside the settlement's ancestry once the parent exited`,
777+
!exited.proof.descendants.some((entry) => entry.pid === pid),
778+
exited.proof.descendants,
771779
);
772780
}
773781

@@ -802,15 +810,23 @@ export function* checkNegativeChildren(check: Check, context: CheckContext): Ope
802810
byes.map((bye) => bye.ttyHolders),
803811
);
804812
const [holdingOrphan, closedOrphan] = orphanPids;
805-
const foundHolding = byes[3].ttyHolders.find((entry) => entry.pid === holdingOrphan);
813+
const foundHolding = orphanExits[0].proof.terminalHolders.find(
814+
(entry) => entry.pid === holdingOrphan,
815+
);
806816
check.expect(
807-
"orphan holding the terminal: named by the worker's shutdown sweep and stopped",
817+
"orphan holding the terminal: named by its parent's settlement sweep and stopped before `exited`",
808818
foundHolding !== undefined && foundHolding.gone && !isReachable(holdingOrphan),
809819
{ holdingOrphan, foundHolding },
810820
);
811-
const closedFound = byes.some((bye) =>
812-
bye.ttyHolders.some((entry) => entry.pid === closedOrphan),
821+
const closedRow = yield* processFacts(closedOrphan);
822+
check.expect(
823+
"orphan that closed the terminal: outlived its parent, reparented, still running",
824+
closedRow !== undefined && closedRow.ppid === 1 && closedRow.tty === "??",
825+
closedRow,
813826
);
827+
const closedFound =
828+
orphanExits[1].proof.terminalHolders.some((entry) => entry.pid === closedOrphan) ||
829+
byes.some((bye) => bye.ttyHolders.some((entry) => entry.pid === closedOrphan));
814830
const closedAlive = isReachable(closedOrphan);
815831
check.fact("orphanClosed", {
816832
pid: closedOrphan,
@@ -842,6 +858,113 @@ export function* checkNegativeChildren(check: Check, context: CheckContext): Ope
842858
);
843859
}
844860

861+
/**
862+
* Regression: a first child forks a descendant that `setsid()`s, keeps the
863+
* pane's terminal, and outlives its parent. The pane must not admit a second
864+
* child until that descendant is stopped and nothing from the first child's
865+
* lifetime still holds the terminal.
866+
*/
867+
export function* checkSequentialHandoff(check: Check, context: CheckContext): Operation<void> {
868+
const evidence = (name: string) => join(context.evidenceDirectory, `handoff-${name}.json`);
869+
const workspace = yield* useWorkspace({
870+
columns: 1,
871+
panes: 1,
872+
evidenceDirectory: context.evidenceDirectory,
873+
titles: ["handoff"],
874+
});
875+
const pane = workspace.pane(0);
876+
const worker = workspace.links[0].hello.pid;
877+
878+
yield* workspace.launch(0, { id: "first", argv: childCommand(evidence("first"), "escape") });
879+
const first = yield* workspace.waitFor(0, isLaunchEvent("ready", "first"));
880+
yield* childEvidenceHas(evidence("first"), (file) => file.descendants.length === 1);
881+
const descendant = (yield* readChildEvidence(evidence("first")))?.descendants[0]?.pid ?? -1;
882+
const escaped = yield* processFacts(descendant);
883+
const holdersBefore = yield* holdersOf(pane.tty);
884+
check.fact("first", { child: first.pid, descendant, escaped, holdersBefore });
885+
check.expect(
886+
"the descendant left the session and process group and still holds the pane terminal",
887+
escaped !== undefined &&
888+
escaped.pgid === escaped.pid &&
889+
escaped.tty === "??" &&
890+
holdersBefore.includes(descendant),
891+
{ escaped, holdersBefore },
892+
);
893+
894+
// The parent exits on its own; a second launch is sent before `exited`
895+
// can possibly have arrived.
896+
const tExit = Date.now();
897+
yield* workspace.keys(0, "exit 0", "Enter");
898+
yield* workspace.launch(0, { id: "early", argv: childCommand(evidence("early"), "plain") });
899+
const early = yield* workspace.waitFor(
900+
0,
901+
(event): event is PaneEvent & { type: "refused" | "ready" } =>
902+
(event.type === "refused" || event.type === "ready") && event.id === "early",
903+
);
904+
const exited = yield* workspace.waitFor(0, isLaunchEvent("exited", "first"));
905+
const handoffMs = Date.now() - tExit;
906+
const order = workspace.events(0).map((event) => event.type);
907+
check.fact("exited", { exitCode: exited.exitCode, proof: exited.proof, handoffMs, order });
908+
check.expect(
909+
"a launch sent while the first child was settling was refused, before `exited`",
910+
early.type === "refused" && order.indexOf("refused") < order.indexOf("exited"),
911+
{ early, order },
912+
);
913+
const swept = exited.proof.terminalHolders.find((entry) => entry.pid === descendant);
914+
check.expect(
915+
"the orphaned descendant was outside ancestry but named by the settlement's terminal sweep",
916+
!exited.proof.descendants.some((entry) => entry.pid === descendant) && swept !== undefined,
917+
{ descendants: exited.proof.descendants, terminalHolders: exited.proof.terminalHolders },
918+
);
919+
check.expect(
920+
"the descendant was stopped before `exited` was reported",
921+
swept?.gone === true && !isReachable(descendant),
922+
{ swept, reachable: isReachable(descendant) },
923+
);
924+
925+
const tRelaunch = Date.now();
926+
yield* workspace.launch(0, { id: "second", argv: childCommand(evidence("second"), "plain") });
927+
const second = yield* workspace.waitFor(0, isLaunchEvent("ready", "second"));
928+
const relaunchMs = Date.now() - tRelaunch;
929+
yield* childEvidenceHas(evidence("second"), (file) => file.pid === second.pid);
930+
const holdersAfter = yield* holdersOf(pane.tty);
931+
const strangers = holdersAfter.filter((pid) => pid !== worker && pid !== second.pid);
932+
check.fact("second", { child: second.pid, holdersAfter, relaunchMs });
933+
check.expect(
934+
"after admission, only the worker and the second child hold the pane terminal",
935+
strangers.length === 0 && holdersAfter.includes(second.pid),
936+
{ holdersAfter, worker, second: second.pid },
937+
);
938+
check.expect(
939+
"the second child runs on the same pane and worker",
940+
(yield* workspace.grid.tmux.run([
941+
"list-panes",
942+
"-t",
943+
"grid:0",
944+
"-F",
945+
"#{pane_id} #{pane_pid}",
946+
])) === `${pane.id} ${pane.pid}`,
947+
);
948+
949+
yield* workspace.keys(0, "exit 0", "Enter");
950+
yield* workspace.waitFor(0, isLaunchEvent("exited", "second"));
951+
const bye = yield* workspace.shutdown(0);
952+
check.expect(
953+
"nothing held the terminal at shutdown",
954+
bye.ttyHolders.length === 0,
955+
bye.ttyHolders,
956+
);
957+
const stopped = yield* workspace.grid.stop();
958+
check.expect("server stopped", stopped.serverGone && stopped.unreachable);
959+
check.expect(
960+
"first child, descendant, second child gone",
961+
yield* allGone([first.pid, descendant, second.pid]),
962+
);
963+
check.note(
964+
`handoff (exit requested → exited) ${handoffMs} ms; relaunch (launch → ready) ${relaunchMs} ms`,
965+
);
966+
}
967+
845968
export function* checkCancellationPoints(check: Check, context: CheckContext): Operation<void> {
846969
const phases = ["prepared", "workers", "ready", "attached", "active"] as const;
847970
for (const phase of phases) {
@@ -932,6 +1055,10 @@ export interface MeasuredRun {
9321055
readyMs: number;
9331056
attachMs: number;
9341057
closeDetectMs: number;
1058+
/** Exit requested on pane 0 → `exited`, including the settlement sweep. */
1059+
handoffMs: number;
1060+
/** `exited` → the next child on the same pane ready. */
1061+
relaunchMs: number;
9351062
teardownMs: number;
9361063
}
9371064

@@ -980,19 +1107,45 @@ export function* measureOnce(panes: number, context: CheckContext): Operation<Me
9801107
yield* controlUntil(workspace.grid, cursor, (event) => event.kind === "client-detached");
9811108
yield* client.process.exited;
9821109
const closeDetectMs = Date.now() - tClose;
1110+
// Sequential handoff on pane 0: exit requested → `exited` (which follows the
1111+
// settlement sweep) → a second child ready on the same pane.
1112+
const tHandoff = Date.now();
1113+
yield* workspace.keys(0, "exit 0", "Enter");
1114+
yield* workspace.waitFor(0, isLaunchEvent("exited", "m0"));
1115+
const handoffMs = Date.now() - tHandoff;
1116+
const tRelaunch = Date.now();
1117+
yield* workspace.launch(0, {
1118+
id: "m0b",
1119+
argv: childCommand(join(context.evidenceDirectory, `measure-${panes}-0b.json`), "plain"),
1120+
});
1121+
const second = yield* workspace.waitFor(0, isLaunchEvent("ready", "m0b"));
1122+
const relaunchMs = Date.now() - tRelaunch;
9831123
const tTeardown = Date.now();
984-
yield* all(readies.map((_, ordinal) => workspace.cancel(ordinal, `m${ordinal}`)));
1124+
yield* all(
1125+
readies.map((_, ordinal) => workspace.cancel(ordinal, ordinal === 0 ? "m0b" : `m${ordinal}`)),
1126+
);
9851127
yield* all(readies.map((_, ordinal) => workspace.shutdown(ordinal)));
9861128
yield* workspace.grid.stop();
9871129
const gone = yield* allGone([
9881130
...readies.map((event) => event.pid),
1131+
second.pid,
9891132
...workspace.links.map((link) => link.hello.pid),
9901133
]);
9911134
if (!gone) {
9921135
throw new Error(`measurement with ${panes} panes: a process survived teardown`);
9931136
}
9941137
const teardownMs = Date.now() - tTeardown;
995-
return { panes, layoutMs, workersMs, readyMs, attachMs, closeDetectMs, teardownMs };
1138+
return {
1139+
panes,
1140+
layoutMs,
1141+
workersMs,
1142+
readyMs,
1143+
attachMs,
1144+
closeDetectMs,
1145+
handoffMs,
1146+
relaunchMs,
1147+
teardownMs,
1148+
};
9961149
}
9971150

9981151
export function* checkMeasurements(
@@ -1022,6 +1175,8 @@ export function* checkMeasurements(
10221175
"readyMs",
10231176
"attachMs",
10241177
"closeDetectMs",
1178+
"handoffMs",
1179+
"relaunchMs",
10251180
"teardownMs",
10261181
] as const) {
10271182
const values = mine.map((run) => run[key]).toSorted((a, b) => a - b);

‎scripts/proofs/tmux-pane-workers/ipc.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,16 @@ const DescendantOutcomeSchema = z.object({
5555
gone: z.boolean(),
5656
});
5757

58+
const TerminalHolderSchema = z.object({ pid: z.number().int(), gone: z.boolean() });
59+
5860
export const QuiescenceProofSchema = z.object({
5961
method: z.enum(["exited", "interrupted", "killed"]),
6062
childPid: z.number().int().optional(),
6163
childGone: z.boolean(),
6264
descendants: z.array(DescendantOutcomeSchema),
6365
survivors: z.array(z.number().int()),
66+
/** Whatever still held the pane's terminal after the escalation, and whether it is gone. */
67+
terminalHolders: z.array(TerminalHolderSchema),
6468
});
6569

6670
export const FromWorkerSchema = z.discriminatedUnion("type", [
@@ -74,6 +78,8 @@ export const FromWorkerSchema = z.discriminatedUnion("type", [
7478
id: z.string(),
7579
exitCode: z.number().int().optional(),
7680
signal: z.string().optional(),
81+
/** The settlement that preceded this report; the pane is free once it arrives. */
82+
proof: QuiescenceProofSchema,
7783
}),
7884
z.object({
7985
type: z.literal("quiescent"),
@@ -83,7 +89,7 @@ export const FromWorkerSchema = z.discriminatedUnion("type", [
8389
z.object({
8490
type: z.literal("bye"),
8591
/** Processes that still held the pane's terminal at shutdown, and whether they are gone. */
86-
ttyHolders: z.array(z.object({ pid: z.number().int(), gone: z.boolean() })),
92+
ttyHolders: z.array(TerminalHolderSchema),
8793
}),
8894
]);
8995

‎scripts/proofs/tmux-pane-workers/proof.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import {
3535
checkMeasurements,
3636
checkNegativeChildren,
3737
checkReadinessBoundary,
38+
checkSequentialHandoff,
3839
checkSignalsDistinct,
3940
checkStartupFailureAtomic,
4041
} from "./checks.ts";
@@ -134,6 +135,7 @@ function* runChecks(options: Options, outDirectory: string): Operation<number> {
134135
["startup-failure-atomic", (check) => checkStartupFailureAtomic(check, context)],
135136
["signals-distinct", (check) => checkSignalsDistinct(check, context)],
136137
["negative-children", (check) => checkNegativeChildren(check, context)],
138+
["sequential-handoff", (check) => checkSequentialHandoff(check, context)],
137139
["cancellation-points", (check) => checkCancellationPoints(check, context)],
138140
["measurements", (check) => checkMeasurements(check, context, options.runs)],
139141
];

0 commit comments

Comments
 (0)