Skip to content

Commit 4977a6a

Browse files
committed
Use CREATE_NEW_CONSOLE.
1 parent 1c07edf commit 4977a6a

File tree

2 files changed

+47
-41
lines changed

2 files changed

+47
-41
lines changed

src/runtime/signal_windows_test.go

Lines changed: 44 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,16 @@ package runtime_test
55
import (
66
"bufio"
77
"bytes"
8-
"context"
98
"fmt"
109
"internal/testenv"
11-
"net"
1210
"os"
1311
"os/exec"
1412
"path/filepath"
1513
"runtime"
14+
"strconv"
1615
"strings"
1716
"syscall"
1817
"testing"
19-
"time"
2018
)
2119

2220
func TestVectoredHandlerDontCrashOnLibrary(t *testing.T) {
@@ -96,50 +94,60 @@ func TestCtrlHandler(t *testing.T) {
9694
t.Fatalf("failed to build go exe: %v\n%s", err, out)
9795
}
9896

99-
// udp socket for synchronization
100-
conn, err := net.ListenPacket("udp", "[::1]:0")
97+
// run test program
98+
cmd = exec.Command(exe)
99+
var stderr bytes.Buffer
100+
cmd.Stderr = &stderr
101+
outPipe, err := cmd.StdoutPipe()
101102
if err != nil {
102-
t.Fatalf("ListenPacket failed: %v", err)
103+
t.Fatalf("Failed to create stdout pipe: %v", err)
103104
}
104-
defer conn.Close()
105-
conn.SetDeadline(time.Now().Add(5 * time.Second))
106-
107-
ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Second)
108-
defer cancel()
105+
outReader := bufio.NewReader(outPipe)
109106

110-
// run test program, in a new command window
111-
cmd = exec.CommandContext(ctx, "cmd.exe", "/c", "start", "Test Command Window", "/wait", exe, conn.LocalAddr().String())
107+
// in a new command window
108+
const CREATE_NEW_CONSOLE = 0x00000010
109+
cmd.SysProcAttr = &syscall.SysProcAttr{
110+
CreationFlags: CREATE_NEW_CONSOLE,
111+
}
112112
if err := cmd.Start(); err != nil {
113113
t.Fatalf("Start failed: %v", err)
114114
}
115115

116-
// read pid of the test program
117-
// cmd.Process.Pid is the pid of cmd.exe, not test.exe
118-
// also ensures the test program is ready to receive signals
119-
var data [512]byte
120-
n, _, err := conn.ReadFrom(data[:])
121-
if err != nil {
122-
t.Fatalf("ReadFrom failed: %v", err)
123-
}
116+
errCh := make(chan error, 1)
117+
go func() {
118+
// wait for child to be ready to receive signals
119+
if line, err := outReader.ReadString('\n'); err != nil {
120+
errCh <- fmt.Errorf("could not read stdout: %w", err)
121+
return
122+
} else if strings.TrimSpace(line) != "ready" {
123+
errCh <- fmt.Errorf("unexpected message: %v", line)
124+
return
125+
}
124126

125-
// gracefully kill pid, this closes the command window
126-
err = exec.Command("taskkill.exe", "/pid", string(data[:n])).Run()
127-
if err != nil {
128-
t.Fatalf("failed to kill: %v", err)
129-
}
127+
// gracefully kill pid, this closes the command window
128+
if err := exec.Command("taskkill.exe", "/pid", strconv.Itoa(cmd.Process.Pid)).Run(); err != nil {
129+
errCh <- fmt.Errorf("failed to kill: %w", err)
130+
return
131+
}
130132

131-
// check child received, handled SIGTERM
132-
n, _, err = conn.ReadFrom(data[:])
133-
if err != nil {
134-
t.Fatalf("ReadFrom failed: %v", err)
135-
}
136-
if expected, got := syscall.SIGTERM.String(), string(data[:n]); expected != got {
137-
t.Fatalf("Expected '%s' got: %s", expected, got)
138-
}
133+
// check child received, handled SIGTERM
134+
if line, err := outReader.ReadString('\n'); err != nil {
135+
errCh <- fmt.Errorf("could not read stdout: %w", err)
136+
return
137+
} else if expected, got := syscall.SIGTERM.String(), strings.TrimSpace(line); expected != got {
138+
errCh <- fmt.Errorf("Expected '%s' got: %s", expected, got)
139+
return
140+
}
141+
142+
errCh <- nil
143+
}()
139144

140-
// check child exited gracefully (exit code 0, didn't timeout)
145+
if err := <-errCh; err != nil {
146+
t.Fatal(err)
147+
}
148+
// check child exited gracefully, did not timeout
141149
if err := cmd.Wait(); err != nil {
142-
t.Fatalf("Program exited with error: %v", err)
150+
t.Fatalf("Program exited with error: %v\n%s", err, &stderr)
143151
}
144152
}
145153

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
package main
22

33
import (
4-
"net"
4+
"fmt"
55
"os"
66
"os/signal"
7-
"strconv"
87
"time"
98
)
109

1110
func main() {
1211
c := make(chan os.Signal, 1)
1312
signal.Notify(c)
1413

15-
con, _ := net.Dial("udp", os.Args[1])
16-
con.Write([]byte(strconv.Itoa(os.Getpid())))
14+
fmt.Println("ready")
1715
sig := <-c
1816

1917
time.Sleep(time.Second)
20-
con.Write([]byte(sig.String()))
18+
fmt.Println(sig)
2119
}

0 commit comments

Comments
 (0)