"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Why does setting `ulimit -n` from a Go program on Linux 32-bit distributions result in an \"invalid argument\" error?

Why does setting `ulimit -n` from a Go program on Linux 32-bit distributions result in an \"invalid argument\" error?

Published on 2024-11-06
Browse:798

Why does setting `ulimit -n` from a Go program on Linux 32-bit distributions result in an \

How to set ulimit -n from a Go program?

Problem

A user attempted to set ulimit -n from within a Go program to restrict it within the program rather than globally, using the setrlimit and getrlimit system calls. However, an error occurred when trying to set the value, stating "invalid argument."

Solution

It was discovered that the issue was due to Go bugs in Getrlimit and Setrlimit for Linux 32-bit distributions. These bugs have since been fixed, and the following steps can be taken to resolve the problem:

  1. Update to the Go default branch tip. This will include the bug fixes.
  2. Rebuild the program using the updated Go version.

Results

After updating to the Go default branch tip and rebuilding the program, the expected behavior is observed:

$ ./rlimit
{1024 4096}
Error Setting Rlimit  operation not permitted
Rlimit Final {1024 4096}
$ sudo ./rlimit
[sudo] password for peterSO: 
{1024 4096}
Rlimit Final {999999 999999}

Note: For Linux/amd64 distributions, the issue was not encountered. The problem was specific to Linux 32-bit distributions.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3