Skip to content
Myr

Terminate child processes on Windows (#4723)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Windows doesn't have signals, the default Kill doesn't actually kill anything, and other forms of termination don't allow cleanup. So we spawn preview processes in a new process group and send them a CTRL_BREAK_EVENT to terminate. However, we only do this for "pwsh" (PowerShell 7+) and unknown/ posix-ish shells, because cmd.exe and Windows PowerShell ("powershell.exe") don't always exit on Ctrl-Break. pwsh also needs the -NonInteractive flag to exit on Ctrl-Break. If the process wasn't given its own group, or if sending the console control event fails, we fall back to the standard Kill (which likely won't help, but doesn't hurt to try). Fix #3134

authored by PJ Eby · committed by GitHub · parent d198c0c · Browse files · 1 file+44 added−7 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+44 added−7 removed

  • other:+44 added−7 removed

1 file+44 added−7 removed

modified:src/util/util_windows.go+44 added-7 removed
9 unmodified lines
10 "regexp"11 "strings"12 "sync/atomic"13 "syscall"1415 "golang.org/x/sys/windows"16)1718type shellType int1920const (21 shellTypeUnknown shellType = iota22 shellTypeCmd23 shellTypePowerShell24 shellTypePwsh25)2627var escapeRegex = regexp.MustCompile(`[&|<>()^%!"]`)28
19 unmodified lines
48 args = args[1:]49 } else if strings.HasPrefix(basename, "cmd") {50 shellType = shellTypeCmd51 args = []string{"/s/c"}49 } else if strings.HasPrefix(basename, "pwsh") || strings.HasPrefix(basename, "powershell") {52 } else if strings.HasPrefix(basename, "pwsh") {53 shellType = shellTypePwsh54 args = []string{"-NoProfile", "-Command"}55 } else if strings.HasPrefix(basename, "powershell") {56 shellType = shellTypePowerShell57 args = []string{"-NoProfile", "-Command"}58 } else {59 args = []string{"-c"}
1 unmodified line
61 return &Executor{shell: shell, shellType: shellType, args: args}62}6364// ExecCommand executes the given command with $SHELL59// FIXME: setpgid is unused. We set it in the Unix implementation so that we60// can kill preview process with its child processes at once.65//66// On Windows, setpgid controls whether the spawned process is placed in a new67// process group (so that it can be signaled independently, e.g. for previews).68// However, we only do this for "pwsh" and non-standard shells, because cmd.exe69// and Windows PowerShell ("powershell.exe") don't always exit on Ctrl-Break.70//71// NOTE: For "powershell", we should ideally set output encoding to UTF8,72// but it is left as is now because no adverse effect has been observed.73func (x *Executor) ExecCommand(command string, setpgid bool) *exec.Cmd {74 shell := x.shell
7 unmodified lines
82 }83 }84 x.shellPath.Store(shell)85 }8687 var creationFlags uint3288 // Set new process group for pwsh (PowerShell 7+) and unknown/posix-ish shells89 if setpgid && (x.shellType == shellTypePwsh || x.shellType == shellTypeUnknown) {90 creationFlags = windows.CREATE_NEW_PROCESS_GROUP91 }9293 var cmd *exec.Cmd94 if x.shellType == shellTypeCmd {95 cmd = exec.Command(shell)96 cmd.SysProcAttr = &syscall.SysProcAttr{97 HideWindow: false,98 CmdLine: fmt.Sprintf(`%s "%s"`, strings.Join(x.args, " "), command),82 CreationFlags: 0,99 CreationFlags: creationFlags,100 }101 } else {102 args := x.args103 if setpgid && x.shellType == shellTypePwsh {104 // pwsh needs -NonInteractive flag to exit on Ctrl-Break105 args = append([]string{"-NonInteractive"}, x.args...)106 }85 cmd = exec.Command(shell, append(x.args, command)...)107 cmd = exec.Command(shell, append(args, command)...)108 cmd.SysProcAttr = &syscall.SysProcAttr{109 HideWindow: false,88 CreationFlags: 0,110 CreationFlags: creationFlags,111 }112 }113 return cmd114}
62 unmodified lines
177 fd -H --no-ignore -td -d 4 | fzf --preview "eza --color=always --tree --level=3 --icons=always {}"178 fd -H --no-ignore -td -d 4 | fzf --preview ".\eza.exe --color=always --tree --level=3 --icons=always {}" --with-shell "powershell -NoProfile -Command"179 */180 return escapeArg(entry)159 case shellTypePowerShell:181 case shellTypePowerShell, shellTypePwsh:182 escaped := strings.ReplaceAll(entry, `"`, `\"`)183 return "'" + strings.ReplaceAll(escaped, "'", "''") + "'"184 default:185 return "'" + strings.ReplaceAll(entry, "'", "'\\''") + "'"
1 unmodified line
187}188189// KillCommand kills the process for the given command190func KillCommand(cmd *exec.Cmd) error {191 // Safely handle nil command or process.192 if cmd == nil || cmd.Process == nil {193 return nil194 }195 // If it has its own process group, we can send it Ctrl-Break196 if cmd.SysProcAttr != nil && cmd.SysProcAttr.CreationFlags&windows.CREATE_NEW_PROCESS_GROUP != 0 {197 if err := windows.GenerateConsoleCtrlEvent(windows.CTRL_BREAK_EVENT, uint32(cmd.Process.Pid)); err == nil {198 return nil199 }200 }201 // If it's the same process group, or if sending the console control event202 // fails (e.g., no console, different console, or process already exited),203 // fall back to a standard kill. This probably won't *help* if there's I/O204 // going on, because Wait() will still hang until the I/O finishes unless we205 // hard-kill the entire process group. But it doesn't hurt to try!206 return cmd.Process.Kill()207}208209// IsWindows returns true on Windows
17 unmodified lines