| 1 | //go:build windows |
| 2 | |
| 3 | package engine |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "os/exec" |
| 8 | "time" |
| 9 | ) |
| 10 | |
| 11 | // setProcessGroup is a no-op on Windows: syscall.SysProcAttr has no Setpgid |
| 12 | // field and process groups are a POSIX concept. The timeout path falls back |
| 13 | // to killing the direct child. |
| 14 | func setProcessGroup(cmd *exec.Cmd) { |
| 15 | } |
| 16 | |
| 17 | // killProcessGroup kills only the direct child on Windows. TerminateProcess |
| 18 | // does not run Python signal or atexit handlers, so descendant cleanup is |
| 19 | // not guaranteed on this platform. |
| 20 | func killProcessGroup(cmd *exec.Cmd) { |
| 21 | if cmd.Process == nil { |
| 22 | return |
| 23 | } |
| 24 | _ = cmd.Process.Kill() |
| 25 | } |
| 26 | |
| 27 | // termProcessGroup kills the direct child on Windows: there is no |
| 28 | // SIGTERM equivalent, so the TERM and KILL phases collapse into the same |
| 29 | // action and the grace wait in Run is skipped on prompt exit. |
| 30 | func termProcessGroup(cmd *exec.Cmd) { |
| 31 | if cmd.Process == nil { |
| 32 | return |
| 33 | } |
| 34 | _ = cmd.Process.Kill() |
| 35 | } |
| 36 | |
| 37 | func waitCommand(ctx context.Context, cmd *exec.Cmd, grace time.Duration) error { |
| 38 | return waitCommandWithDrain(ctx, cmd, grace, pipeGracePeriod, nil, nil) |
| 39 | } |
| 40 | |
| 41 | func waitCommandWithDrain( |
| 42 | ctx context.Context, |
| 43 | cmd *exec.Cmd, |
| 44 | grace, pipeGrace time.Duration, |
| 45 | drainDone <-chan struct{}, |
| 46 | closeReadPipes func(), |
| 47 | ) error { |
| 48 | drainDone, closeReadPipes = normalizeDrain(drainDone, closeReadPipes) |
| 49 | waitCh := make(chan error, 1) |
| 50 | go func() { waitCh <- cmd.Wait() }() |
| 51 | |
| 52 | select { |
| 53 | case err := <-waitCh: |
| 54 | drainTimedOut := finishDrain(drainDone, closeReadPipes, pipeGrace) |
| 55 | if err == nil && drainTimedOut { |
| 56 | return exec.ErrWaitDelay |
| 57 | } |
| 58 | return err |
| 59 | case <-ctx.Done(): |
| 60 | termProcessGroup(cmd) |
| 61 | timer := time.NewTimer(grace) |
| 62 | defer timer.Stop() |
| 63 | var err error |
| 64 | select { |
| 65 | case err = <-waitCh: |
| 66 | case <-timer.C: |
| 67 | killProcessGroup(cmd) |
| 68 | err = <-waitCh |
| 69 | } |
| 70 | finishDrain(drainDone, closeReadPipes, pipeGrace) |
| 71 | return err |
| 72 | } |
| 73 | } |
| 74 |