summaryrefslogtreecommitdiffstats
path: root/misc/cgo/test/issue18146.go
blob: e50f9ae53016fce2f88e1b0118145635f41c19d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build !windows
// +build !windows

// Issue 18146: pthread_create failure during syscall.Exec.

package cgotest

import (
	"bytes"
	"crypto/md5"
	"os"
	"os/exec"
	"runtime"
	"syscall"
	"testing"
	"time"
)

func test18146(t *testing.T) {
	if testing.Short() {
		t.Skip("skipping in short mode")
	}

	if runtime.GOOS == "darwin" || runtime.GOOS == "ios" {
		t.Skipf("skipping flaky test on %s; see golang.org/issue/18202", runtime.GOOS)
	}

	if runtime.GOARCH == "mips" || runtime.GOARCH == "mips64" {
		t.Skipf("skipping on %s", runtime.GOARCH)
	}

	attempts := 1000
	threads := 4

	// Restrict the number of attempts based on RLIMIT_NPROC.
	// Tediously, RLIMIT_NPROC was left out of the syscall package,
	// probably because it is not in POSIX.1, so we define it here.
	// It is not defined on Solaris.
	var nproc int
	setNproc := true
	switch runtime.GOOS {
	default:
		setNproc = false
	case "aix":
		nproc = 9
	case "linux":
		nproc = 6
	case "darwin", "dragonfly", "freebsd", "netbsd", "openbsd":
		nproc = 7
	}
	if setNproc {
		var rlim syscall.Rlimit
		if syscall.Getrlimit(nproc, &rlim) == nil {
			max := int(rlim.Cur) / (threads + 5)
			if attempts > max {
				t.Logf("lowering attempts from %d to %d for RLIMIT_NPROC", attempts, max)
				attempts = max
			}
		}
	}

	if os.Getenv("test18146") == "exec" {
		runtime.GOMAXPROCS(1)
		for n := threads; n > 0; n-- {
			go func() {
				for {
					_ = md5.Sum([]byte("Hello, !"))
				}
			}()
		}
		runtime.GOMAXPROCS(threads)
		argv := append(os.Args, "-test.run=NoSuchTestExists")
		if err := syscall.Exec(os.Args[0], argv, os.Environ()); err != nil {
			t.Fatal(err)
		}
	}

	var cmds []*exec.Cmd
	defer func() {
		for _, cmd := range cmds {
			cmd.Process.Kill()
		}
	}()

	args := append(append([]string(nil), os.Args[1:]...), "-test.run=Test18146")
	for n := attempts; n > 0; n-- {
		cmd := exec.Command(os.Args[0], args...)
		cmd.Env = append(os.Environ(), "test18146=exec")
		buf := bytes.NewBuffer(nil)
		cmd.Stdout = buf
		cmd.Stderr = buf
		if err := cmd.Start(); err != nil {
			// We are starting so many processes that on
			// some systems (problem seen on Darwin,
			// Dragonfly, OpenBSD) the fork call will fail
			// with EAGAIN.
			if pe, ok := err.(*os.PathError); ok {
				err = pe.Err
			}
			if se, ok := err.(syscall.Errno); ok && (se == syscall.EAGAIN || se == syscall.EMFILE) {
				time.Sleep(time.Millisecond)
				continue
			}

			t.Error(err)
			return
		}
		cmds = append(cmds, cmd)
	}

	failures := 0
	for _, cmd := range cmds {
		err := cmd.Wait()
		if err == nil {
			continue
		}

		t.Errorf("syscall.Exec failed: %v\n%s", err, cmd.Stdout)
		failures++
	}

	if failures > 0 {
		t.Logf("Failed %v of %v attempts.", failures, len(cmds))
	}
}