Skip to content

fix(libkernel): set the open file limit at load on macOS - #6836

Merged
adriadam10 merged 3 commits into
boykopovar:mainfrom
DavidSamir:fix/libkernel-open-file-limit-macos
Oct 19, 2026
Merged

adriadam10 merged 3 commits into
boykopovar:mainfrom
DavidSamir:fix/libkernel-open-file-limit-macos

Conversation

@DavidSamir

Copy link
Copy Markdown
Contributor

What

getrlimit(RLIMIT_NOFILE) reports the host's limits, and the libkernel.md entry says that on Linux libkernel raises the soft limit to the hard one at load. That happens in OpenFileLimit.cpp, which is built only for Linux. On macOS the two limits stay different: 1048576 and unlimited in a shell here, 256 and unlimited by default.

XPSemu (PPSA97358) raises the soft limit to the hard one in QEMU's os_setup_limits when they differ. On macOS setrlimit refuses that, and the title stops:

libc++abi: terminating due to uncaught exception of type std::runtime_error: setrlimit: changing the limit of resource 8 is not supported

The soft limit can't simply be raised to the hard one on macOS. macOS fails with EMFILE at kern.maxfilesperproc, 10240 here, whatever the soft limit. A standalone program opened /dev/null up to fd 10239 both with the soft limit unlimited and with both limits at 10240. At load, libkernel now sets both limits to the smaller of the hard limit and kern.maxfilesperproc. So getrlimit reports, as the debt entry describes, the descriptor count after which opening fails. The Linux path still raises the soft limit to the hard one, and Windows is unchanged.

Commits:

  1. The limit at load on macOS.
  2. guest_getrlimit on macOS: the limit against kern.maxfilesperproc, and guest pipes failing with EMFILE at the reported limit (the check Windows already runs, moved into a helper).
  3. The getrlimit debt entry.

Tested

macOS 26.7.1, Apple M2, AppleClang 21.0.0, x86-64 under Rosetta, Release. main doesn't build the system libraries on macOS by itself, so the tests and the title ran in a local tree: main ce991f1 with the macOS runtime work merged (#4627, #4630, #6124, the macOS relinker PRs, mugurc's port branches). The branch's OpenFileLimit.cpp and GuestGetrlimit.cpp are identical to the ones tested.

  • guest_getrlimit aborts with main's OpenFileLimit.cpp, because soft and hard differ. With this branch it passes 5 runs of 5, and also with the shell limits set to 256/unlimited, 256/4096 and 4096/4096.
  • Wrong version: setting both limits to the hard limit without the kern.maxfilesperproc clamp fails the extended test.
  • All 372 guest_ tests here, with main's file and with this branch: guest_getrlimit is the only difference. The other 22 failures are the same with both.
  • XPSemu (PPSA97358, Alpha-2.1), catalog sha256 checked, relinked with --macos --to-rosetta --macos-fixed-base 0x400000 (feat(relinker): place macOS executables at a fixed base with --macos-fixed-base #6802), 2 runs of 2:
    • With main's file it stops on the setrlimit refusal above.
    • With this branch it gets past it and stops at sceKernelJitCreateSharedMemory, which this local tree doesn't build on macOS. main's POSIX backing uses memfd_create, which macOS doesn't have. In standalone probes under Rosetta, code in a shared memory mapping didn't run. An shm_open object mapped twice (one view RW, one RX) raised SIGBUS, and so did one mapped once RWX. A temporary file couldn't be mapped executable (EPERM). Only anonymous private memory ran written code.
  • tools/check_conventions.py --base origin/main and git diff --check pass.

Not tested: Linux and Windows. Linux takes the same branch as before: it returns when soft equals hard, and otherwise raises soft to hard. Windows compiles none of the changed lines, apart from the pipe loop moving into a helper.

Overlap:

Whichever lands second rebases.

Checklist

@github-actions

github-actions Bot commented Oct 16, 2026 •

Copy link
Copy Markdown

Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 7543 docs/dev/debt/libkernel.md L169-170

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 5021 docs/dev/debt/libkernel.md
PR 6044 core/libs/tests/GuestGetrlimit.cpp
PR 6149 docs/dev/debt/libkernel.md
PR 6446 docs/dev/debt/libkernel.md
PR 6469 docs/dev/debt/libkernel.md
PR 6519 docs/dev/debt/libkernel.md
PR 6740 docs/dev/debt/libkernel.md
PR 6936 docs/dev/debt/libkernel.md
PR 6977 docs/dev/debt/libkernel.md
PR 7049 docs/dev/debt/libkernel.md
PR 7105 docs/dev/debt/libkernel.md
PR 7135 docs/dev/debt/libkernel.md
PR 7149 docs/dev/debt/libkernel.md
PR 7201 core/libs/prx/libkernel/File/src/OpenFileLimit.cpp
PR 7509 docs/dev/debt/libkernel.md
PR 7554 docs/dev/debt/libkernel.md

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 16, 2026
@blawesom

Copy link
Copy Markdown
Contributor

Rebase handoff — rebased onto current main (authorship unchanged). One conflict in docs/dev/debt/libkernel.md: your macOS RLIMIT_NOFILE entry is unioned with main's newer entries.

Verified on Linux (CachyOS, kernel 7.2.9, GCC 16.2.1, Ninja, Release, BUILD_TESTING=ON, AMD RX 9070 XT / RADV):

To pick it up:

git fetch https://github.com/blawesom/AnyPS5 rebase/6836
git checkout <your branch>
git merge --ff-only FETCH_HEAD

Head is 375614b61.

@DavidSamir

Copy link
Copy Markdown
Contributor Author

Thanks, @blawesom. I checked your rebase/6836 against this branch: the code commits are identical, and the debt-file commit differs only by the conflict resolution. Rather than force-pushing, I merged current main into the branch with the same resolution, so the history stays as reviewed. The PR's diff against main now matches your rebase. Head is a3fb02df9, and it merges cleanly with main.

AI-assisted: yes

@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 17, 2026
ycarogabriel-lgtm added a commit to ycarogabriel-lgtm/AnyPS5 that referenced this pull request Oct 17, 2026
…DavidSamir)

