Skip to content

Commit 4f9ce37

Browse files
committed
fix(acp): reuse Windows cleanup before runtime replacement
1 parent 8ba196d commit 4f9ce37

2 files changed

Lines changed: 48 additions & 9 deletions

File tree

‎apps/server/src/provider/acp/AcpJsonRpcConnection.test.ts‎

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import * as Fiber from "effect/Fiber";
1717
import * as Scope from "effect/Scope";
1818
import * as Result from "effect/Result";
1919
import * as Stream from "effect/Stream";
20-
import { ChildProcessSpawner } from "effect/process";
20+
import { ChildProcess, ChildProcessSpawner } from "effect/process";
2121
import { describe, expect } from "vite-plus/test";
2222

2323
import * as AcpSessionRuntime from "@t3tools/provider-acp/server/AcpSessionRuntime";
@@ -297,6 +297,45 @@ describe("AcpSessionRuntime", () => {
297297
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
298298
);
299299

300+
it.effect.each([0, 1])("reuses Windows finalizer cleanup result %s before replacement", (code) =>
301+
Effect.gen(function* () {
302+
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
303+
let kills = 0;
304+
const runtime = yield* AcpSessionRuntime.make({
305+
...mockRuntimeOptions,
306+
ownDetachedProcessGroup: true,
307+
processGroupPlatform: "win32",
308+
}).pipe(
309+
Effect.provideService(HostProcess.Platform, "win32"),
310+
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, {
311+
...spawner,
312+
spawn: (command) => {
313+
if (!ChildProcess.isStandardCommand(command) || command.command !== "taskkill") {
314+
return spawner.spawn(command);
315+
}
316+
return spawner.spawn(
317+
ChildProcess.make(process.execPath, [
318+
"-e",
319+
`process.exit(${kills++ === 0 ? code : 128})`,
320+
]),
321+
);
322+
},
323+
}),
324+
Effect.scoped,
325+
);
326+
expect(kills).toBe(1);
327+
if (runtime.terminateProcessGroup === undefined) return expect.fail("Missing ACP teardown");
328+
if (code === 0) yield* runtime.terminateProcessGroup;
329+
else {
330+
expect(yield* Effect.flip(runtime.terminateProcessGroup)).toMatchObject({
331+
_tag: "AcpProcessGroupTerminationError",
332+
exitCode: code,
333+
});
334+
}
335+
expect(kills).toBe(1);
336+
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
337+
);
338+
300339
it.effect.each(["session/new", "session/resume"] as const)(
301340
"buffers root metadata while %s startup is still pending",
302341
(setupMethod) =>

‎packages/provider-acp/src/server/AcpSessionRuntime.ts‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1561,12 +1561,14 @@ export const make = Effect.fn("AcpSessionRuntime.make")(function* (
15611561
return cause?.code === "ESRCH" ? Effect.succeed(false) : Effect.fail(error);
15621562
}),
15631563
);
1564+
const terminateWindowsProcessTreeWithTaskkillOnce = yield* Effect.cached(
1565+
terminateWindowsProcessTreeWithTaskkill(Number(child.pid)).pipe(
1566+
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
1567+
Effect.uninterruptible,
1568+
),
1569+
);
15641570
const terminateWindowsProcessTree =
1565-
options.windowsProcessTreeTerminator ??
1566-
((pid: number) =>
1567-
terminateWindowsProcessTreeWithTaskkill(pid).pipe(
1568-
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
1569-
));
1571+
options.windowsProcessTreeTerminator ?? (() => terminateWindowsProcessTreeWithTaskkillOnce);
15701572
const terminatePosixProcessTree = Effect.fn("AcpSessionRuntime.terminatePosixProcessTree")(
15711573
function* (grace: Duration.Input, platformOverride?: NodeJS.Platform) {
15721574
const platform = platformOverride ?? options.processGroupPlatform;
@@ -1639,9 +1641,7 @@ export const make = Effect.fn("AcpSessionRuntime.make")(function* (
16391641
scopeUnit !== undefined
16401642
? terminateScopedProcess
16411643
: hostPlatform === "win32"
1642-
? terminateWindowsProcessTreeWithTaskkill(Number(child.pid)).pipe(
1643-
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
1644-
)
1644+
? terminateWindowsProcessTreeWithTaskkillOnce
16451645
: linuxCgroupLease !== undefined
16461646
? terminateLinuxCgroupLease(linuxCgroupLease)
16471647
: options.ownDescendantProcessGroups === true

0 commit comments

Comments
 (0)