Skip to content

Commit d2512af

Browse files
misc/cgo/test: limit issue18146 attempts based on RLIMIT_NPROC
Fixes #18381. Change-Id: I0a476cd7f6182c8d4646628477c56c133d5671ee Reviewed-on: https://go-review.googlesource.com/34667 Run-TryBot: Ian Lance Taylor <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent d51046b commit d2512af

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

misc/cgo/test/issue18146.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,31 @@ func test18146(t *testing.T) {
3737
attempts = 100
3838
}
3939

40+
// Restrict the number of attempts based on RLIMIT_NPROC.
41+
// Tediously, RLIMIT_NPROC was left out of the syscall package,
42+
// probably because it is not in POSIX.1, so we define it here.
43+
// It is not defined on Solaris.
44+
var nproc int
45+
setNproc := true
46+
switch runtime.GOOS {
47+
default:
48+
setNproc = false
49+
case "linux":
50+
nproc = 6
51+
case "darwin", "dragonfly", "freebsd", "netbsd", "openbsd":
52+
nproc = 7
53+
}
54+
if setNproc {
55+
var rlim syscall.Rlimit
56+
if syscall.Getrlimit(nproc, &rlim) == nil {
57+
max := int(rlim.Cur) / (threads + 5)
58+
if attempts > max {
59+
t.Logf("lowering attempts from %d to %d for RLIMIT_NPROC", attempts, max)
60+
attempts = max
61+
}
62+
}
63+
}
64+
4065
if os.Getenv("test18146") == "exec" {
4166
runtime.GOMAXPROCS(1)
4267
for n := threads; n > 0; n-- {

0 commit comments

Comments
 (0)