Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion apps/server/src/terminal/Layers/Manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -983,7 +983,10 @@ it.layer(

it.effect("escalates terminal shutdown to SIGKILL when process does not exit in time", () =>
Effect.gen(function* () {
const { manager, ptyAdapter } = yield* createManager(5, { processKillGraceMs: 10 });
const { manager, ptyAdapter } = yield* createManager(5, {
platform: "linux",
processKillGraceMs: 10,
});
yield* manager.open(openInput());
const process = ptyAdapter.processes[0];
expect(process).toBeDefined();
Expand All @@ -999,6 +1002,25 @@ it.layer(
}).pipe(Effect.provide(TestClock.layer())),
);

it.effect("kills the terminal without a signal on Windows", () =>
Effect.gen(function* () {
const { manager, ptyAdapter } = yield* createManager(5, {
platform: "win32",
subprocessInspector: () =>
Effect.succeed({ hasRunningSubprocess: false, childCommand: null }),
});
yield* manager.open(openInput());
const process = ptyAdapter.processes[0];
expect(process).toBeDefined();
if (!process) return;

yield* manager.close({ threadId: "thread-1" });
yield* waitFor(Effect.sync(() => process.killed));

expect(process.killSignals).toEqual([undefined]);
}),
);

it.effect("publishes closed events when terminals are explicitly closed", () =>
Effect.gen(function* () {
const { manager, getEvents } = yield* createManager();
Expand Down Expand Up @@ -1501,6 +1523,7 @@ it.layer(
Effect.gen(function* () {
const scope = yield* Scope.make("sequential");
const { manager, ptyAdapter } = yield* createManager(5, {
platform: "linux",
processKillGraceMs: 10,
}).pipe(Effect.provideService(Scope.Scope, scope));
yield* manager.open(openInput());
Expand Down
25 changes: 24 additions & 1 deletion apps/server/src/terminal/Layers/Manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class TerminalProcessSignalError extends Schema.TaggedErrorClass<TerminalProcess
{
message: Schema.String,
cause: Schema.optional(Schema.Defect()),
signal: Schema.Literals(["SIGTERM", "SIGKILL"]),
signal: Schema.optional(Schema.Literals(["SIGTERM", "SIGKILL"])),
},
) {}

Expand Down Expand Up @@ -1075,6 +1075,29 @@ export const makeTerminalManagerWithOptions = Effect.fn("makeTerminalManagerWith
threadId: string,
terminalId: string,
) {
if (platform === "win32") {
// node-pty throws on Windows whenever a signal is passed; a signal-less
// kill terminates every process attached to the pty's console
// (shell and child processes such as dev servers), so no escalation step.
yield* Effect.try({
try: () => process.kill(),
catch: (cause) =>
new TerminalProcessSignalError({
message: "Failed to kill terminal console process tree.",
cause,
}),
}).pipe(
Effect.catch((error) =>
Effect.logWarning("failed to kill terminal process", {
threadId,
terminalId,
error: error.message,
}),
),
);
return;
}

const terminated = yield* Effect.try({
try: () => process.kill("SIGTERM"),
catch: (cause) =>
Expand Down
Loading