Replaces this branch's equivalent fix. Both raise RLIMIT_NOFILE at load to kern.maxfilesperproc, the real ceiling on macOS; the PR's version also records the macOS behaviour in the getrlimit debt entry and extends guest_getrlimit, and is the one going upstream.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 17, 2026
ycarogabriel-lgtm added a commit to ycarogabriel-lgtm/AnyPS5 that referenced this pull request Oct 18, 2026
…DavidSamir)

Replaces this branch's equivalent fix. Both raise RLIMIT_NOFILE at load to kern.maxfilesperproc, the real ceiling on macOS; the PR's version also records the macOS behaviour in the getrlimit debt entry and extends guest_getrlimit, and is the one going upstream.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 18, 2026
@github-actions github-actions Bot added the unblocks title Names a title ID on the Unblocks line label Oct 18, 2026
@adriadam10 adriadam10 self-assigned this Oct 19, 2026
@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 19, 2026
getrlimit(RLIMIT_NOFILE) reports the host's limits, and libkernel
raised the soft limit to the hard one at load only on Linux. On macOS
the soft limit stayed below an unlimited hard limit (1048576 and
infinity in a shell here, 256 and infinity by default), so the two
differed. XPSemu (PPSA97358) then raises the soft limit to the hard one
in QEMU's os_setup_limits, and setrlimit refuses the change.

macOS fails with EMFILE at kern.maxfilesperproc (10240 here) whatever
the soft limit, even an unlimited one. At load, libkernel now sets both
limits to the smaller of the hard limit and kern.maxfilesperproc, so
getrlimit reports the descriptor count after which opening fails. The
Linux path still raises the soft limit to the hard one.
guest_getrlimit failed on macOS because the host's soft and hard
RLIMIT_NOFILE differed. On macOS it now also checks that the limit does
not exceed kern.maxfilesperproc, and, as on Windows, that guest pipes
fail with EMFILE within 64 descriptors of the reported limit. The pipe
loop moves into a helper that both use.
@adriadam10
adriadam10 force-pushed the fix/libkernel-open-file-limit-macos branch from d9149b8 to f673abe Compare October 19, 2026 11:43
@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 19, 2026
ycarogabriel-lgtm added a commit to ycarogabriel-lgtm/AnyPS5 that referenced this pull request Oct 19, 2026
…load on macOS (@DavidSamir)

This branch already carried an earlier head of this PR; DavidSamir has
pushed newer commits since, and this merge brings the branch up to
them. The only conflict is the getrlimit and setrlimit debt entries,
which this branch keeps further down with main's text and its own
short macOS line; the PR's copies would have duplicated them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@adriadam10
adriadam10 merged commit 863486c into boykopovar:main Oct 19, 2026
10 checks passed
@adriadam10

Copy link
Copy Markdown
Collaborator

Rebased onto current main (conflicts in GuestGetrlimit.cpp and the getrlimit debt entry after the Windows stdio-limit change): the Windows stream check is now a _WIN32-only helper called after the shared descriptor check, and the debt entry lists Windows, Linux and macOS. Re-checked the macOS clamp, the unchanged Linux path and the extended test; libkernel tests pass; merged.

ycarogabriel-lgtm added a commit to ycarogabriel-lgtm/AnyPS5 that referenced this pull request Oct 25, 2026
…DavidSamir)

Replaces this branch's equivalent fix. Both raise RLIMIT_NOFILE at load to kern.maxfilesperproc, the real ceiling on macOS; the PR's version also records the macOS behaviour in the getrlimit debt entry and extends guest_getrlimit, and is the one going upstream.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs libkernel unblocks title Names a title ID on the Unblocks line

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants