@code-yeongyu/senpi-pty 2026.11.6-2 → 2026.11.9-2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/pipe-fallback.d.ts +6 -13
- package/dist/pipe-fallback.js +32 -47
- package/dist/process-tree.d.ts +55 -0
- package/dist/process-tree.js +148 -0
- package/dist/registry-types.d.ts +2 -0
- package/dist/registry.js +7 -2
- package/dist/session-exit.d.ts +4 -0
- package/dist/session-exit.js +26 -0
- package/dist/session-types.d.ts +3 -0
- package/dist/session.d.ts +6 -2
- package/dist/session.js +29 -32
- package/native/prebuilds/win32-arm64/senpi_pty.win32-arm64.node +0 -0
- package/native/prebuilds/win32-x64/senpi_pty.win32-x64.node +0 -0
- package/package.json +1 -1
package/dist/pipe-fallback.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { spawn } from "node:child_process";
|
|
2
1
|
import type { NativePtyLoadResult } from "./native-loader.ts";
|
|
2
|
+
export { terminateChildTree } from "./process-tree.ts";
|
|
3
3
|
export interface PipeFallbackSessionOptions {
|
|
4
4
|
readonly command: string;
|
|
5
5
|
readonly args?: readonly string[];
|
|
@@ -25,21 +25,10 @@ export type PipeFallbackOperationResult = {
|
|
|
25
25
|
readonly code: "exited" | "not_pty" | "not_started" | "stdin_unavailable";
|
|
26
26
|
readonly note: string;
|
|
27
27
|
};
|
|
28
|
-
type ChildProcessHandle = ReturnType<typeof spawn>;
|
|
29
28
|
type DataHandler = (chunk: Buffer) => void;
|
|
30
29
|
type ExitHandler = (exit: PipeFallbackSessionExit) => void;
|
|
31
30
|
export declare function isPipeFallbackForced(env?: Readonly<Record<string, string | undefined>>): boolean;
|
|
32
31
|
export declare function shouldUsePipeFallback(nativeLoadResult: NativePtyLoadResult, env?: Readonly<Record<string, string | undefined>>): boolean;
|
|
33
|
-
/**
|
|
34
|
-
* Terminate the child, preferring its whole process group on POSIX. The
|
|
35
|
-
* detached child is its own group leader and grandchildren inherit the stdio
|
|
36
|
-
* pipes, so leaving them alive keeps 'close' from ever firing (the POSIX twin
|
|
37
|
-
* of the Windows taskkill /T branch below). Every path that stops a child must
|
|
38
|
-
* go through here: when the group signal is unavailable (Windows, no pid) or
|
|
39
|
-
* the group is already gone, it still falls back to a direct kill, so the
|
|
40
|
-
* child is never left running.
|
|
41
|
-
*/
|
|
42
|
-
export declare function terminateChildTree(child: Pick<ChildProcessHandle, "pid" | "kill">, signal: NodeJS.Signals): "group" | "direct" | "none";
|
|
43
32
|
export declare class PipeFallbackSession {
|
|
44
33
|
readonly backend = "pipe-fallback";
|
|
45
34
|
readonly note = "Running with child_process pipe fallback because no PTY backend is active; terminal screen state and resize are unavailable.";
|
|
@@ -51,6 +40,8 @@ export declare class PipeFallbackSession {
|
|
|
51
40
|
private spawnError;
|
|
52
41
|
private timedOut;
|
|
53
42
|
private timeoutHandle;
|
|
43
|
+
private drainHandle;
|
|
44
|
+
private readonly leftover;
|
|
54
45
|
private readonly dataHandlers;
|
|
55
46
|
private readonly exitHandlers;
|
|
56
47
|
constructor(options: PipeFallbackSessionOptions);
|
|
@@ -64,9 +55,11 @@ export declare class PipeFallbackSession {
|
|
|
64
55
|
resize(cols: number, rows: number): PipeFallbackOperationResult;
|
|
65
56
|
kill(signal?: NodeJS.Signals): PipeFallbackOperationResult;
|
|
66
57
|
waitExit(): Promise<PipeFallbackSessionExit>;
|
|
58
|
+
/** After the settle, whether processes the command started are still in its process group (senpi#6201). */
|
|
59
|
+
leftoverAlive(): boolean;
|
|
60
|
+
waitLeftoverGone(graceMs: number): Promise<boolean>;
|
|
67
61
|
private emitData;
|
|
68
62
|
private settleExit;
|
|
69
63
|
}
|
|
70
64
|
export declare function createPipeFallbackSession(options: PipeFallbackSessionOptions): PipeFallbackSession;
|
|
71
|
-
export {};
|
|
72
65
|
//# sourceMappingURL=pipe-fallback.d.ts.map
|
package/dist/pipe-fallback.js
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
import { spawn } from "node:child_process";
|
|
2
|
+
import { LeftoverProcessGroup, terminateChildTree, terminateWindowsTree } from "./process-tree.js";
|
|
3
|
+
export { terminateChildTree } from "./process-tree.js";
|
|
2
4
|
const PIPE_FALLBACK_NOTE = "Running with child_process pipe fallback because no PTY backend is active; terminal screen state and resize are unavailable.";
|
|
5
|
+
/**
|
|
6
|
+
* How long stdout/stderr may keep draining after the child exits. 'close' waits for every holder of
|
|
7
|
+
* the pipes, so a grandchild the command left running would hold the exit for its whole life; the
|
|
8
|
+
* native PTY settles when the shell exits (senpi#6201).
|
|
9
|
+
*/
|
|
10
|
+
const EXIT_DRAIN_GRACE_MS = 250;
|
|
3
11
|
export function isPipeFallbackForced(env = process.env) {
|
|
4
12
|
const value = env.SENPI_PTY_FORCE_PIPE;
|
|
5
13
|
return value === "1" || value === "true";
|
|
@@ -7,32 +15,6 @@ export function isPipeFallbackForced(env = process.env) {
|
|
|
7
15
|
export function shouldUsePipeFallback(nativeLoadResult, env = process.env) {
|
|
8
16
|
return isPipeFallbackForced(env) || nativeLoadResult.native === null;
|
|
9
17
|
}
|
|
10
|
-
/**
|
|
11
|
-
* Terminate the child, preferring its whole process group on POSIX. The
|
|
12
|
-
* detached child is its own group leader and grandchildren inherit the stdio
|
|
13
|
-
* pipes, so leaving them alive keeps 'close' from ever firing (the POSIX twin
|
|
14
|
-
* of the Windows taskkill /T branch below). Every path that stops a child must
|
|
15
|
-
* go through here: when the group signal is unavailable (Windows, no pid) or
|
|
16
|
-
* the group is already gone, it still falls back to a direct kill, so the
|
|
17
|
-
* child is never left running.
|
|
18
|
-
*/
|
|
19
|
-
export function terminateChildTree(child, signal) {
|
|
20
|
-
const pid = child.pid;
|
|
21
|
-
// A child that never started has no pid; on Node < 24.22 / 26.10 its kill() signals our own process group.
|
|
22
|
-
if (pid === undefined)
|
|
23
|
-
return "none";
|
|
24
|
-
if (process.platform !== "win32") {
|
|
25
|
-
try {
|
|
26
|
-
process.kill(-pid, signal);
|
|
27
|
-
return "group";
|
|
28
|
-
}
|
|
29
|
-
catch {
|
|
30
|
-
// Group already gone — fall through to the direct kill.
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
child.kill(signal);
|
|
34
|
-
return "direct";
|
|
35
|
-
}
|
|
36
18
|
function normalizeSpawnError(command, error) {
|
|
37
19
|
return {
|
|
38
20
|
code: "spawn_error",
|
|
@@ -57,6 +39,7 @@ export class PipeFallbackSession {
|
|
|
57
39
|
this.exitResult = null;
|
|
58
40
|
this.resolveExit = null;
|
|
59
41
|
this.timedOut = false;
|
|
42
|
+
this.leftover = new LeftoverProcessGroup();
|
|
60
43
|
this.dataHandlers = new Set();
|
|
61
44
|
this.exitHandlers = new Set();
|
|
62
45
|
this.options = {
|
|
@@ -101,14 +84,18 @@ export class PipeFallbackSession {
|
|
|
101
84
|
child.on("error", (error) => {
|
|
102
85
|
this.spawnError = normalizeSpawnError(this.options.command, error);
|
|
103
86
|
});
|
|
104
|
-
|
|
87
|
+
const settleChildExit = (exitCode, signal) => {
|
|
105
88
|
this.settleExit({
|
|
106
89
|
exitCode: this.spawnError ? null : exitCode,
|
|
107
90
|
signal,
|
|
108
91
|
timedOut: this.timedOut,
|
|
109
92
|
error: this.spawnError,
|
|
110
93
|
});
|
|
94
|
+
};
|
|
95
|
+
child.on("exit", (exitCode, signal) => {
|
|
96
|
+
this.drainHandle = setTimeout(() => settleChildExit(exitCode, signal), EXIT_DRAIN_GRACE_MS);
|
|
111
97
|
});
|
|
98
|
+
child.on("close", settleChildExit);
|
|
112
99
|
if (this.options.timeoutMs !== undefined) {
|
|
113
100
|
this.timeoutHandle = setTimeout(() => {
|
|
114
101
|
this.timedOut = true;
|
|
@@ -184,6 +171,12 @@ export class PipeFallbackSession {
|
|
|
184
171
|
}
|
|
185
172
|
kill(signal = "SIGTERM") {
|
|
186
173
|
if (this.exitResult !== null) {
|
|
174
|
+
const pgid = this.leftover.signal(signal);
|
|
175
|
+
if (pgid !== undefined)
|
|
176
|
+
return {
|
|
177
|
+
ok: true,
|
|
178
|
+
note: `Sent ${signal} to what the exited session left in its process group (pgid ${pgid}).`,
|
|
179
|
+
};
|
|
187
180
|
return { ok: false, code: "exited", note: "Cannot kill pipe fallback session: session has exited." };
|
|
188
181
|
}
|
|
189
182
|
if (this.child === null) {
|
|
@@ -191,26 +184,8 @@ export class PipeFallbackSession {
|
|
|
191
184
|
}
|
|
192
185
|
const child = this.child;
|
|
193
186
|
const pid = child.pid;
|
|
194
|
-
if (process.platform === "win32" && pid !== undefined)
|
|
195
|
-
|
|
196
|
-
// grandchildren (e.g. `sleep` under `bash.exe -c`) alive and holding the stdout
|
|
197
|
-
// pipe open — so 'close' never fires and waitExit() hangs teardown. taskkill /T /F
|
|
198
|
-
// terminates the whole tree so the pipe EOFs and the session settles.
|
|
199
|
-
try {
|
|
200
|
-
const killer = spawn("taskkill.exe", ["/pid", String(pid), "/T", "/F"], {
|
|
201
|
-
stdio: "ignore",
|
|
202
|
-
windowsHide: true,
|
|
203
|
-
});
|
|
204
|
-
killer.once("error", () => {
|
|
205
|
-
terminateChildTree(child, signal);
|
|
206
|
-
});
|
|
207
|
-
return { ok: true, note: `Terminated child_process pipe fallback process tree (pid ${pid}).` };
|
|
208
|
-
}
|
|
209
|
-
catch {
|
|
210
|
-
terminateChildTree(child, signal);
|
|
211
|
-
return { ok: true, note: `Sent ${signal} to child_process pipe fallback session.` };
|
|
212
|
-
}
|
|
213
|
-
}
|
|
187
|
+
if (process.platform === "win32" && pid !== undefined)
|
|
188
|
+
return { ok: true, note: terminateWindowsTree(child, pid, signal) };
|
|
214
189
|
const route = terminateChildTree(this.child, signal);
|
|
215
190
|
if (route === "none") {
|
|
216
191
|
return {
|
|
@@ -227,6 +202,13 @@ export class PipeFallbackSession {
|
|
|
227
202
|
waitExit() {
|
|
228
203
|
return this.exitPromise;
|
|
229
204
|
}
|
|
205
|
+
/** After the settle, whether processes the command started are still in its process group (senpi#6201). */
|
|
206
|
+
leftoverAlive() {
|
|
207
|
+
return this.exitResult !== null && this.leftover.alive();
|
|
208
|
+
}
|
|
209
|
+
waitLeftoverGone(graceMs) {
|
|
210
|
+
return this.leftover.waitGone(graceMs);
|
|
211
|
+
}
|
|
230
212
|
emitData(chunk) {
|
|
231
213
|
for (const handler of this.dataHandlers)
|
|
232
214
|
handler(chunk);
|
|
@@ -236,6 +218,9 @@ export class PipeFallbackSession {
|
|
|
236
218
|
return;
|
|
237
219
|
if (this.timeoutHandle)
|
|
238
220
|
clearTimeout(this.timeoutHandle);
|
|
221
|
+
if (this.drainHandle)
|
|
222
|
+
clearTimeout(this.drainHandle);
|
|
223
|
+
this.leftover.arm(this.processGroupId);
|
|
239
224
|
this.exitResult = exit;
|
|
240
225
|
this.resolveExit?.(exit);
|
|
241
226
|
for (const handler of this.exitHandlers)
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { type ChildProcess } from "node:child_process";
|
|
2
|
+
import type { TerminalSessionHandle, TerminalSessionSignal } from "./session-types.ts";
|
|
3
|
+
/**
|
|
4
|
+
* Terminate the child, preferring its whole process group on POSIX. The
|
|
5
|
+
* detached child is its own group leader and grandchildren inherit the stdio
|
|
6
|
+
* pipes, so leaving them alive keeps 'close' from ever firing (the POSIX twin
|
|
7
|
+
* of the Windows taskkill /T branch below). Every path that stops a child must
|
|
8
|
+
* go through here: when the group signal is unavailable (Windows, no pid) or
|
|
9
|
+
* the group is already gone, it still falls back to a direct kill, so the
|
|
10
|
+
* child is never left running.
|
|
11
|
+
*/
|
|
12
|
+
export declare function terminateChildTree(child: Pick<ChildProcess, "pid" | "kill">, signal: NodeJS.Signals): "group" | "direct" | "none";
|
|
13
|
+
/**
|
|
14
|
+
* On Windows child.kill() TerminateProcess-es only the direct child, leaving
|
|
15
|
+
* grandchildren (e.g. `sleep` under `bash.exe -c`) alive and holding the stdout
|
|
16
|
+
* pipe open — so 'close' never fires and waitExit() hangs teardown. taskkill /T /F
|
|
17
|
+
* terminates the whole tree so the pipe EOFs and the session settles. Returns the note.
|
|
18
|
+
*/
|
|
19
|
+
export declare function terminateWindowsTree(child: ChildProcess, pid: number, signal: NodeJS.Signals): string;
|
|
20
|
+
/**
|
|
21
|
+
* The process group of a session that settled while processes the command started were still in it
|
|
22
|
+
* (`cmd &`, a daemon, the children of a shell killed from outside). Settling does not kill them
|
|
23
|
+
* (senpi#6201); this keeps them reachable from kill(), terminate() and registry teardown. It is keyed on
|
|
24
|
+
* the group and remembered only when the group still had members at the settle.
|
|
25
|
+
*
|
|
26
|
+
* The id must never be signalled once it could name someone else's group. POSIX does not hand an id out as
|
|
27
|
+
* a pid while a process group with that id exists, so it can only be reused after the group empties. An
|
|
28
|
+
* armed record therefore probes on its own every `LEFTOVER_PROBE_MS` (unref'd: it never keeps the process
|
|
29
|
+
* alive) and disarms for good the first time the group is seen empty, from the probe or from any stop
|
|
30
|
+
* path, which also clears the probe. A stranger could only be hit if the pid space wrapped (99999 on macOS,
|
|
31
|
+
* pid_max on Linux) and handed out exactly this id as a new group leader within one probe interval after
|
|
32
|
+
* the last member exited: not a realistic window.
|
|
33
|
+
*/
|
|
34
|
+
export declare class LeftoverProcessGroup {
|
|
35
|
+
#private;
|
|
36
|
+
constructor(options?: {
|
|
37
|
+
readonly hasMembers?: (pgid: number) => boolean;
|
|
38
|
+
readonly probeIntervalMs?: number;
|
|
39
|
+
});
|
|
40
|
+
/** The group still remembered, without probing it; undefined once disarmed. */
|
|
41
|
+
get armedGroup(): number | undefined;
|
|
42
|
+
/** Called once at the settle. */
|
|
43
|
+
arm(pgid: number | undefined): void;
|
|
44
|
+
alive(): boolean;
|
|
45
|
+
/** Signal the group while it has members; the pgid signalled, or undefined when nothing is left. */
|
|
46
|
+
signal(signal: NodeJS.Signals): number | undefined;
|
|
47
|
+
/** Wait up to `graceMs` for the group to empty; true once nothing is left. */
|
|
48
|
+
waitGone(graceMs: number): Promise<boolean>;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* TERM, a bounded grace, then KILL for whatever a settled session left in its process group: the same
|
|
52
|
+
* escalation a live session gets. A backend without a leftover group (native, Bun) makes this a no-op.
|
|
53
|
+
*/
|
|
54
|
+
export declare function stopLeftoverGroup(handle: TerminalSessionHandle, signal: TerminalSessionSignal, graceMs: number, forcedGraceMs: number): Promise<void>;
|
|
55
|
+
//# sourceMappingURL=process-tree.d.ts.map
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import process from "node:process";
|
|
3
|
+
/** How often a stop path re-checks whether a process group still has members; no event reports it. */
|
|
4
|
+
const GROUP_POLL_MS = 25;
|
|
5
|
+
/** How often an armed leftover record checks on its own whether its group is gone (see LeftoverProcessGroup). */
|
|
6
|
+
const LEFTOVER_PROBE_MS = 1000;
|
|
7
|
+
/**
|
|
8
|
+
* Terminate the child, preferring its whole process group on POSIX. The
|
|
9
|
+
* detached child is its own group leader and grandchildren inherit the stdio
|
|
10
|
+
* pipes, so leaving them alive keeps 'close' from ever firing (the POSIX twin
|
|
11
|
+
* of the Windows taskkill /T branch below). Every path that stops a child must
|
|
12
|
+
* go through here: when the group signal is unavailable (Windows, no pid) or
|
|
13
|
+
* the group is already gone, it still falls back to a direct kill, so the
|
|
14
|
+
* child is never left running.
|
|
15
|
+
*/
|
|
16
|
+
export function terminateChildTree(child, signal) {
|
|
17
|
+
const pid = child.pid;
|
|
18
|
+
// A child that never started has no pid; on Node < 24.22 / 26.10 its kill() signals our own process group.
|
|
19
|
+
if (pid === undefined)
|
|
20
|
+
return "none";
|
|
21
|
+
if (process.platform !== "win32") {
|
|
22
|
+
try {
|
|
23
|
+
process.kill(-pid, signal);
|
|
24
|
+
return "group";
|
|
25
|
+
}
|
|
26
|
+
catch {
|
|
27
|
+
// Group already gone — fall through to the direct kill.
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
child.kill(signal);
|
|
31
|
+
return "direct";
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* On Windows child.kill() TerminateProcess-es only the direct child, leaving
|
|
35
|
+
* grandchildren (e.g. `sleep` under `bash.exe -c`) alive and holding the stdout
|
|
36
|
+
* pipe open — so 'close' never fires and waitExit() hangs teardown. taskkill /T /F
|
|
37
|
+
* terminates the whole tree so the pipe EOFs and the session settles. Returns the note.
|
|
38
|
+
*/
|
|
39
|
+
export function terminateWindowsTree(child, pid, signal) {
|
|
40
|
+
try {
|
|
41
|
+
const killer = spawn("taskkill.exe", ["/pid", String(pid), "/T", "/F"], { stdio: "ignore", windowsHide: true });
|
|
42
|
+
killer.once("error", () => {
|
|
43
|
+
terminateChildTree(child, signal);
|
|
44
|
+
});
|
|
45
|
+
return `Terminated child_process pipe fallback process tree (pid ${pid}).`;
|
|
46
|
+
}
|
|
47
|
+
catch {
|
|
48
|
+
terminateChildTree(child, signal);
|
|
49
|
+
return `Sent ${signal} to child_process pipe fallback session.`;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
/** Whether process group `pgid` has a member. Only ESRCH means empty: EPERM still names a live group. */
|
|
53
|
+
function processGroupHasMembers(pgid) {
|
|
54
|
+
try {
|
|
55
|
+
process.kill(-pgid, 0);
|
|
56
|
+
return true;
|
|
57
|
+
}
|
|
58
|
+
catch (error) {
|
|
59
|
+
return !(error instanceof Error && "code" in error && error.code === "ESRCH");
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* The process group of a session that settled while processes the command started were still in it
|
|
64
|
+
* (`cmd &`, a daemon, the children of a shell killed from outside). Settling does not kill them
|
|
65
|
+
* (senpi#6201); this keeps them reachable from kill(), terminate() and registry teardown. It is keyed on
|
|
66
|
+
* the group and remembered only when the group still had members at the settle.
|
|
67
|
+
*
|
|
68
|
+
* The id must never be signalled once it could name someone else's group. POSIX does not hand an id out as
|
|
69
|
+
* a pid while a process group with that id exists, so it can only be reused after the group empties. An
|
|
70
|
+
* armed record therefore probes on its own every `LEFTOVER_PROBE_MS` (unref'd: it never keeps the process
|
|
71
|
+
* alive) and disarms for good the first time the group is seen empty, from the probe or from any stop
|
|
72
|
+
* path, which also clears the probe. A stranger could only be hit if the pid space wrapped (99999 on macOS,
|
|
73
|
+
* pid_max on Linux) and handed out exactly this id as a new group leader within one probe interval after
|
|
74
|
+
* the last member exited: not a realistic window.
|
|
75
|
+
*/
|
|
76
|
+
export class LeftoverProcessGroup {
|
|
77
|
+
#pgid;
|
|
78
|
+
#probe;
|
|
79
|
+
#hasMembers;
|
|
80
|
+
#probeIntervalMs;
|
|
81
|
+
constructor(options = {}) {
|
|
82
|
+
this.#hasMembers = options.hasMembers ?? processGroupHasMembers;
|
|
83
|
+
this.#probeIntervalMs = options.probeIntervalMs ?? LEFTOVER_PROBE_MS;
|
|
84
|
+
}
|
|
85
|
+
/** The group still remembered, without probing it; undefined once disarmed. */
|
|
86
|
+
get armedGroup() {
|
|
87
|
+
return this.#pgid;
|
|
88
|
+
}
|
|
89
|
+
/** Called once at the settle. */
|
|
90
|
+
arm(pgid) {
|
|
91
|
+
if (pgid === undefined || !this.#hasMembers(pgid))
|
|
92
|
+
return;
|
|
93
|
+
this.#pgid = pgid;
|
|
94
|
+
this.#probe = setInterval(() => this.alive(), this.#probeIntervalMs);
|
|
95
|
+
this.#probe.unref?.();
|
|
96
|
+
}
|
|
97
|
+
alive() {
|
|
98
|
+
if (this.#pgid === undefined)
|
|
99
|
+
return false;
|
|
100
|
+
if (this.#hasMembers(this.#pgid))
|
|
101
|
+
return true;
|
|
102
|
+
this.#disarm();
|
|
103
|
+
return false;
|
|
104
|
+
}
|
|
105
|
+
#disarm() {
|
|
106
|
+
this.#pgid = undefined;
|
|
107
|
+
clearInterval(this.#probe);
|
|
108
|
+
this.#probe = undefined;
|
|
109
|
+
}
|
|
110
|
+
/** Signal the group while it has members; the pgid signalled, or undefined when nothing is left. */
|
|
111
|
+
signal(signal) {
|
|
112
|
+
const pgid = this.#pgid;
|
|
113
|
+
if (pgid === undefined || !this.alive())
|
|
114
|
+
return undefined;
|
|
115
|
+
try {
|
|
116
|
+
process.kill(-pgid, signal);
|
|
117
|
+
return pgid;
|
|
118
|
+
}
|
|
119
|
+
catch {
|
|
120
|
+
// ESRCH: the last member went between the probe and the signal.
|
|
121
|
+
return undefined;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
/** Wait up to `graceMs` for the group to empty; true once nothing is left. */
|
|
125
|
+
async waitGone(graceMs) {
|
|
126
|
+
const deadline = Date.now() + graceMs;
|
|
127
|
+
while (this.alive()) {
|
|
128
|
+
if (Date.now() >= deadline)
|
|
129
|
+
return false;
|
|
130
|
+
await new Promise((resolve) => setTimeout(resolve, GROUP_POLL_MS));
|
|
131
|
+
}
|
|
132
|
+
return true;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* TERM, a bounded grace, then KILL for whatever a settled session left in its process group: the same
|
|
137
|
+
* escalation a live session gets. A backend without a leftover group (native, Bun) makes this a no-op.
|
|
138
|
+
*/
|
|
139
|
+
export async function stopLeftoverGroup(handle, signal, graceMs, forcedGraceMs) {
|
|
140
|
+
if (handle.leftoverAlive?.() !== true)
|
|
141
|
+
return;
|
|
142
|
+
handle.kill(signal);
|
|
143
|
+
if ((await handle.waitLeftoverGone?.(graceMs)) !== false)
|
|
144
|
+
return;
|
|
145
|
+
handle.kill("SIGKILL");
|
|
146
|
+
await handle.waitLeftoverGone?.(forcedGraceMs);
|
|
147
|
+
}
|
|
148
|
+
//# sourceMappingURL=process-tree.js.map
|
package/dist/registry-types.d.ts
CHANGED
|
@@ -22,6 +22,8 @@ export interface SessionRegistrySession {
|
|
|
22
22
|
readonly stop?: () => MaybePromise<unknown>;
|
|
23
23
|
readonly kill?: (signal?: TerminalSessionSignal) => MaybePromise<unknown>;
|
|
24
24
|
readonly signal?: (signal: TerminalSessionSignal) => MaybePromise<unknown>;
|
|
25
|
+
/** Stops what the session left in its process group after it settled (TerminalSession, senpi#6201). */
|
|
26
|
+
readonly stopLeftover?: () => Promise<unknown>;
|
|
25
27
|
readonly waitExit?: () => Promise<unknown>;
|
|
26
28
|
readonly wait?: () => Promise<unknown>;
|
|
27
29
|
}
|
package/dist/registry.js
CHANGED
|
@@ -73,8 +73,11 @@ export class SessionRegistry {
|
|
|
73
73
|
continue;
|
|
74
74
|
await this.cleanupDetachedChildren(entry);
|
|
75
75
|
sweptIds.push(entry.id);
|
|
76
|
-
if (options.remove)
|
|
77
|
-
|
|
76
|
+
if (!options.remove)
|
|
77
|
+
continue;
|
|
78
|
+
// Forgetting the session must not strand what it left running in its process group (senpi#6201).
|
|
79
|
+
await entry.session.stopLeftover?.();
|
|
80
|
+
this.removeEntry(entry.id);
|
|
78
81
|
}
|
|
79
82
|
return sweptIds;
|
|
80
83
|
}
|
|
@@ -151,6 +154,7 @@ export class SessionRegistry {
|
|
|
151
154
|
if (!entry)
|
|
152
155
|
return;
|
|
153
156
|
await this.cleanupDetachedChildren(entry);
|
|
157
|
+
await entry.session.stopLeftover?.();
|
|
154
158
|
this.removeEntry(entry.id);
|
|
155
159
|
}
|
|
156
160
|
}
|
|
@@ -176,6 +180,7 @@ export class SessionRegistry {
|
|
|
176
180
|
else
|
|
177
181
|
await this.forceKillEntry(entry);
|
|
178
182
|
}
|
|
183
|
+
await entry.session.stopLeftover?.();
|
|
179
184
|
return true;
|
|
180
185
|
}
|
|
181
186
|
/**
|
package/dist/session-exit.d.ts
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import type { PipeFallbackOperationResult } from "./pipe-fallback.ts";
|
|
2
2
|
import type { TerminalSessionBackend, TerminalSessionExit, TerminalSessionOperationResult } from "./session-types.ts";
|
|
3
|
+
/** A grace option as a usable wait: unset, non-finite or negative values take the fallback. */
|
|
4
|
+
export declare function normalizeGraceMs(value: number | undefined, fallback: number): number;
|
|
5
|
+
/** Await a session's exit for at most `graceMs`; `null` means it did not settle in time (or never started). */
|
|
6
|
+
export declare function settledExitWithin(exitPromise: Promise<TerminalSessionExit> | null, graceMs: number): Promise<TerminalSessionExit | null>;
|
|
3
7
|
export declare function normalizeTerminalExit(exit: unknown, backend: TerminalSessionBackend, killRequested: boolean): TerminalSessionExit;
|
|
4
8
|
export declare function normalizeOperationResult(result: TerminalSessionOperationResult | PipeFallbackOperationResult | undefined, defaultNote: string): TerminalSessionOperationResult;
|
|
5
9
|
export declare function normalizeUnknownOperationResult(result: unknown): TerminalSessionOperationResult | undefined;
|
package/dist/session-exit.js
CHANGED
|
@@ -1,3 +1,29 @@
|
|
|
1
|
+
/** A grace option as a usable wait: unset, non-finite or negative values take the fallback. */
|
|
2
|
+
export function normalizeGraceMs(value, fallback) {
|
|
3
|
+
if (value === undefined || !Number.isFinite(value) || value < 0)
|
|
4
|
+
return fallback;
|
|
5
|
+
return value;
|
|
6
|
+
}
|
|
7
|
+
/** Await a session's exit for at most `graceMs`; `null` means it did not settle in time (or never started). */
|
|
8
|
+
export async function settledExitWithin(exitPromise, graceMs) {
|
|
9
|
+
if (exitPromise === null || graceMs <= 0)
|
|
10
|
+
return null;
|
|
11
|
+
let graceTimer;
|
|
12
|
+
try {
|
|
13
|
+
return await Promise.race([
|
|
14
|
+
// Attaching the catch here also keeps a late backend rejection from
|
|
15
|
+
// surfacing as an unhandled rejection once the grace timer wins.
|
|
16
|
+
exitPromise.catch(() => null),
|
|
17
|
+
new Promise((resolve) => {
|
|
18
|
+
graceTimer = setTimeout(() => resolve(null), graceMs);
|
|
19
|
+
}),
|
|
20
|
+
]);
|
|
21
|
+
}
|
|
22
|
+
finally {
|
|
23
|
+
if (graceTimer !== undefined)
|
|
24
|
+
clearTimeout(graceTimer);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
1
27
|
export function normalizeTerminalExit(exit, backend, killRequested) {
|
|
2
28
|
if (backend === "pipe-fallback" && isPipeFallbackSessionExit(exit)) {
|
|
3
29
|
return normalizePipeFallbackExit(exit, killRequested);
|
package/dist/session-types.d.ts
CHANGED
|
@@ -64,6 +64,9 @@ export interface TerminalSessionHandle {
|
|
|
64
64
|
readonly wait?: () => Promise<unknown>;
|
|
65
65
|
readonly pid?: number;
|
|
66
66
|
readonly processGroupId?: number;
|
|
67
|
+
/** After the settle, whether processes the command started are still in its process group (pipe fallback only). */
|
|
68
|
+
readonly leftoverAlive?: () => boolean;
|
|
69
|
+
readonly waitLeftoverGone?: (graceMs: number) => Promise<boolean>;
|
|
67
70
|
}
|
|
68
71
|
export type CreateNativeTerminalSession = (options: TerminalSessionNativeOptions, onData: TerminalSessionDataHandler) => TerminalSessionHandle;
|
|
69
72
|
export interface TerminalSessionDependencies {
|
package/dist/session.d.ts
CHANGED
|
@@ -46,10 +46,14 @@ export declare class TerminalSession {
|
|
|
46
46
|
* never started).
|
|
47
47
|
*/
|
|
48
48
|
terminate(options?: TerminalSessionTerminateOptions): Promise<TerminalSessionExit | null>;
|
|
49
|
+
/**
|
|
50
|
+
* TERM, grace, KILL for what a settled session left in its process group; a no-op while it runs, once the
|
|
51
|
+
* group is empty, and on backends that keep no leftover group (native, Bun).
|
|
52
|
+
*/
|
|
53
|
+
stopLeftover(options?: TerminalSessionTerminateOptions): Promise<void>;
|
|
49
54
|
waitExit(): Promise<TerminalSessionExit>;
|
|
50
55
|
private waitBackendExit;
|
|
51
|
-
|
|
52
|
-
private waitExitWithin;
|
|
56
|
+
private exitWithin;
|
|
53
57
|
private settleExit;
|
|
54
58
|
private emitData;
|
|
55
59
|
}
|
package/dist/session.js
CHANGED
|
@@ -2,8 +2,9 @@ import { Buffer } from "node:buffer";
|
|
|
2
2
|
import process from "node:process";
|
|
3
3
|
import { loadNativePty } from "./native-loader.js";
|
|
4
4
|
import { PipeFallbackSession, shouldUsePipeFallback } from "./pipe-fallback.js";
|
|
5
|
+
import { stopLeftoverGroup } from "./process-tree.js";
|
|
5
6
|
import { createBunTerminalSession, isBunTerminalEnabled } from "./session-bun.js";
|
|
6
|
-
import { exitedOperation, normalizeOperationResult, normalizeTerminalExit, notStartedOperation, } from "./session-exit.js";
|
|
7
|
+
import { exitedOperation, normalizeGraceMs, normalizeOperationResult, normalizeTerminalExit, notStartedOperation, settledExitWithin, } from "./session-exit.js";
|
|
7
8
|
import { getNativeSessionFactory } from "./session-native.js";
|
|
8
9
|
import { defaultCommand, normalizeRawTailBytes, toNativeOptions, toPipeFallbackOptions } from "./session-options.js";
|
|
9
10
|
import { RawOutputTail } from "./session-raw-tail.js";
|
|
@@ -145,6 +146,9 @@ export class TerminalSession {
|
|
|
145
146
|
kill(signal = "SIGTERM") {
|
|
146
147
|
const handle = this.backendHandle;
|
|
147
148
|
if (this.settledExit !== null) {
|
|
149
|
+
// Settled is not stopped: what the command left in its process group stays reachable (senpi#6201).
|
|
150
|
+
if (handle?.leftoverAlive?.() === true)
|
|
151
|
+
return normalizeOperationResult(handle.kill(signal), `Sent ${signal}.`);
|
|
148
152
|
return {
|
|
149
153
|
ok: true,
|
|
150
154
|
idempotent: true,
|
|
@@ -177,16 +181,32 @@ export class TerminalSession {
|
|
|
177
181
|
* never started).
|
|
178
182
|
*/
|
|
179
183
|
async terminate(options = {}) {
|
|
180
|
-
if (this.settledExit !== null)
|
|
184
|
+
if (this.settledExit !== null) {
|
|
185
|
+
await this.stopLeftover(options);
|
|
181
186
|
return this.settledExit;
|
|
187
|
+
}
|
|
182
188
|
if (this.backendHandle === null)
|
|
183
189
|
return null;
|
|
184
190
|
this.kill(options.signal ?? "SIGTERM");
|
|
185
|
-
const graceful = await this.
|
|
186
|
-
if (graceful !== null)
|
|
191
|
+
const graceful = await this.exitWithin(normalizeGraceMs(options.graceMs, DEFAULT_TERMINATE_GRACE_MS));
|
|
192
|
+
if (graceful !== null) {
|
|
193
|
+
await this.stopLeftover(options);
|
|
187
194
|
return graceful;
|
|
195
|
+
}
|
|
188
196
|
this.kill(FORCE_SIGNAL);
|
|
189
|
-
return await this.
|
|
197
|
+
return await this.exitWithin(normalizeGraceMs(options.forcedGraceMs, DEFAULT_FORCED_GRACE_MS));
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* TERM, grace, KILL for what a settled session left in its process group; a no-op while it runs, once the
|
|
201
|
+
* group is empty, and on backends that keep no leftover group (native, Bun).
|
|
202
|
+
*/
|
|
203
|
+
async stopLeftover(options = {}) {
|
|
204
|
+
const handle = this.backendHandle;
|
|
205
|
+
if (this.settledExit === null || handle === null)
|
|
206
|
+
return;
|
|
207
|
+
const graceMs = normalizeGraceMs(options.graceMs, DEFAULT_TERMINATE_GRACE_MS);
|
|
208
|
+
const forcedGraceMs = normalizeGraceMs(options.forcedGraceMs, DEFAULT_FORCED_GRACE_MS);
|
|
209
|
+
await stopLeftoverGroup(handle, options.signal ?? "SIGTERM", graceMs, forcedGraceMs);
|
|
190
210
|
}
|
|
191
211
|
async waitExit() {
|
|
192
212
|
if (this.settledExit !== null)
|
|
@@ -202,28 +222,10 @@ export class TerminalSession {
|
|
|
202
222
|
const exit = await wait.call(handle);
|
|
203
223
|
return normalizeTerminalExit(exit, backend, this.lastSignal !== null);
|
|
204
224
|
}
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
const exitPromise = this.exitPromise;
|
|
210
|
-
if (exitPromise === null || graceMs <= 0)
|
|
211
|
-
return this.settledExit;
|
|
212
|
-
let graceTimer;
|
|
213
|
-
try {
|
|
214
|
-
return await Promise.race([
|
|
215
|
-
// Attaching the catch here also keeps a late backend rejection from
|
|
216
|
-
// surfacing as an unhandled rejection once the grace timer wins.
|
|
217
|
-
exitPromise.catch(() => null),
|
|
218
|
-
new Promise((resolve) => {
|
|
219
|
-
graceTimer = setTimeout(() => resolve(null), graceMs);
|
|
220
|
-
}),
|
|
221
|
-
]);
|
|
222
|
-
}
|
|
223
|
-
finally {
|
|
224
|
-
if (graceTimer !== undefined)
|
|
225
|
-
clearTimeout(graceTimer);
|
|
226
|
-
}
|
|
225
|
+
exitWithin(graceMs) {
|
|
226
|
+
return this.settledExit !== null
|
|
227
|
+
? Promise.resolve(this.settledExit)
|
|
228
|
+
: settledExitWithin(this.exitPromise, graceMs);
|
|
227
229
|
}
|
|
228
230
|
settleExit(exit) {
|
|
229
231
|
if (this.settledExit !== null)
|
|
@@ -243,11 +245,6 @@ export class TerminalSession {
|
|
|
243
245
|
handler(buffer);
|
|
244
246
|
}
|
|
245
247
|
}
|
|
246
|
-
function normalizeGraceMs(value, fallback) {
|
|
247
|
-
if (value === undefined || !Number.isFinite(value) || value < 0)
|
|
248
|
-
return fallback;
|
|
249
|
-
return value;
|
|
250
|
-
}
|
|
251
248
|
export function createTerminalSession(options = {}, dependencies = {}) {
|
|
252
249
|
return new TerminalSession(options, dependencies).start();
|
|
253
250
|
}
|
|
Binary file
|
|
Binary file
|