fix(libkernel): fail a select with EBADF when its pipe closes during the wait on Windows #14206

Merged
JJenkx merged 2 commits from upstream-pr/14206 into main 2026-11-02 19:46:48 +00:00
JJenkx commented 2026-10-31 23:20:46 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by JJenkx on 2026-10-31T23:20:46Z.


What

On Windows, closing a pipe while another thread waited for it in select terminated the process.

The close releases the pipe's host handle before the C runtime descriptor. In that window there were two ways to crash:

  1. The pipe readiness query. QueryPipe ran NtQueryInformationFile on a handle that had already been closed. The query failed and it threw poll: querying pipe descriptor 3 failed.
  2. The second look-up. Even with the first fixed, DescriptorIsOpen still saw the descriptor mapped (_get_osfhandle returns the dead handle). So select looked the descriptor up again, found no known kind, and threw select: descriptor 3 is neither a socket, a pipe, a file, a directory nor a kqueue. The select debt entry already noted this second case.

FreeBSD 11.4's kern_select looks every descriptor up again in each pass and fails a closed one with EBADF.

The fix has three parts, all Windows-only:

  • QueryPipe returns "not a pipe" instead of throwing when the descriptor no longer maps to the queried handle, or that handle is no longer FILE_TYPE_PIPE. In the captured failure, GetFileType on the handle returned 0 while the descriptor still mapped to it.
  • DescriptorIsOpen also requires GetHandleInformation to succeed on the mapped handle. The -2 pseudo-handle of an unattached standard stream still counts as open.
  • select answers EBADF for an unknown-kind descriptor that is no longer open, before it would throw. A descriptor that is open but of an unsupported kind still throws.

Linux is unchanged.

poll reached the same pipe query and crashed the same way: poll: querying pipe descriptor 3 failed. A second commit adds a test for it.

  • With the QueryPipe change, the poll returns 0 when the pipe closes during the wait.
  • FreeBSD would report the closed descriptor as POLLNVAL. The test accepts either answer, so it does not lock in the 0.

The "select" debt entry now says the call fails with EBADF on Windows. The checker warns that this entry is over 1000 characters (debt-length). It was already 1087 characters on main, and this change adds 11.

Tested

