Skip to content

Commit 8f3f7c1

Browse files
committed
runtime: change TestCtrlHandler to run in ConPTY
Fixes #51602. Previous test would not run in a pseudo-console (ConPTY). New test avoids taskkill entirely by having the child request its own console window be closed. Verified that this runs locally (within a real console), over SSH (within a pseudo-console), and that it breaks if #41884 were reverted.
1 parent e475cf2 commit 8f3f7c1

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

src/runtime/signal_windows_test.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"os/exec"
1111
"path/filepath"
1212
"runtime"
13-
"strconv"
1413
"strings"
1514
"syscall"
1615
"testing"
@@ -113,18 +112,6 @@ func TestCtrlHandler(t *testing.T) {
113112
cmd.Wait()
114113
}()
115114

116-
// wait for child to be ready to receive signals
117-
if line, err := outReader.ReadString('\n'); err != nil {
118-
t.Fatalf("could not read stdout: %v", err)
119-
} else if strings.TrimSpace(line) != "ready" {
120-
t.Fatalf("unexpected message: %s", line)
121-
}
122-
123-
// gracefully kill pid, this closes the command window
124-
if err := exec.Command("taskkill.exe", "/pid", strconv.Itoa(cmd.Process.Pid)).Run(); err != nil {
125-
t.Fatalf("failed to kill: %v", err)
126-
}
127-
128115
// check child received, handled SIGTERM
129116
if line, err := outReader.ReadString('\n'); err != nil {
130117
t.Fatalf("could not read stdout: %v", err)

src/runtime/testdata/testwinsignal/main.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,32 @@ package main
22

33
import (
44
"fmt"
5+
"log"
56
"os"
67
"os/signal"
8+
"syscall"
79
"time"
810
)
911

1012
func main() {
1113
c := make(chan os.Signal, 1)
1214
signal.Notify(c)
1315

14-
fmt.Println("ready")
16+
kernel32 := syscall.NewLazyDLL("kernel32.dll")
17+
getConsoleWindow := kernel32.NewProc("GetConsoleWindow")
18+
hwnd, _, err := getConsoleWindow.Call()
19+
if hwnd == 0 {
20+
log.Fatal("no associated console: ", err)
21+
}
22+
23+
const _WM_CLOSE = 0x0010
24+
user32 := syscall.NewLazyDLL("user32.dll")
25+
postMessage := user32.NewProc("PostMessageW")
26+
ok, _, err := postMessage.Call(hwnd, _WM_CLOSE, 0, 0)
27+
if ok == 0 {
28+
log.Fatal("post message failed: ", err)
29+
}
30+
1531
sig := <-c
1632

1733
time.Sleep(time.Second)

0 commit comments

Comments
 (0)