Skip to content

fix(libkernel): report a pipe closed before poll reads its status flags as POLLNVAL on Linux - #17853

Merged
oneandonlydean merged 2 commits into
boykopovar:mainfrom
dantdmdarealguy:agent/cifails8
Nov 14, 2026
Merged

oneandonlydean merged 2 commits into
boykopovar:mainfrom
dantdmdarealguy:agent/cifails8

Conversation

@dantdmdarealguy

Copy link
Copy Markdown
Contributor

What

guest_poll still aborts on Linux CI after #16626, now with a different message. Run 42602115865 (job 129110707167, main c0d23c29c1, 2026-11-09 10:36Z) failed with:

terminate called after throwing an instance of 'std::runtime_error'
  what():  poll: querying pipe descriptor 7 failed

On Linux, poll decides that a descriptor is a pipe in IsPipe (Socket/src/Udp.cpp) in two steps:

  1. fstat reports a FIFO.
  2. fcntl(F_GETFL) then reads the access mode.

PollsAPipeClosedDuringTheWait closes the read end from the main thread while the waiter thread is inside poll. If the close lands between those two calls, fcntl fails with EBADF and poll throws.

#16626 handled the case where fstat already sees the descriptor closed: the caller rechecks DescriptorIsOpen and reports POLLNVAL. This change handles the case where the close happens after fstat. An EBADF from the F_GETFL query now leaves the descriptor unclassified, so the same recheck reports POLLNVAL, as it does for a descriptor closed before the call. Any other fcntl failure still throws.

The new Linux test case PollsAPipeClosedBeforeItsStatusQuery in GuestPoll.cpp removes the timing from the race. The test executable interposes fcntl and closes the pipe through close just before libkernel's F_GETFL query, then requires poll to return 1 with POLLNVAL. GuestPosixSpawnFlags.cpp and GuestRuntimeLifecycle.cpp interpose libc the same way. guest_poll_tests now also links ${CMAKE_DL_LIBS} for dlsym.

Tested

Windows 11 Pro, Intel Core i9-14900K, 128 GB RAM, NVIDIA GeForce RTX 5080 (driver 32.0.16.1742). Windows build: WinLibs GCC 15.2.0, Release. Linux build: Arch Linux on WSL2, GCC 16.1.1, glibc 2.43, Release.

  • New test on Linux. ctest -R '^guest_poll$':

    • On main ee63f5e8d5 with only the test added, it fails every time with the CI message poll: querying pipe descriptor 7 failed (100/100 runs).
    • With the change it passes.
  • Race without the interposer. A local program, not part of this PR, has a waiter thread call poll(pipe read end, POLLIN, 0) in a loop while the main thread closes the read end after a varying spin. It does this 2000 times per run.

    main this change
    Runs with at least one throw, 2000 races per run 100/100 (all poll: querying pipe descriptor 6 failed) 0/100
    Runs with at least one throw, 200 races per run 87/100 0/100

    Over all runs, poll returned only 0, or 1 with POLLNVAL.

  • Unmodified guest_poll on Linux, 16 runs at a time.

    • main: 0/700. That includes 300 runs with 16 busy-loop processes for load. The window is about a microsecond, and CI hits it in about 2 of 300 Linux jobs.
    • This change: 0/300, including 100 under the same load.
  • Windows. The Windows pipe path is not changed and the new case is Linux-only.

    • ctest -R '^guest_poll$' --repeat until-fail:100: 100/100 pass on main and 100/100 with the change.
    • ctest -R "^guest_poll$|patched_libraries": passes.
    • Full build and --target libs succeed with no warnings in the touched files.
    • tools/check_test_registration.py, tools/check_conventions.py --base origin/main and tools/pr_overlap.py pass. No open PR changes IsPipe or this test.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes, some AI assistance was used

…gs as POLLNVAL on Linux

poll classified a descriptor as a pipe with fstat and then read its access mode with fcntl(F_GETFL). When another thread closed the pipe between the two calls, fcntl failed with EBADF and poll threw "querying pipe descriptor N failed", which aborts guest_poll on Linux CI. An EBADF from that query now leaves the descriptor unclassified, so the existing recheck reports it as POLLNVAL like a descriptor closed before the call.
@github-actions

github-actions Bot commented Nov 9, 2026 •

Copy link
Copy Markdown

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 4563 core/libs/prx/libkernel/CMakeLists.txt
PR 6149 core/libs/prx/libkernel/CMakeLists.txt
PR 6936 core/libs/prx/libkernel/CMakeLists.txt
PR 9156 core/libs/prx/libkernel/CMakeLists.txt
PR 9293 core/libs/prx/libkernel/CMakeLists.txt
PR 9297 core/libs/prx/libkernel/CMakeLists.txt
PR 9302 core/libs/prx/libkernel/CMakeLists.txt
PR 9383 core/libs/prx/libkernel/CMakeLists.txt
PR 9389 core/libs/prx/libkernel/CMakeLists.txt
PR 9557 core/libs/prx/libkernel/CMakeLists.txt
PR 9566 core/libs/prx/libkernel/CMakeLists.txt
PR 9938 core/libs/prx/libkernel/CMakeLists.txt
PR 11896 core/libs/prx/libkernel/CMakeLists.txt
core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 12682 core/libs/prx/libkernel/CMakeLists.txt
core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 12688 core/libs/prx/libkernel/CMakeLists.txt
core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 12700 core/libs/prx/libkernel/CMakeLists.txt
core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 12965 core/libs/prx/libkernel/CMakeLists.txt
core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 14970 core/libs/prx/libkernel/CMakeLists.txt
core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 16372 core/libs/prx/libkernel/CMakeLists.txt
PR 16790 core/libs/prx/libkernel/CMakeLists.txt
PR 17253 core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 17431 core/libs/prx/libkernel/CMakeLists.txt
PR 17433 core/libs/prx/libkernel/CMakeLists.txt
PR 17442 core/libs/prx/libkernel/CMakeLists.txt
PR 17447 core/libs/prx/libkernel/CMakeLists.txt
PR 17455 core/libs/prx/libkernel/CMakeLists.txt
PR 17463 core/libs/prx/libkernel/CMakeLists.txt
PR 17485 core/libs/prx/libkernel/CMakeLists.txt
PR 17489 core/libs/prx/libkernel/CMakeLists.txt
PR 18293 core/libs/prx/libkernel/CMakeLists.txt
PR 18500 core/libs/prx/libkernel/CMakeLists.txt
PR 18506 core/libs/prx/libkernel/CMakeLists.txt
PR 18515 core/libs/prx/libkernel/CMakeLists.txt
PR 19181 core/libs/prx/libkernel/CMakeLists.txt

