Repository navigation
register: clamp the raised RLIMIT_NOFILE to the hard limit - #1648
Open
ManoharPaturi wants to merge 1 commit into
Open
ManoharPaturi wants to merge 1 commit into
ManoharPaturi wants to merge 1 commit into
Conversation
The kernel checks the number of registered files against the soft RLIMIT_NOFILE and fails with -EMFILE if the count exceeds it. When the library raises the soft limit on behalf of the caller, it adds the file count to the current soft limit. That sum can exceed the hard limit, in which case setrlimit(2) fails with EPERM and the registration ends up failing with -EMFILE even though the request itself would fit below the hard limit. Raise the soft limit to the number of files being registered, clamped to the hard limit, so the retry can succeed whenever the hard limit permits it. Add a test that opens a window where cur < nr <= max and cur + nr > max, and verifies that registration goes through.
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.
The kernel checks the number of files being registered against the soft RLIMIT_NOFILE and fails with -EMFILE when the count exceeds it. When io_uring_register_files() and the other file registration helpers hit that error, the library raises the soft limit on the caller's behalf by adding the file count to the current soft limit.
That sum can overshoot the hard limit. Raising the soft limit above the hard limit fails with EPERM, the setrlimit(2) result is not inspected, and the retried registration fails with -EMFILE again, even though the request itself would fit below the hard limit. Every window with cur < nr <= max and cur + nr > max hits this. For example with cur=200, max=350 and 300 files to register, 200 + 300 = 500 exceeds 350, so the raise fails although the hard limit alone would have been enough for the request.
Raise the soft limit to the number of files being registered, clamped to the hard limit, so the retry succeeds whenever the hard limit permits the request.
Testing: test/file-register.c gains test_rlimit_window(), which opens 300 descriptors, sets cur=200 and max=350, and registers them. On an Ubuntu 24.04 runner the old expression makes this return -EMFILE, while with the clamp the registration succeeds. Full CI matrix is green.