fix(windows): launch commands on the interactive desktop

This commit is contained in:
cabbage committed 2026-10-27 18:35:29 +00:00
1 parent 30e7bb8545
commit 8d28925e44
20 files changed
+1036 -28

No files matched your search

@@ -13,6 +13,7 @@ import (
"fmt"
"os"
"strings"
"sync"
"syscall"
"time"
"unsafe"
@@ -41,6 +42,13 @@ var (
procFreeConsole = kernel32.NewProc("FreeConsole")
procGenerateCtrlEvent = kernel32.NewProc("GenerateConsoleCtrlEvent")
procSetCtrlHandler = kernel32.NewProc("SetConsoleCtrlHandler")
consoleControlMu sync.Mutex
ignoreOwnConsoleEvent = winapi.NewCallback(func(event uint32) uintptr {
if event == 0 || event == 1 {
return 1
} // CTRL_C_EVENT / CTRL_BREAK_EVENT.
return 0
})
)
func stdinLineEnding() []byte { return []byte{'\r', '\n'} }
@@ -851,16 +859,30 @@ func sendControlBreak(pid uint32) (bool, error) {
if pid == 0 {
return false, errors.New("command has no verified console PID")
}
// Console attachment is process-wide. The service fallback must serialize
// concurrent requests; the normal route uses a separate helper process.
consoleControlMu.Lock()
defer consoleControlMu.Unlock()
// A console-subsystem test/helper may have its own headless console. It
// must detach before attaching to the verified command's private console.
procFreeConsole.Call()
if result, _, err := procAttachConsole.Call(uintptr(pid)); result == 0 {
if err == syscall.Errno(0) {
err = syscall.GetLastError()
}
return false, err
}
defer procFreeConsole.Call()
// Prevent the service/helper itself from acting on the generated event.
procSetCtrlHandler.Call(0, 1)
defer procSetCtrlHandler.Call(0, 0)
// NULL/TRUE ignores only Ctrl+C, not the Ctrl+Break sent below. Install a
// real handler after attachment (which resets the handler table), then
// detach before removing it so our own broadcast cannot terminate us.
if result, _, err := procSetCtrlHandler.Call(ignoreOwnConsoleEvent, 1); result == 0 {
procFreeConsole.Call()
return false, fmt.Errorf("protect console signal helper: %w", err)
}
defer func() {
procFreeConsole.Call()
procSetCtrlHandler.Call(ignoreOwnConsoleEvent, 0)
}()
result, _, err := procGenerateCtrlEvent.Call(1 /* CTRL_BREAK_EVENT */, 0)
if result == 0 {
if err == syscall.Errno(0) {