fix(libkernel): report urgent data in poll on macOS #9509
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/9509"
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 011010 on 2026-10-21T23:53:10Z.
What
XNU's
pollnever reportsPOLLPRIfor out-of-band data, so a guest poll that asks for it never sees the urgent byte:guest_pollaborts at its OOB case, and a title waiting on priority data would sleep to its timeout.On macOS the pending byte is now read from
SIOCATMARK, and the wait takes the sliced pathmainalready uses for a kqueue wait instead of a second loop of its own:UrgentPendingasksSIOCATMARKfor the descriptor. It reports a pending urgent byte without consuming it, and it has no descriptor limit: it replaces anfd_setprobe, which calledFD_SETunchecked and wrote past thefd_seton the stack for a descriptor at or aboveFD_SETSIZE.waitsForUrgentmakes a poll that asks forPOLLPRIon a socket count as sliced, on macOS only.anyUrgentPendingruns at the top of each slice; a pending byte skips the wait and ends the loop.collectaddsPOLLPRIandPOLLRDBANDfrom the same check, as FreeBSD 11.4'ssopoll_genericdoes, before mapping the other flags and the errors.waitsForUrgentisconstexpr false, and the slice decision is what it was.This is the
pollpath only. The other two paths that ask the host for urgent readiness, guestselect/pselectthroughGuestSockets::Polland libSceNet'sPollSockets, still miss urgent data on macOS and are recorded as a gap in the technical-debt entry.The idea of probing the socket for the urgent byte comes from #7201, which does it with
recv(MSG_OOB | MSG_PEEK). I measured that variant on macOS 27.0.1 and it consumes the byte, so this usesSIOCATMARKinstead; #7201 may want the same change.keventwithNOTE_OOBdoes not fire there either, which is why neither is used.Tested
Linux (Ubuntu 24.04.5, x86-64, GCC 13.3.0, CMake 3.28.3), on this branch rebased onto current
main(518d36eaec), device lavapipe:guest_pollpasses with the change. This host is the control, not the proof: the LinuxpollreportsPOLLPRIitself, and the helper is compiled out.python3 tools/check_conventions.py --base origin/main: all conventions checks passed.Host mechanisms measured on macOS 27.0.1 (Apple M4), because the probe had to be chosen, not guessed. With one urgent byte sent and the receiver polling for it:
selectexception setFD_SETSIZE(1024)recv(MSG_OOB | MSG_PEEK)keventEVFILT_EXCEPT+NOTE_OOBSIOCATMARKMSG_OOBafterwards)The
FD_SEToverflow the review caught is why theselectprobe cannot be kept.macOS: the reworked code is not run here, and I am not claiming it.
maindoes not compile the libraries on macOS yet (link.hincore/libs/prx/libc/src/GuestAllocations.cpp:26), andmugurc/ci/macos-series-2predates #7605's unified loop, so applying this change to it needs a merge ofmaininto the series rather than a hunk. What has been run on macOS is the same behaviour on the older structure:guest_pollpasses with it on the series, and @maximilianfv reported an M1 Pro run on 2026-10-23 where the test aborts without the change atGuestPoll.cpp:202and passes 2/2 with it.One measurement a macOS run may need to weigh: in my probe the urgent byte failed to arrive at all in about half of fresh loopback connections, independent of the probe used, so
guest_poll's OOB case may be flaky on a Mac for a reason this change does not address.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 MarcZX10 on 2026-10-22T00:09:24Z.
Review: APPROVED
Reviewed the current macOS path.
UrgentPendingchecks only sockets that requestedPOLLPRIand whosepollresult omitted it; the zero-timeout exception-set check supplies the missing readiness bit, while the 10 ms slices are limited to macOS waits that requested priority data. Finite deadlines, zero timeout, pre-ready descriptors, and infinite waits are handled without changing other platforms’ wait path. The PR reports a macOS Rosetta regression run that fails before and passes after, a Linux unchanged-path control, and a standalone select/poll probe; hosted eligibility, intake, conventions, labels, and report checks pass, while product build jobs are skipped. I did not run tests locally. The declared #7605 and #8483 dependencies must be resolved before merge.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T02:06:24Z.
Your #7605 dependency and prior #8483 coordination are now documented. The latest overlap scan also flags three open edits to the same libkernel debt paragraph: #7049, #8658 and #9101. These appear to be documentation-line overlaps rather than behavioral prerequisites; please confirm they only require a debt-file rebase/preservation of both descriptions, and record that plan in the overlap note so the checklist’s
Depends on: #7605, #8483remains accurate for actual behavior/order.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T02:06:25Z.
Review: APPROVED
The macOS path is narrow: on Apple platforms only, requested
POLLPRIomitted bypollis supplied byselect’s exception set, and only waits that request priority data use 10 ms polling slices. The author reports macOS before/afterguest_poll, unchanged Linux control, and a focused host probe; policy checks pass and hosted product jobs are skipped. I did not run local tests. #7605/#8483 order is recorded; please preserve the remaining adjacent libkernel debt edits from #7049/#8658/#9101 when rebasing.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T04:21:18Z.
Review: DISMISSED
The macOS-only readiness path remains consistent with the earlier review: requested POLLPRI omitted by poll is supplied via select's exception set, and only affected waits use short slices. The reported macOS before/after run and unchanged Linux control remain author-reported.
Two repository requirements are still open:
c61d20f548. Please rebase/update and rerun the focused poll/conventions/overlap checks on the current base.I found no new behavior defect and did not rerun local tests. Product jobs are skipped in the hosted checks.
Imported from boykopovar/AnyPS5. Originally posted by maximilianfv on 2026-10-25T10:58:37Z.
Review: DISMISSED
The
collectpart is right, and with itguest_pollgets past the urgent-data check on macOS. The diff no longer applies tomain, though, and the rebase has to change how #7605's loop decides to return. Guestselectstill misses urgent data, and the helper fails silently for host descriptors at or above 1024.Tested on Apple M1 Pro, macOS 27.0.1 (x86_64 build under Rosetta 2)
Base: mugurc's macOS series (
27e968974) merged withmainat2742004ed2, plus other open macOS fixes. I then toggled only this PR.At
fa0ee571ad,git applyfails on the fourthUdp.cpphunk, the sliced wait. That hunk conflicts withmainitself, not with the macOS series: #7605 replaced the single non-Windowspollwith the shared sliced loop. So the "with" state below is a local rebase:main:slicedis also set on__APPLE__when a socket asks forPOLLPRI;socketReadycount also counts a socket whose hostreventsare empty butUrgentPendingis true.I ran 20 tests three times each:
guest_poll,guest_select,guest_select_wait,guest_kernel_sys_pselect,guest_tcp,guest_udpand the 14guest_socket_*tests.guest_pollGuestPoll.cpp:219: aftersend(MSG_OOB),poll(POLLPRI, 1000)returns 0guest_socket_port_zeroandguest_socket_pairfail both ways;guest_socket_read_writefailed once, a flakeGuestPoll.cpp:246The later stop at line 246 is a separate macOS gap, not this PR's. After the peer's
shutdown(SHUT_WR), XNU reportsPOLLIN|POLLHUP(0x11), where the test expectsInalone. A native probe gives the same result. #9512 handles a related Linux case but not this one, and I found no open PR that does. My comment of 2026-10-23 reportedguest_pollpassing with this PR. That test file predatesPollsAStreamWhosePeerClosed: the urgent check was at line 202 then and is at line 219 now.I also added a temporary probe to the test: a guest socket, an urgent byte sent from a native peer, and timing with
steady_clock. Results:poll(POLLPRI, 3000), byte sent at 200 msselectexceptfds, byte already pendingselectexceptfds (3 s), byte sent at 200 msrevents0x2, after 202 msrevents0x2, after 3001 msThe last row is why the wait change matters. Without it, the urgent byte is only noticed when the full timeout expires. By the same code path, a wait with timeout -1 would not return at all (not run).
Findings
The diff conflicts with
main, and the rebase needs more than moving the loop. GitHub reports this branchCONFLICTING. Onmain,poll_nid_postfixdecides whether to return atUdp.cpp:1875, beforecollectruns, andsocketReadycounts hostreventsonly (Udp.cpp:1873). IfUrgentPendingstays only insidecollect, an urgent byte with no host event never ends a slice, so the wait runs to its deadline. Three changes are needed:sliced(Udp.cpp:1829) when a socket asks forPOLLPRI;UrgentPendingin the per-slice exit test;main).With those changes the 202 ms row above holds. The "Based on current
main" box needs re-ticking after the rebase.Host descriptors at or above
FD_SETSIZEsilently lose urgent data (Udp.cpp:554-556at the PR head).OpenFileLimit.cppraisesRLIMIT_NOFILEtokern.maxfilesperproc, which is 61440 here, so host socket descriptors above 1023 can occur. I dup'ed a socket to descriptor 1500 in a standalone probe:FD_SETleaves the bit clear, because the SDK's overflow check skips it;selectfails withEINVAL;UrgentPendingreturnsfalse, which is the old wrong answer.Two checks work at descriptor 1500 on this Mac:
keventwithEVFILT_READandfflags = NOTE_OOBreportsNOTE_OOB(0x2) infflagswhile the byte is pending. kqueue has noFD_SETSIZEceiling and can block, so it could be the basis for a wait without slices.EVFILT_READalso fires on ordinary data, so it is not a drop-inPOLLPRI-only wait. At the PR head, aPOLLIN|POLLPRIwait with timeout -1 wakes every 10 ms for as long as it waits; withmain's slices it would wake every 1 ms.recv(fd, &b, 1, MSG_OOB | MSG_PEEK | MSG_DONTWAIT)returns 1.If you keep
select, at least handledescriptor >= FD_SETSIZEexplicitly instead of returningfalse.Guest
selectandpselectstill miss urgent data on macOS, and the title suggests they are fixed. Guestselectsends exceptfds throughKernelSockets(Socket.cpp:68-70) toGuestSockets::Poll. That function still mapsUrgentto a bare hostPOLLPRI(Udp.cpp:1026and1043onmain).PollSocketsin libSceNet (Export.cpp:724) does the same for sceNet sockets. The probe above shows guestselectleaves the exceptfds bit clear in both states, both for a pending byte and for one that arrives during the wait. Put the check in one helper that all three paths call, or list the two remaining paths in the debt entry. If onlypollis fixed, the title should namepoll.The debt entry describes a divergence that does not exist (
docs/dev/debt/libkernel.md:253at the PR head). The entry says macOS reports urgent data "until it is read". In a native probe on this Mac ("ab", then"u"sent withMSG_OOB, then"cd"),select's exception bit:recv(MSG_OOB)then fails.That is the same rule as FreeBSD 11.4's
sopoll_generic(so_oobmark || SBS_RCVATMARK). Per CONTRIBUTING, the entry should open with the gap that remains: urgent data is noticed one slice late,POLLRDBANDis not synthesized, and theselectpaths are not fixed. It should not open with the implementation.Question about
POLLRDBAND. FreeBSD 11.4'ssopoll_generic(sys/kern/uipc_socket.c:3076-3078) setsevents & (POLLPRI | POLLRDBAND)on the same condition. This PR synthesizes onlyPOLLPRI(Udp.cpp:1791), so a guest that asks for0x80alone still gets nothing on macOS. Was that left out on purpose?Question about prior art. #7201 (opened 2026-10-17) already adds a macOS
UrgentPending, usingrecv(MSG_OOB | MSG_PEEK), and aPOLLPRI-only sliced loop inpoll_nid_postfixwith the same 10 ms andclamp(left, 0, 10)shape. Could you mention it in the body, and credit it if this change was based on it?Your
SIOCATMARKmeasurement differs from mine. You measured it as 0 while the byte was pending on your M4. On this M1 Pro under Rosetta it reads 1 with the byte pending. Usingselectwas still the right call either way.AI-assisted: yes (Claude Code rebased the PR locally, ran the tests and probes, read the code and wrote this review).
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-25T15:16:24Z.
Review: CHANGES_REQUESTED
Thanks for the fix. Agreeing with @maximilianfv's review (the evidence there is from a macOS run); this needs a rewrite on top of current
main, not just a rebase:Udp.cpphunk 4 (the separate Apple slicing loop) no longer applies: #7605 merged the paths into onefor (;;)loop with aslicedflag. Setsliced(Udp.cpp:1827-1829on main) when a socket asks forPOLLPRIon macOS, and countUrgentPendingin the per-slice exit test next tosocketReady(Udp.cpp:1871-1875). With only thecollectchange an urgent byte is noticed at the full timeout (3001 ms vs 202 ms in the reported probe).UrgentPending(Udp.cpp:554-556): a descriptor >=FD_SETSIZEmakesFD_SETa no-op andselectfail withEINVAL, so it returnsfalsesilently. Handle it explicitly (e.g.recv(MSG_OOB | MSG_PEEK | MSG_DONTWAIT)orkeventwithNOTE_OOB, both reported to work at fd 1500).select/pselect(viaGuestSockets::Poll, which still mapsUrgentto barePOLLPRI) and libSceNet'sPollSocketsstill miss urgent data. Put the check in one helper all paths call, or rename the PR topolland list the other paths in the debt entry.docs/dev/debt/libkernel.md:253: the "reported until it is read" divergence was not reproduced (the exception bit clears once the stream passes the mark, as in FreeBSD'ssopoll_generic). Open the entry with the remaining gap (noticed one slice late,POLLRDBANDnot synthesized, theselectpaths) and use the slice length the loop really uses (1 ms on main).sopoll_genericalso setsPOLLRDBANDon the same condition; please synthesize it or say why not.UrgentPending, and credit it if this builds on it.Tick "Based on current
main" again after the rebase.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-26T18:29:05Z.
Review: CHANGES_REQUESTED
Thanks, the single-loop rework fixes the slicing point (
slicedplusurgentin the exit test is what I meant). What is still open:Udp.cpp:571-577:UrgentPendingstill callsFD_SETon any descriptor. On macOSFD_SETdoes not checkfd >= FD_SETSIZE, so a high descriptor now writes past thefd_seton the stack instead of silently returningfalse. Handle it explicitly (e.g.recv(MSG_OOB | MSG_PEEK | MSG_DONTWAIT)orkeventwithNOTE_OOB, both reported to work at fd 1500).select/pselect(GuestSockets::Poll, which still mapsUrgentto barePOLLPRI) and libSceNet'sPollSocketsstill miss urgent data. Call one helper from all paths, or rename the PR topolland list the other paths in the debt entry.sopoll_genericalso setsPOLLRDBANDon the same condition; please synthesize it incollector say why not.docs/dev/debt/libkernel.md:306: "it is reported until it is read" was not reproduced (the exception bit clears once the stream passes the mark, as in FreeBSD). Open the entry with the remaining gap (theselectpaths,POLLRDBAND, wake-up on a 1 ms slice) instead.UrgentPending) in the body, and credit it if this builds on it.