@JJenkx

JJenkx commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

Windows check of head 0c374211ef, merged onto main 3cd86e6ea7 (a4defe3940, clean merge).

Setup: Windows 11 Enterprise 22621, WinLibs GCC 15.2 UCRT, Release. The hosted Windows check does not run these tests on pull requests.

  • guest_poll passes in 3 of 3 runs, none skipped.

AI-assisted: yes.

@slopgobblercobbler

Copy link
Copy Markdown
Contributor

Hi @dantdmdarealguy, thank you for investigating this race condition. While the observation regarding the window between fstat and fcntl(F_GETFL) in IsPipe is technically insightful, this PR cannot be accepted in its current form for several reasons:

  1. Pure Guest Scope Violation: Repository standards require guest tests to exercise AnyPS5 guest exports (prx/<lib>/include/Api.hpp / Sce... headers) without hooking host OS syscalls or libc directly. Interposing host libc's fcntl via dlsym(RTLD_NEXT) and linking ${CMAKE_DL_LIBS} in GuestPoll.cpp violates this boundary.
  2. Platform Portability: The interposer test PollsAPipeClosedBeforeItsStatusQuery() is #ifdef __linux__ only, relying on glibc dynamic linker interposition semantics. It provides zero test coverage on Windows or macOS.
  3. Diff Integrity: Repository guidelines require strict diff integrity (2 files modified per PR: 1 source/test file + 1 CMakeLists.txt registration). This PR modifies 3 files across library core, test suite, and build definitions.
  4. CI Build Failure: Modifying core/libs/prx/libkernel/Socket/src/Udp.cpp triggers the pipe-write macOS CI workflow (core/libs/tests/pipe_write/CMakeLists.txt), which fails linking pipe_write_tests on macos-15-intel due to undefined symbol _NotImplementedAt_nid_no_patch.
  5. Existing Coverage: GuestPoll.cpp on main already includes PollsAPipeClosedDuringTheWait(), which validates concurrent closure across 200 iterations and asserts that poll returns either 0 or 1 with POLLNVAL.

Closing this pull request based on these repository and technical constraints.

@dantdmdarealguy

Copy link
Copy Markdown
Contributor Author

Thanks for the review. Taking the points in order:

  1. Interposing libc in a guest test is already done on main: GuestPosixSpawnFlags.cpp:28 (posix_spawn), GuestRuntimeLifecycle.cpp:36 (pthread_cond_wait) and GuestForceTlsDestructor.cpp:74 use dlsym(RTLD_NEXT, …) the same way. It's what makes the race deterministic instead of timing-dependent.
  2. The test is Linux-only because the race is: the fstat → fcntl(F_GETFL) check in IsPipe is the Linux path. JJenkx's Windows run above passes 3/3.
  3. I can't find a two-file limit in CONTRIBUTING; the third file is the test's CMake registration (${CMAKE_DL_LIBS} for dlsym).
  4. The pipe-write macOS failure isn't from this change, which adds no NotImplemented call. It fails the same way (_NotImplementedAt_nid_no_patch undefined) on any PR that touches Udp.cpp, for example fix(libkernel): bind an unbound stream socket on listen on Windows #16585 (run 42728336878). The fixture builds Udp.cpp alone, and Udp.cpp on main already calls NotImplemented_nid_no_patch. I'll send a separate fix for the fixture.
  5. PollsAPipeClosedDuringTheWait is the test that still aborts on Linux CI, for example run 42602115865 with poll: querying pipe descriptor 7 failed. It can only hit the window by chance, which is why the new case forces it.

@dantdmdarealguy

Copy link
Copy Markdown
Contributor Author

Following up on point 4: the pipe-write fixture fix already exists as JJenkx's #17962, which adds the missing NotImplementedAt_nid_no_patch stub to PipeWrite.cpp. Its macOS pipe-write jobs pass (run 42734123486), so I won't open a duplicate. Once #17962 merges, this PR's macOS checks should go green on a rerun.

@gabichuuuuu

Copy link
Copy Markdown
Contributor

Windows check of head 0c374211ef, merged onto main 28237a8567 (clean merge, built together with other open PRs that touch different files).

Setup: Windows 11 x64, WinLibs GCC 15.2.0 (r7), Ninja, Release, BUILD_TESTING=ON, Python 3.15.

  • Tests built from this PR's test files: guest_poll.
  • guest_poll fails in 3 of 3 runs.
  • Negative control: with this PR's code reverted to main (files it adds removed) and rebuilt, the same tests still pass in 2 of 2 runs, so they do not detect the change on this machine.
  • The failing tests also fail on plain main 28237a8567 on this machine, so those failures predate this PR.

AI-assisted: yes.

@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Nov 14, 2026
@oneandonlydean
oneandonlydean merged commit 3d5bd83 into boykopovar:main Nov 14, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants