I tried to use Golang to get current "open fd" limit with the Getrlimit
function, here is my code:
package main
import (
"fmt"
"syscall"
)
func main() {
lim := syscall.Rlimit{}
err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &lim)
if err != nil {
panic(err)
}
fmt.Println("soft:", lim.Cur)
fmt.Println("hard:", lim.Max)
}
Run this will print:
$ go run main.go
soft: 1048576
hard: 1048576
Here you can see the soft number is "1048576", however, the ulimit -Sn
prints it as "1024". I also tried this with C code:
#include <sys/resource.h>
#include <stdio.h>
int main() {
struct rlimit rlim;
if (getrlimit(RLIMIT_NOFILE, &rlim) == -1) {
return -1;
}
printf("soft: %lld \n", (long long) rlim.rlim_cur);
printf("hard: %lld \n", (long long) rlim.rlim_max);
return 0;
}
Compile and run it will also print:
soft: 1024
hard: 1048576
So the number Golang printed out for the "rlim_max" (hard) is correct, but "rlim_cur" (soft) is incorrect, it's supposed to be "1024"? As you can see both the C code and ulimit -Sn
command printed "1024".
Here is my testing environment:
I am wondering if anyone experienced the same problem and why it happened like this? Thank you!
This is per design since 1.19, see https://github.com/golang/go/issues/46279 for the whole discussion.