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

Merged
dantdmdarealguy merged 2 commits from upstream-pr/17853 into main 2026-11-14 23:15:07 +00:00
dantdmdarealguy commented 2026-11-09 18:20:12 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by dantdmdarealguy on 2026-11-09T18:20:12Z.


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
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5228915291 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/17853). Originally posted by [dantdmdarealguy](https://github.com/dantdmdarealguy) on 2026-11-09T18:20:12Z. --- ### 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 <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes, some AI assistance was used
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!17853
No description provided.