Skip to content

Commit bdf5192

Browse files
committed
fix(server): stop Windows terminal processes when closing
1 parent 061543e commit bdf5192

2 files changed

Lines changed: 49 additions & 11 deletions

File tree

‎apps/server/src/terminal/NodePtyAdapter.test.ts‎

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,50 @@ const spawn = vi.fn(() => ({
2121

2222
vi.mock("node-pty", () => ({ spawn }));
2323

24-
const testLayer = NodePtyAdapter.layer.pipe(
25-
Layer.provide(
26-
Layer.mergeAll(
27-
NodeServices.layer,
28-
Layer.succeed(HostProcessPlatform, "win32"),
29-
Layer.succeed(HostProcessArchitecture, "x64"),
24+
const makeTestLayer = (platform: NodeJS.Platform = "win32") =>
25+
NodePtyAdapter.layer.pipe(
26+
Layer.provide(
27+
Layer.mergeAll(
28+
NodeServices.layer,
29+
Layer.succeed(HostProcessPlatform, platform),
30+
Layer.succeed(HostProcessArchitecture, "x64"),
31+
),
3032
),
31-
),
32-
);
33+
);
34+
35+
const testLayer = makeTestLayer();
36+
37+
for (const platform of ["win32", "linux", "darwin"] as const) {
38+
it.effect(`terminates through node-pty using ${platform} semantics`, () =>
39+
Effect.gen(function* () {
40+
const adapter = yield* PtyAdapter.PtyAdapter;
41+
const process = yield* adapter.spawn({
42+
shell: "test-shell",
43+
cwd: ".",
44+
cols: 80,
45+
rows: 24,
46+
env: {},
47+
});
48+
const nativeProcess = spawn.mock.results.at(-1)!.value;
49+
nativeProcess.kill.mockImplementation((signal?: string) => {
50+
if (platform === "win32" && signal) {
51+
throw new Error("Signals not supported on windows.");
52+
}
53+
});
54+
55+
process.kill("SIGTERM");
56+
process.kill("SIGKILL");
57+
process.kill();
58+
59+
assert.deepEqual(
60+
nativeProcess.kill.mock.calls,
61+
platform === "win32"
62+
? [[undefined], [undefined], [undefined]]
63+
: [["SIGTERM"], ["SIGKILL"], [undefined]],
64+
);
65+
}).pipe(Effect.provide(makeTestLayer(platform))),
66+
);
67+
}
3368

3469
it.effect("spawns through the public adapter with the provided host references", () =>
3570
Effect.gen(function* () {

‎apps/server/src/terminal/NodePtyAdapter.ts‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,11 @@ const ensureNodePtySpawnHelperExecutable = Effect.fn(function* () {
6969

7070
class NodePtyProcess implements PtyAdapter.PtyProcess {
7171
private readonly process: import("node-pty").IPty;
72+
private readonly platform: NodeJS.Platform;
7273

73-
constructor(process: import("node-pty").IPty) {
74+
constructor(process: import("node-pty").IPty, platform: NodeJS.Platform) {
7475
this.process = process;
76+
this.platform = platform;
7577
}
7678

7779
get pid(): number {
@@ -87,7 +89,8 @@ class NodePtyProcess implements PtyAdapter.PtyProcess {
8789
}
8890

8991
kill(signal?: string): void {
90-
this.process.kill(signal);
92+
// node-pty terminates the Windows process tree without a POSIX signal.
93+
this.process.kill(this.platform === "win32" ? undefined : signal);
9194
}
9295

9396
onData(callback: (data: string) => void): () => void {
@@ -164,7 +167,7 @@ export const make = Effect.fn("NodePtyAdapter.make")(function* (
164167
cause,
165168
}),
166169
});
167-
return new NodePtyProcess(ptyProcess);
170+
return new NodePtyProcess(ptyProcess, platform);
168171
}),
169172
});
170173
});

0 commit comments

Comments
 (0)