runc exec: fix RLIMIT_NOFILE race#4266
Closed
kolyshkin wants to merge 2 commits intoopencontainers:mainfrom
Closed
runc exec: fix RLIMIT_NOFILE race#4266kolyshkin wants to merge 2 commits intoopencontainers:mainfrom
kolyshkin wants to merge 2 commits intoopencontainers:mainfrom
Conversation
Go CL 393354 [1] (in Go 1.19beta1) introduced raising the RLIMIT_NOFILE
soft limit to the current value of the hard limit.
Further CLs (in Go 1.21, but backported to Go 1.20.x and 1.19.x)
introduced more code in between getrlimit and setrlimit syscalls
(see Go's src/syscall/rlimit.go).
In runc exec, we use prlimit to set rlimits for the child (runc init)
some time after we start it. This results in the following race:
```
runc exec (parent) runc init (child)
------------------ -----------------
(see (*setnsProcess).start (see init in src/syscall/rlimit.go)
getrlimit(RLIMIT_NOFILE, &lim)
prlimit ....
setrlimit(RLIMIT_NOFILE, &nlim)
```
As a result, once in a while runc exec NOFILE limit is wrong. I
reproduced this in Ubuntu 20.04, running a container with soft and hard
NOFILE limit set to 65536 in config.json, and using the following
script:
```
set -e
RUNC=./runc-1.1-go1.19.13
$RUNC --version
i=0
while [ $i -lt 100000 ]; do
LIM=$($RUNC exec bionic sh -c "ulimit -n");
if [ $LIM -ne 65536 ]; then
echo "WHOOPSIE (iter $i) got numfile $LIM";
break;
fi;
((i%100==0)) && printf "[%s] %10d\n" "$(date)" "$i";
((i++)) || true
done
```
Running it for a few minutes, you'll get:
> WHOOPSIE (iter 3148) got numfile 1024
This seems like a go runtime bug (the changes did not account for the
possibility of setprlimit from another process), but while it's being
fixed, let's use this workaround of applying child's limits at a
predefined time (after all init functions).
NOTE it does not make sense to add a test case because it takes many
iterations to hit the issue, and we can not afford it in CI.
[1] https://go-review.googlesource.com/c/go/+/393354
Signed-off-by: Kir Kolyshkin <[email protected]>
Signed-off-by: Kir Kolyshkin <[email protected]>
Contributor
Author
|
Closing in favor of #4265 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Go CL 393354 (in Go 1.19beta1) introduced raising the RLIMIT_NOFILE soft limit to the current value of the hard limit.
Further CLs (in Go 1.21, but backported to Go 1.20.x and 1.19.x) introduced more code in between getrlimit and setrlimit syscalls (see Go's src/syscall/rlimit.go).
In runc exec, we use
prlimit(2)to set rlimits for the child (runc init) some time after we start it. This results in the following race:As a result, once in a while runc exec NOFILE limit is wrong. I reproduced this in Ubuntu 20.04, running a container with soft and hard NOFILE limit set to 65536 in config.json, and using the following script:
Running it for a few minutes, you'll get:
This seems like a go runtime bug (the changes did not account for the possibility of setprlimit from another process), but while it's being fixed, let's use this workaround of applying child's limits at a predefined time (after all init functions).
NOTE it does not make sense to add a test case because it takes many iterations to hit the issue, and we can not afford it in CI.