Skip to content

Commit f7f9c8f

Browse files
author
Bryan C. Mills
committed
runtime: allocate fewer bytes during TestEINTR
This will hopefully address the occasional "runtime: out of memory" failures observed on the openbsd-arm-jsing builder: https://build.golang.org/log/c296d866e5d99ba401b18c1a2ff3e4d480e5238c Also make the "spin" and "winch" loops concurrent instead of sequential to cut down the test's running time. Finally, change Block to coordinate by closing stdin instead of sending SIGINT. The SIGINT handler wasn't necessarily registered by the time the signal was sent. Updates #20400 Updates #39043 Change-Id: Ie12fc75b87e33847dc25a12edb4126db27492da6 Reviewed-on: https://go-review.googlesource.com/c/go/+/234538 Run-TryBot: Bryan C. Mills <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent dfd613e commit f7f9c8f

File tree

1 file changed

+18
-13
lines changed
  • src/runtime/testdata/testprogcgo

1 file changed

+18
-13
lines changed

src/runtime/testdata/testprogcgo/eintr.go

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ import (
3232
"errors"
3333
"fmt"
3434
"io"
35+
"io/ioutil"
3536
"log"
3637
"net"
3738
"os"
3839
"os/exec"
39-
"os/signal"
4040
"sync"
4141
"syscall"
4242
"time"
@@ -71,14 +71,15 @@ func EINTR() {
7171
// spin does CPU bound spinning and allocating for a millisecond,
7272
// to get a SIGURG.
7373
//go:noinline
74-
func spin() (float64, [][]byte) {
74+
func spin() (float64, []byte) {
7575
stop := time.Now().Add(time.Millisecond)
7676
r1 := 0.0
77-
var r2 [][]byte
77+
r2 := make([]byte, 200)
7878
for time.Now().Before(stop) {
7979
for i := 1; i < 1e6; i++ {
8080
r1 += r1 / float64(i)
81-
r2 = append(r2, bytes.Repeat([]byte{byte(i)}, 100))
81+
r2 = append(r2, bytes.Repeat([]byte{byte(i)}, 100)...)
82+
r2 = r2[100:]
8283
}
8384
}
8485
return r1, r2
@@ -96,8 +97,13 @@ func winch() {
9697

9798
// sendSomeSignals triggers a few SIGURG and SIGWINCH signals.
9899
func sendSomeSignals() {
99-
spin()
100+
done := make(chan struct{})
101+
go func() {
102+
spin()
103+
close(done)
104+
}()
100105
winch()
106+
<-done
101107
}
102108

103109
// testPipe tests pipe operations.
@@ -212,6 +218,10 @@ func testExec(wg *sync.WaitGroup) {
212218
go func() {
213219
defer wg.Done()
214220
cmd := exec.Command(os.Args[0], "Block")
221+
stdin, err := cmd.StdinPipe()
222+
if err != nil {
223+
log.Fatal(err)
224+
}
215225
cmd.Stderr = new(bytes.Buffer)
216226
cmd.Stdout = cmd.Stderr
217227
if err := cmd.Start(); err != nil {
@@ -220,9 +230,7 @@ func testExec(wg *sync.WaitGroup) {
220230

221231
go func() {
222232
sendSomeSignals()
223-
if err := cmd.Process.Signal(os.Interrupt); err != nil {
224-
panic(err)
225-
}
233+
stdin.Close()
226234
}()
227235

228236
if err := cmd.Wait(); err != nil {
@@ -231,10 +239,7 @@ func testExec(wg *sync.WaitGroup) {
231239
}()
232240
}
233241

234-
// Block blocks until the process receives os.Interrupt.
242+
// Block blocks until stdin is closed.
235243
func Block() {
236-
c := make(chan os.Signal, 1)
237-
signal.Notify(c, os.Interrupt)
238-
defer signal.Stop(c)
239-
<-c
244+
io.Copy(ioutil.Discard, os.Stdin)
240245
}

0 commit comments

Comments
 (0)