Windows 11 Enterprise 10.0.22621, Ryzen 9 5950X, WinLibs GCC 15.2.0 UCRT r7, Ninja, Release, full build with BUILD_TESTING=ON, on main 7a5d9bf02a.

  • New guest_select check ClosesDuringAWait, 200 times:
    • It creates a pipe, starts a 20 ms select on the read end in a thread, closes that end 1 to 3 ms after the thread has started, and joins.
    • Each select must return 0 or fail with EBADF.
    • With this change: passes 3/3, about 3.5 s per run.
    • With Udp.cpp, Stdio.cpp and Socket.cpp restored to main: crashes (0xc0000409) 3 of 3, each within a second.
    • A 2000-iteration probe of the same pattern returned EBADF 2000/2000 on each of three runs with the change.
  • New guest_poll check PollsAPipeClosedDuringTheWait: the same pattern, 200 times, with a 20 ms poll. Each poll must return 0, or 1 with POLLNVAL.
    • With the change: passes 3/3.
    • With only Udp.cpp restored to main: crashes (0xc0000409) 3 of 3.
  • A 2000-iteration kqueue variant (EVFILT_READ on a pipe, closed during a 20 ms kevent) already times out without a crash on main, as FreeBSD's knote_fdclose behaves. No change was needed there.
  • ctest -R "poll|select|pipe|socket|kqueue|equeue" -j8 --repeat until-fail:3 with both commits: 165/165 pass.
  • Regression set: ctest -R "select|poll|pipe|file|fs|stat|socket|kqueue|equeue|descriptor|fcntl|dup" -j8 --repeat until-fail:3: 285/286 pass.
    • The one failure, guest_filesystem, also fails on main at line 1551. #14197 fixes it.
    • With that change applied as well, guest_filesystem passes 3/3.
  • Not run: Linux and macOS (no change there; the new check accepts the 0 a host poll would give). No title was tested.

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
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5102474030 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/14206). Originally posted by [JJenkx](https://github.com/JJenkx) on 2026-10-31T23:20:46Z. --- ### What On Windows, closing a pipe while another thread waited for it in `select` terminated the process. The close releases the pipe's host handle before the C runtime descriptor. In that window there were two ways to crash: 1. **The pipe readiness query.** `QueryPipe` ran `NtQueryInformationFile` on a handle that had already been closed. The query failed and it threw `poll: querying pipe descriptor 3 failed`. 2. **The second look-up.** Even with the first fixed, `DescriptorIsOpen` still saw the descriptor mapped (`_get_osfhandle` returns the dead handle). So `select` looked the descriptor up again, found no known kind, and threw `select: descriptor 3 is neither a socket, a pipe, a file, a directory nor a kqueue`. The `select` debt entry already noted this second case. FreeBSD 11.4's [`kern_select`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/sys_generic.c#L1129-L1140) looks every descriptor up again in each pass and fails a closed one with `EBADF`. The fix has three parts, all Windows-only: - `QueryPipe` returns "not a pipe" instead of throwing when the descriptor no longer maps to the queried handle, or that handle is no longer `FILE_TYPE_PIPE`. In the captured failure, `GetFileType` on the handle returned 0 while the descriptor still mapped to it. - `DescriptorIsOpen` also requires `GetHandleInformation` to succeed on the mapped handle. The `-2` pseudo-handle of an unattached standard stream still counts as open. - `select` answers `EBADF` for an unknown-kind descriptor that is no longer open, before it would throw. A descriptor that is open but of an unsupported kind still throws. Linux is unchanged. `poll` reached the same pipe query and crashed the same way: `poll: querying pipe descriptor 3 failed`. A second commit adds a test for it. - With the `QueryPipe` change, the `poll` returns 0 when the pipe closes during the wait. - FreeBSD would report the closed descriptor as `POLLNVAL`. The test accepts either answer, so it does not lock in the 0. The "select" debt entry now says the call fails with `EBADF` on Windows. The checker warns that this entry is over 1000 characters (`debt-length`). It was already 1087 characters on `main`, and this change adds 11. ### Tested Windows 11 Enterprise 10.0.22621, Ryzen 9 5950X, WinLibs GCC 15.2.0 UCRT r7, Ninja, Release, full build with `BUILD_TESTING=ON`, on `main` `7a5d9bf02a`. - **New `guest_select` check `ClosesDuringAWait`**, 200 times: - It creates a pipe, starts a 20 ms `select` on the read end in a thread, closes that end 1 to 3 ms after the thread has started, and joins. - Each `select` must return 0 or fail with `EBADF`. - With this change: passes 3/3, about 3.5 s per run. - With `Udp.cpp`, `Stdio.cpp` and `Socket.cpp` restored to `main`: crashes (`0xc0000409`) 3 of 3, each within a second. - A 2000-iteration probe of the same pattern returned `EBADF` 2000/2000 on each of three runs with the change. - **New `guest_poll` check `PollsAPipeClosedDuringTheWait`**: the same pattern, 200 times, with a 20 ms `poll`. Each `poll` must return 0, or 1 with `POLLNVAL`. - With the change: passes 3/3. - With only `Udp.cpp` restored to `main`: crashes (`0xc0000409`) 3 of 3. - A 2000-iteration `kqueue` variant (`EVFILT_READ` on a pipe, closed during a 20 ms `kevent`) already times out without a crash on `main`, as FreeBSD's `knote_fdclose` behaves. No change was needed there. - `ctest -R "poll|select|pipe|socket|kqueue|equeue" -j8 --repeat until-fail:3` with both commits: 165/165 pass. - **Regression set:** `ctest -R "select|poll|pipe|file|fs|stat|socket|kqueue|equeue|descriptor|fcntl|dup" -j8 --repeat until-fail:3`: 285/286 pass. - The one failure, `guest_filesystem`, also fails on `main` at line 1551. #14197 fixes it. - With that change applied as well, `guest_filesystem` passes 3/3. - **Not run:** Linux and macOS (no change there; the new check accepts the 0 a host poll would give). No title was tested. ### 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
BunnyFl commented 2026-11-02 01:42:40 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by BunnyFl on 2026-11-02T01:42:40Z.

Review: APPROVED


Windows 11, WinLibs GCC 15.2.0 r7, Release. Head 590b279e78 merged into upstream/main 93dbd0f82a:

  • guest_poll, guest_select and the unchanged neighbour guest_select_wait: pass, and stay green with ctest --repeat until-fail:5 (each new test loops 200 close-during-wait races).
  • Negative control: with main's Stdio.cpp, Socket.cpp and Udp.cpp, guest_poll terminates with poll: querying pipe descriptor 3 failed, and guest_select with select: descriptor 3 is neither a socket, a pipe, a file, a directory nor a kqueue. Those are both crash paths from the description.

DescriptorIsOpen now also requires GetHandleInformation to succeed, which affects its other callers too. A descriptor whose host handle is already gone now reads as closed there as well. That's closer to FreeBSD's per-call fget and looks like what you want.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5731297245 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/14206#pullrequestreview-5731297245). Originally posted by [BunnyFl](https://github.com/BunnyFl) on 2026-11-02T01:42:40Z. **Review:** APPROVED --- Windows 11, WinLibs GCC 15.2.0 r7, Release. Head 590b279e78 merged into upstream/main 93dbd0f82a: - `guest_poll`, `guest_select` and the unchanged neighbour `guest_select_wait`: pass, and stay green with `ctest --repeat until-fail:5` (each new test loops 200 close-during-wait races). - Negative control: with main's `Stdio.cpp`, `Socket.cpp` and `Udp.cpp`, `guest_poll` terminates with `poll: querying pipe descriptor 3 failed`, and `guest_select` with `select: descriptor 3 is neither a socket, a pipe, a file, a directory nor a kqueue`. Those are both crash paths from the description. `DescriptorIsOpen` now also requires `GetHandleInformation` to succeed, which affects its other callers too. A descriptor whose host handle is already gone now reads as closed there as well. That's closer to FreeBSD's per-call `fget` and looks like what you want.
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!14206
No description provided.