Skip to content

Commit 8427429

Browse files
committed
os: raise open file rlimit at startup
Some systems set an artificially low soft limit on open file count, for compatibility with code that uses select and its hard-coded maximum file descriptor (limited by the size of fd_set). Go does not use select, so it should not be subject to these limits. On some systems the limit is 256, which is very easy to run into, even in simple programs like gofmt when they parallelize walking a file tree. After a long discussion on go.dev/issue/46279, we decided the best approach was for Go to raise the limit unconditionally for itself, and then leave old software to set the limit back as needed. Code that really wants Go to leave the limit alone can set the hard limit, which Go of course has no choice but to respect. Take 2, after CL 392415 was rolled back for macOS and OpenBSD failures. The macOS failures should be handled by the new call to sysctl("kern.maxfilesperproc"), and the OpenBSD failures are handled by skipping the test (and filing #51713). Fixes #46279. Change-Id: I45c81b94590b447b483018a05ae980b8f02dc5de Reviewed-on: https://go-review.googlesource.com/c/go/+/393354 Trust: Russ Cox <[email protected]> Reviewed-by: Bryan Mills <[email protected]> Run-TryBot: Russ Cox <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent f839aaa commit 8427429

File tree

4 files changed

+103
-0
lines changed

4 files changed

+103
-0
lines changed

src/os/rlimit.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright 2022 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
//go:build aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris
6+
7+
package os
8+
9+
import "syscall"
10+
11+
// Some systems set an artificially low soft limit on open file count, for compatibility
12+
// with code that uses select and its hard-coded maximum file descriptor
13+
// (limited by the size of fd_set).
14+
//
15+
// Go does not use select, so it should not be subject to these limits.
16+
// On some systems the limit is 256, which is very easy to run into,
17+
// even in simple programs like gofmt when they parallelize walking
18+
// a file tree.
19+
//
20+
// After a long discussion on go.dev/issue/46279, we decided the
21+
// best approach was for Go to raise the limit unconditionally for itself,
22+
// and then leave old software to set the limit back as needed.
23+
// Code that really wants Go to leave the limit alone can set the hard limit,
24+
// which Go of course has no choice but to respect.
25+
func init() {
26+
var lim syscall.Rlimit
27+
if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &lim); err == nil && lim.Cur != lim.Max {
28+
lim.Cur = lim.Max
29+
adjustFileLimit(&lim)
30+
syscall.Setrlimit(syscall.RLIMIT_NOFILE, &lim)
31+
}
32+
}

src/os/rlimit_darwin.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2022 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
//go:build darwin
6+
7+
package os
8+
9+
import "syscall"
10+
11+
// adjustFileLimit adds per-OS limitations on the Rlimit used for RLIMIT_NOFILE. See rlimit.go.
12+
func adjustFileLimit(lim *syscall.Rlimit) {
13+
// On older macOS, setrlimit(RLIMIT_NOFILE, lim) with lim.Cur = infinity fails.
14+
// Set to the value of kern.maxfilesperproc instead.
15+
n, err := syscall.SysctlUint32("kern.maxfilesperproc")
16+
if err != nil {
17+
return
18+
}
19+
if lim.Cur > uint64(n) {
20+
lim.Cur = uint64(n)
21+
}
22+
}

src/os/rlimit_stub.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright 2022 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
//go:build aix || dragonfly || freebsd || linux || netbsd || openbsd || solaris
6+
7+
package os
8+
9+
import "syscall"
10+
11+
// adjustFileLimit adds per-OS limitations on the Rlimit used for RLIMIT_NOFILE. See rlimit.go.
12+
func adjustFileLimit(lim *syscall.Rlimit) {}

src/os/rlimit_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright 2022 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package os_test
6+
7+
import (
8+
. "os"
9+
"runtime"
10+
"testing"
11+
)
12+
13+
func TestOpenFileLimit(t *testing.T) {
14+
if runtime.GOOS == "openbsd" && (runtime.GOARCH == "arm" || runtime.GOARCH == "arm64") {
15+
t.Skip("broken on openbsd/arm and openbsd/arm64 builder - go.dev/issue/51713")
16+
}
17+
18+
// For open file count,
19+
// macOS sets the default soft limit to 256 and no hard limit.
20+
// CentOS and Fedora set the default soft limit to 1024,
21+
// with hard limits of 4096 and 524288, respectively.
22+
// Check that we can open 1200 files, which proves
23+
// that the rlimit is being raised appropriately on those systems.
24+
var files []*File
25+
for i := 0; i < 1200; i++ {
26+
f, err := Open("rlimit.go")
27+
if err != nil {
28+
t.Error(err)
29+
break
30+
}
31+
files = append(files, f)
32+
}
33+
34+
for _, f := range files {
35+
f.Close()
36+
}
37+
}

0 commit comments

Comments
 (0)