Skip to content
Closed
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
4 changes: 4 additions & 0 deletions process/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# @effection/process

## \[0.8.2]

- Bound graceful Windows process shutdown before force-killing the process tree.

## \[2.1.4]

### Dependencies
Expand Down
2 changes: 1 addition & 1 deletion process/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@effectionx/process",
"description": "Spawn and manage child processes with structured concurrency",
"version": "0.8.1",
"version": "0.8.2",
"keywords": ["process"],
"type": "module",
"main": "./dist/mod.js",
Expand Down
26 changes: 18 additions & 8 deletions process/src/exec/win32.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import {
all,
createSignal,
ensure,
race,
sleep,
spawn,
withResolvers,
} from "effection";
Expand All @@ -29,6 +31,8 @@ import { unbox, useEvalScope } from "@effectionx/scope-eval";

type ProcessResultValue = [number?, string?];

const gracefulShutdownTimeout = 1000;

function* killTree(pid: number) {
try {
const killer = spawnProcess(
Expand Down Expand Up @@ -177,16 +181,22 @@ export function* createWin32Process(
stdin.end();
}

// depending on how we shutdown, this may already be closed and
// will pass immediately over the operations
yield* all([io.stdoutDone.operation, io.stderrDone.operation]);

if (pid && childProcess.exitCode === null) {
// If the process is still around after we've waited
// for stdout and stderr to close,
// then force kill the tree.
const closed = yield* race([
(function* () {
yield* processResult.operation;
return true;
})(),
(function* () {
yield* sleep(gracefulShutdownTimeout);
return false;
})(),
]);

if (pid && !closed) {
yield* killTree(pid);
}

yield* all([io.stdoutDone.operation, io.stderrDone.operation]);
});

return {
Expand Down
23 changes: 23 additions & 0 deletions process/test/exec.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,29 @@ describe("exec", () => {
expect(status).toBeUndefined();
});
});

if (process.platform === "win32") {
it("force kills the process tree when graceful shutdown does not close stdio", function* () {
const ready = withResolvers<Process>();
const task = yield* spawn(function* () {
const proc = yield* exec("node", {
arguments: [
"--experimental-strip-types",
"fixtures/ignore-shutdown.ts",
],
cwd: import.meta.dirname,
});
ready.resolve(proc);
yield* proc.join();
});

const proc = yield* ready.operation;
const started = yield* expectMatch(/ready/, lines()(proc.stdout));
expect(started).toBe(true);

expect(yield* task.halt()).toBeUndefined();
});
}
});

describe("successfully", () => {
Expand Down
24 changes: 24 additions & 0 deletions process/test/fixtures/ignore-shutdown.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { spawn } from "node:child_process";
import process from "node:process";

const descendant = spawn(
process.execPath,
["-e", 'process.on("SIGINT", () => {}); setInterval(() => {}, 1000);'],
{
stdio: ["ignore", "inherit", "inherit"],
windowsHide: true,
},
);

descendant.once("spawn", () => {
console.log("ready");
});

descendant.once("error", (error) => {
console.error(error);
process.exitCode = 1;
});

process.on("SIGINT", () => {});
process.stdin.resume();
setInterval(() => {}, 1000);
Loading