Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(task): kill descendants when killing task process on Windows #27163

Merged
Merged
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
85 changes: 51 additions & 34 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ nix = "=0.27.1"
# windows deps
junction = "=0.2.0"
winapi = "=0.3.9"
windows-sys = { version = "0.52.0", features = ["Win32_Foundation", "Win32_Media", "Win32_Storage_FileSystem", "Win32_System_IO", "Win32_System_WindowsProgramming", "Wdk", "Wdk_System", "Wdk_System_SystemInformation", "Win32_Security", "Win32_System_Pipes", "Wdk_Storage_FileSystem", "Win32_System_Registry", "Win32_System_Kernel"] }
windows-sys = { version = "0.59.0", features = ["Win32_Foundation", "Win32_Media", "Win32_Storage_FileSystem", "Win32_System_IO", "Win32_System_WindowsProgramming", "Wdk", "Wdk_System", "Wdk_System_SystemInformation", "Win32_Security", "Win32_System_Pipes", "Wdk_Storage_FileSystem", "Win32_System_Registry", "Win32_System_Kernel", "Win32_System_Threading", "Win32_UI", "Win32_UI_Shell"] }
winres = "=0.1.12"

[profile.release]
Expand Down
2 changes: 1 addition & 1 deletion cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ deno_path_util.workspace = true
deno_resolver.workspace = true
deno_runtime = { workspace = true, features = ["include_js_files_for_snapshotting"] }
deno_semver.workspace = true
deno_task_shell = "=0.20.1"
deno_task_shell = "=0.20.2"
deno_telemetry.workspace = true
deno_terminal.workspace = true
libsui = "0.5.0"
Expand Down
2 changes: 1 addition & 1 deletion ext/io/bi_pipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ pub fn bi_pipe_pair_raw(
&s,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
0,
std::ptr::null_mut(),
);
if hd2 == INVALID_HANDLE_VALUE {
return Err(io::Error::last_os_error());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"if": "windows",
"args": "run --check -A main.ts",
"output": "[WILDCARD]" // just ensure no failure
}
5 changes: 5 additions & 0 deletions tests/specs/task/kill_task_windows_kills_children/deno.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"tasks": {
"start": "deno run -A script.js"
}
}
75 changes: 75 additions & 0 deletions tests/specs/task/kill_task_windows_kills_children/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
class StdoutReader {
readonly #reader: ReadableStreamDefaultReader<string>;
text = "";

constructor(stream: ReadableStream<Uint8Array>) {
const textStream = stream.pipeThrough(new TextDecoderStream());
this.#reader = textStream.getReader();
}

[Symbol.dispose]() {
this.#reader.releaseLock();
}

async waitForText(waitingText: string) {
if (this.text.includes(waitingText)) {
return;
}

while (true) {
const { value, done } = await this.#reader.read();
if (value) {
this.text += value;
if (this.text.includes(waitingText)) {
break;
}
}
if (done) {
throw new Error("Did not find text: " + waitingText);
}
}
}
}

const command = new Deno.Command("deno", {
args: ["task", "start"],
stdout: "piped",
});

const child = command.spawn();

const reader = new StdoutReader(child.stdout!);
console.log("Waiting...");
await reader.waitForText("Ready");
console.log("Received.");
const pid = parseInt(reader.text.split("\n")[0], 10);
console.log("PID", pid);
// ensure this function works
if (!isPidAlive(child.pid)) {
throw new Error("Unexpected.");
}
if (!isPidAlive(pid)) {
throw new Error("Unexpected.");
}
child.kill();
// now the grandchild shouldn't be alive
if (isPidAlive(pid)) {
throw new Error("Unexpected.");
}

function isPidAlive(pid: number) {
const command = new Deno.Command("cmd", {
args: ["/c", `wmic process where processid=${pid} get processid`],
});

try {
const { stdout } = command.outputSync(); // Execute the command
const output = new TextDecoder().decode(stdout);

console.log("wmic output:", output.trim());
return output.includes(pid.toString());
} catch (error) {
console.error("Error checking PID:", error);
return false;
}
}
3 changes: 3 additions & 0 deletions tests/specs/task/kill_task_windows_kills_children/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
console.log(Deno.pid);
console.log("Ready");
setInterval(() => {}, 10_000); // stay alive