Skip to content

Commit f48da43

Browse files
holimankaralabe
authored andcommitted
common/fdlimit: cap on MacOS file limits, fixes ethereum#18994 (ethereum#19035)
* common/fdlimit: cap on MacOS file limits, fixes ethereum#18994 * common/fdlimit: fix Maximum-check to respect OPEN_MAX * common/fdlimit: return error if OPEN_MAX is exceeded in Raise() * common/fdlimit: goimports * common/fdlimit: check value after setting fdlimit * common/fdlimit: make comment a bit more descriptive * cmd/utils: make fdlimit happy path a bit cleaner
1 parent 3de19c8 commit f48da43

File tree

5 files changed

+22
-13
lines changed

5 files changed

+22
-13
lines changed

cmd/utils/flags.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -950,10 +950,11 @@ func makeDatabaseHandles() int {
950950
if err != nil {
951951
Fatalf("Failed to retrieve file descriptor allowance: %v", err)
952952
}
953-
if err := fdlimit.Raise(uint64(limit)); err != nil {
953+
raised, err := fdlimit.Raise(uint64(limit))
954+
if err != nil {
954955
Fatalf("Failed to raise file descriptor allowance: %v", err)
955956
}
956-
return limit / 2 // Leave half for networking and other stuff
957+
return int(raised / 2) // Leave half for networking and other stuff
957958
}
958959

959960
// MakeAddress converts an account specified directly as a hex encoded string or

common/fdlimit/fdlimit_freebsd.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,24 @@ import "syscall"
2626

2727
// Raise tries to maximize the file descriptor allowance of this process
2828
// to the maximum hard-limit allowed by the OS.
29-
func Raise(max uint64) error {
29+
func Raise(max uint64) (uint64, error) {
3030
// Get the current limit
3131
var limit syscall.Rlimit
3232
if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &limit); err != nil {
33-
return err
33+
return 0, err
3434
}
3535
// Try to update the limit to the max allowance
3636
limit.Cur = limit.Max
3737
if limit.Cur > int64(max) {
3838
limit.Cur = int64(max)
3939
}
4040
if err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &limit); err != nil {
41-
return err
41+
return 0, err
42+
}
43+
if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &limit); err != nil {
44+
return 0, err
4245
}
43-
return nil
46+
return limit.Cur, nil
4447
}
4548

4649
// Current retrieves the number of file descriptors allowed to be opened by this

common/fdlimit/fdlimit_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func TestFileDescriptorLimits(t *testing.T) {
3636
if limit, err := Current(); err != nil || limit <= 0 {
3737
t.Fatalf("failed to retrieve file descriptor limit (%d): %v", limit, err)
3838
}
39-
if err := Raise(uint64(target)); err != nil {
39+
if _, err := Raise(uint64(target)); err != nil {
4040
t.Fatalf("failed to raise file allowance")
4141
}
4242
if limit, err := Current(); err != nil || limit < target {

common/fdlimit/fdlimit_unix.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,26 @@ import "syscall"
2222

2323
// Raise tries to maximize the file descriptor allowance of this process
2424
// to the maximum hard-limit allowed by the OS.
25-
func Raise(max uint64) error {
25+
// Returns the size it was set to (may differ from the desired 'max')
26+
func Raise(max uint64) (uint64, error) {
2627
// Get the current limit
2728
var limit syscall.Rlimit
2829
if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &limit); err != nil {
29-
return err
30+
return 0, err
3031
}
3132
// Try to update the limit to the max allowance
3233
limit.Cur = limit.Max
3334
if limit.Cur > max {
3435
limit.Cur = max
3536
}
3637
if err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &limit); err != nil {
37-
return err
38+
return 0, err
39+
}
40+
// MacOS can silently apply further caps, so retrieve the actually set limit
41+
if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &limit); err != nil {
42+
return 0, err
3843
}
39-
return nil
44+
return limit.Cur, nil
4045
}
4146

4247
// Current retrieves the number of file descriptors allowed to be opened by this

common/fdlimit/fdlimit_windows.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import "errors"
2020

2121
// Raise tries to maximize the file descriptor allowance of this process
2222
// to the maximum hard-limit allowed by the OS.
23-
func Raise(max uint64) error {
23+
func Raise(max uint64) (uint64, error) {
2424
// This method is NOP by design:
2525
// * Linux/Darwin counterparts need to manually increase per process limits
2626
// * On Windows Go uses the CreateFile API, which is limited to 16K files, non
@@ -30,7 +30,7 @@ func Raise(max uint64) error {
3030
if max > 16384 {
3131
return errors.New("file descriptor limit (16384) reached")
3232
}
33-
return nil
33+
return max, nil
3434
}
3535

3636
// Current retrieves the number of file descriptors allowed to be opened by this

0 commit comments

Comments
 (0)