|
1 | | -//go:build windows |
2 | | - |
3 | 1 | package runner |
4 | 2 |
|
5 | 3 | import ( |
6 | | - "fmt" |
7 | 4 | "io" |
8 | 5 | "os" |
9 | 6 | "os/exec" |
10 | 7 | "strconv" |
11 | 8 | "strings" |
12 | | - "syscall" |
| 9 | + "time" |
13 | 10 | ) |
14 | 11 |
|
15 | 12 | func (e *Engine) killCmd(cmd *exec.Cmd) (pid int, err error) { |
16 | 13 | pid = cmd.Process.Pid |
| 14 | + // https://stackoverflow.com/a/44551450 |
| 15 | + kill := exec.Command("TASKKILL", "/T", "/F", "/PID", strconv.Itoa(pid)) |
17 | 16 |
|
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. |
21 | 17 | 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()) |
33 | 22 | } |
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. |
38 | 25 | _, _ = cmd.Process.Wait() |
39 | | - |
40 | 26 | return pid, err |
41 | 27 | } |
42 | 28 |
|
43 | 29 | func (e *Engine) startCmd(cmd string) (*exec.Cmd, io.ReadCloser, io.ReadCloser, error) { |
44 | 30 | var err error |
45 | 31 |
|
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) |
48 | 34 | } |
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) |
59 | 36 | stderr, err := c.StderrPipe() |
60 | 37 | if err != nil { |
61 | | - return nil, nil, nil, fmt.Errorf("failed to create stderr pipe: %w", err) |
| 38 | + return nil, nil, nil, err |
62 | 39 | } |
63 | | - |
64 | 40 | stdout, err := c.StdoutPipe() |
65 | 41 | if err != nil { |
66 | | - return nil, nil, nil, fmt.Errorf("failed to create stdout pipe: %w", err) |
| 42 | + return nil, nil, nil, err |
67 | 43 | } |
68 | 44 |
|
69 | 45 | c.Stdout = os.Stdout |
70 | 46 | c.Stderr = os.Stderr |
71 | 47 |
|
72 | 48 | err = c.Start() |
73 | 49 | if err != nil { |
74 | | - return nil, nil, nil, fmt.Errorf("failed to start command: %w", err) |
| 50 | + return nil, nil, nil, err |
75 | 51 | } |
76 | | - |
77 | | - return c, stdout, stderr, nil |
| 52 | + return c, stdout, stderr, err |
78 | 53 | } |
0 commit comments