Skip to content

Commit f4d163b

Browse files
authored
Revert "(fix): Remove double-kill bug in Windows process termination (#777) (…" (#857)
This reverts commit c206647.
1 parent c206647 commit f4d163b

File tree

1 file changed

+16
-41
lines changed

1 file changed

+16
-41
lines changed

runner/util_windows.go

Lines changed: 16 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,53 @@
1-
//go:build windows
2-
31
package runner
42

53
import (
6-
"fmt"
74
"io"
85
"os"
96
"os/exec"
107
"strconv"
118
"strings"
12-
"syscall"
9+
"time"
1310
)
1411

1512
func (e *Engine) killCmd(cmd *exec.Cmd) (pid int, err error) {
1613
pid = cmd.Process.Pid
14+
// https://stackoverflow.com/a/44551450
15+
kill := exec.Command("TASKKILL", "/T", "/F", "/PID", strconv.Itoa(pid))
1716

18-
// On Windows, SIGINT is not supported for process trees.
19-
// Windows uses different process termination mechanisms than Unix.
20-
// TASKKILL is the proper way to terminate process hierarchies on Windows.
2117
if e.config.Build.SendInterrupt {
22-
e.mainLog("send_interrupt is not supported on Windows, using TASKKILL instead")
23-
}
24-
25-
// Use TASKKILL to forcefully terminate the entire process tree
26-
e.mainDebug("sending TASKKILL to process tree")
27-
killCmd := exec.Command("TASKKILL", "/F", "/T", "/PID", strconv.Itoa(pid))
28-
29-
// Hide the console window for cleaner UX
30-
killCmd.SysProcAttr = &syscall.SysProcAttr{
31-
HideWindow: true,
32-
CreationFlags: 0x08000000, // CREATE_NO_WINDOW
18+
if err = kill.Run(); err != nil {
19+
return
20+
}
21+
time.Sleep(e.config.killDelay())
3322
}
34-
35-
err = killCmd.Run()
36-
37-
// Wait for the process to fully terminate and release resources
23+
err = kill.Run()
24+
// Wait releases any resources associated with the Process.
3825
_, _ = cmd.Process.Wait()
39-
4026
return pid, err
4127
}
4228

4329
func (e *Engine) startCmd(cmd string) (*exec.Cmd, io.ReadCloser, io.ReadCloser, error) {
4430
var err error
4531

46-
if !strings.Contains(cmd, ".exe") && !strings.Contains(cmd, ".bat") && !strings.Contains(cmd, ".cmd") {
47-
e.mainDebug("command may not be recognized as executable: %s", cmd)
32+
if !strings.Contains(cmd, ".exe") {
33+
e.runnerLog("CMD will not recognize non .exe file for execution, path: %s", cmd)
4834
}
49-
50-
// Use cmd.exe instead of PowerShell for better performance
51-
c := exec.Command("cmd", "/C", cmd)
52-
53-
// Hide the console window
54-
c.SysProcAttr = &syscall.SysProcAttr{
55-
HideWindow: true,
56-
CreationFlags: 0x08000000, // CREATE_NO_WINDOW
57-
}
58-
35+
c := exec.Command("powershell", cmd)
5936
stderr, err := c.StderrPipe()
6037
if err != nil {
61-
return nil, nil, nil, fmt.Errorf("failed to create stderr pipe: %w", err)
38+
return nil, nil, nil, err
6239
}
63-
6440
stdout, err := c.StdoutPipe()
6541
if err != nil {
66-
return nil, nil, nil, fmt.Errorf("failed to create stdout pipe: %w", err)
42+
return nil, nil, nil, err
6743
}
6844

6945
c.Stdout = os.Stdout
7046
c.Stderr = os.Stderr
7147

7248
err = c.Start()
7349
if err != nil {
74-
return nil, nil, nil, fmt.Errorf("failed to start command: %w", err)
50+
return nil, nil, nil, err
7551
}
76-
77-
return c, stdout, stderr, nil
52+
return c, stdout, stderr, err
7853
}

0 commit comments

Comments
 (0)