fix(libkernel): fail a select with EBADF when its pipe closes during the wait on Windows #14206
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/14206"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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
selectterminated the process.The close releases the pipe's host handle before the C runtime descriptor. In that window there were two ways to crash:
QueryPiperanNtQueryInformationFileon a handle that had already been closed. The query failed and it threwpoll: querying pipe descriptor 3 failed.DescriptorIsOpenstill saw the descriptor mapped (_get_osfhandlereturns the dead handle). Soselectlooked the descriptor up again, found no known kind, and threwselect: descriptor 3 is neither a socket, a pipe, a file, a directory nor a kqueue. Theselectdebt entry already noted this second case.FreeBSD 11.4's
kern_selectlooks every descriptor up again in each pass and fails a closed one withEBADF.The fix has three parts, all Windows-only:
QueryPipereturns "not a pipe" instead of throwing when the descriptor no longer maps to the queried handle, or that handle is no longerFILE_TYPE_PIPE. In the captured failure,GetFileTypeon the handle returned 0 while the descriptor still mapped to it.DescriptorIsOpenalso requiresGetHandleInformationto succeed on the mapped handle. The-2pseudo-handle of an unattached standard stream still counts as open.selectanswersEBADFfor 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.
pollreached the same pipe query and crashed the same way:poll: querying pipe descriptor 3 failed. A second commit adds a test for it.QueryPipechange, thepollreturns 0 when the pipe closes during the wait.POLLNVAL. The test accepts either answer, so it does not lock in the 0.The "select" debt entry now says the call fails with
EBADFon Windows. The checker warns that this entry is over 1000 characters (debt-length). It was already 1087 characters onmain, 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, onmain7a5d9bf02a.guest_selectcheckClosesDuringAWait, 200 times:selecton the read end in a thread, closes that end 1 to 3 ms after the thread has started, and joins.selectmust return 0 or fail withEBADF.Udp.cpp,Stdio.cppandSocket.cpprestored tomain: crashes (0xc0000409) 3 of 3, each within a second.EBADF2000/2000 on each of three runs with the change.guest_pollcheckPollsAPipeClosedDuringTheWait: the same pattern, 200 times, with a 20 mspoll. Eachpollmust return 0, or 1 withPOLLNVAL.Udp.cpprestored tomain: crashes (0xc0000409) 3 of 3.kqueuevariant (EVFILT_READon a pipe, closed during a 20 mskevent) already times out without a crash onmain, as FreeBSD'sknote_fdclosebehaves. No change was needed there.ctest -R "poll|select|pipe|socket|kqueue|equeue" -j8 --repeat until-fail:3with both commits: 165/165 pass.ctest -R "select|poll|pipe|file|fs|stat|socket|kqueue|equeue|descriptor|fcntl|dup" -j8 --repeat until-fail:3: 285/286 pass.guest_filesystem, also fails onmainat line 1551. #14197 fixes it.guest_filesystempasses 3/3.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)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
590b279e78merged into upstream/main93dbd0f82a:guest_poll,guest_selectand the unchanged neighbourguest_select_wait: pass, and stay green withctest --repeat until-fail:5(each new test loops 200 close-during-wait races).Stdio.cpp,Socket.cppandUdp.cpp,guest_pollterminates withpoll: querying pipe descriptor 3 failed, andguest_selectwithselect: descriptor 3 is neither a socket, a pipe, a file, a directory nor a kqueue. Those are both crash paths from the description.DescriptorIsOpennow also requiresGetHandleInformationto 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-callfgetand looks like what you want.