Skip to content

Commit 777809a

Browse files
[containers-shared] Fix Docker spawn opening console windows on Windows
On Windows, `detached: true` in child_process.spawn() gives each child process its own visible console window. This caused many windows to flash open during `wrangler deploy` with `[[containers]]` configured. - Set `detached` conditionally: true on Unix (for process group cleanup), false on Windows (to prevent console windows) - Add `windowsHide: true` to further suppress console windows on Windows - Guard `process.kill(-pid)` abort logic: use `child.kill()` on Windows since negative-PID process group kill is Unix-only Fixes #12963
1 parent 38991ec commit 777809a

File tree

3 files changed

+41
-10
lines changed

3 files changed

+41
-10
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
fix: prevent Docker container builds from spawning console windows on Windows
6+
7+
On Windows, `detached: true` in `child_process.spawn()` gives each child process its own visible console window, causing many windows to flash open during `wrangler deploy` with `[[containers]]`. The `detached` option is now only set on non-Windows platforms (where it is needed for process group cleanup), and `windowsHide: true` is added to further suppress console windows on Windows.

packages/containers-shared/src/build.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,14 @@ export function dockerBuild(
5858
const child = spawn(dockerPath, options.buildCmd, {
5959
stdio: ["pipe", "inherit", "inherit"],
6060
// We need to set detached to true so that the child process
61-
// will control all of its child processed and we can kill
62-
// all of them in case we need to abort the build process
63-
detached: true,
61+
// will control all of its child processes and we can kill
62+
// all of them in case we need to abort the build process.
63+
// On Windows, detached: true opens a new console window per child
64+
// process, so we only set it on non-Windows platforms.
65+
detached: process.platform !== "win32",
66+
// Prevent child processes from opening visible console windows on Windows.
67+
// This is a no-op on non-Windows platforms.
68+
windowsHide: true,
6469
});
6570
if (child.stdin !== null) {
6671
child.stdin.write(options.dockerfile);
@@ -85,8 +90,15 @@ export function dockerBuild(
8590
abort: () => {
8691
child.unref();
8792
if (child.pid !== undefined) {
88-
// kill run on the negative PID kills the whole group controlled by the child process
89-
process.kill(-child.pid);
93+
if (process.platform === "win32") {
94+
// On Windows, negative-PID process group kill is not supported.
95+
// Kill the child process directly instead.
96+
child.kill();
97+
} else {
98+
// Kill using the negative PID to terminate the whole process group
99+
// controlled by the child process.
100+
process.kill(-child.pid);
101+
}
90102
}
91103
},
92104
ready,

packages/containers-shared/src/utils.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,14 @@ export const runDockerCmd = (
2626
const child = spawn(dockerPath, args, {
2727
stdio: stdio ?? "inherit",
2828
// We need to set detached to true so that the child process
29-
// will control all of its child processed and we can kill
30-
// all of them in case we need to abort the build process
31-
detached: true,
29+
// will control all of its child processes and we can kill
30+
// all of them in case we need to abort the build process.
31+
// On Windows, detached: true opens a new console window per child
32+
// process, so we only set it on non-Windows platforms.
33+
detached: process.platform !== "win32",
34+
// Prevent child processes from opening visible console windows on Windows.
35+
// This is a no-op on non-Windows platforms.
36+
windowsHide: true,
3237
});
3338
let errorHandled = false;
3439

@@ -51,8 +56,15 @@ export const runDockerCmd = (
5156
aborted = true;
5257
child.unref();
5358
if (child.pid !== undefined) {
54-
// kill run on the negative PID kills the whole group controlled by the child process
55-
process.kill(-child.pid);
59+
if (process.platform === "win32") {
60+
// On Windows, negative-PID process group kill is not supported.
61+
// Kill the child process directly instead.
62+
child.kill();
63+
} else {
64+
// Kill using the negative PID to terminate the whole process group
65+
// controlled by the child process.
66+
process.kill(-child.pid);
67+
}
5668
}
5769
},
5870
ready,

0 commit comments

Comments
 (0)