Repository navigation
fix(libkernel): wake every thread waiting on an equeue or kqueue when an event fires - #7492
Conversation
|
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.
|
|
Windows run (this was syntax-checked only there): Windows 11, MinGW GCC 15.2, with this PR merged onto
AI-assisted: yes (Claude Code); results above were run on this machine. |
|
Windows run of head
Titles: Not checked: Linux, macOS, Written with Claude Code assistance (validation by a subagent, reviewed in this session). |
|
Support triage — rebase note: the only rebase conflict against current |
… an event fires AddEvent, TriggerEvent and EditEvent woke one of the threads blocked in sceKernelWaitEqueue or kevent on the queue. With two or more waiters, a level-triggered event (sceKernelAddUserEvent, or EVFILT_USER without EV_CLEAR) released only one of them; the others slept until their timeout, or for good without one, while a poll of the same queue reported the event. A timer added while several threads waited reached only the thread woken to recompute its wait; the others slept until their own timeout, or for good without one. FreeBSD 11.4 kern_event.c activates a knote through knote_enqueue, which calls kqueue_wakeup and wakeup(kq), and wakeup makes every thread sleeping on the queue runnable (kern_synch.c). Each woken thread scans again: a level knote goes back on the queue after it is reported, so every waiter returns it, while an EV_CLEAR knote is taken by one waiter and the others sleep again. Notify every waiter, as Close already does; WaitForEventsUntil already collects again and goes back to sleep when it finds nothing. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
fd2eb35 to
2feb074
Compare
|
Thanks. Rebased onto On the rebased branch both tests pass, 15 times in a row, and abort again at the AI-assisted: yes (Claude Code). |
ahmadmuj
left a comment
There was a problem hiding this comment.
Reviewed head 2feb074210, merged onto current main 0119103fa9 (clean). Linux (WSL2 Ubuntu 24.04), GCC 13.3, Release. Main has had 24 commits under Equeue/ since this branch's base cf2c66706, so I mostly wanted to know if the fix still holds up on top of them.
What I ran:
guest_equeue_eventsandguest_kqueueon the merged tree: 10/10 each (--repeat until-fail:10).- Control, main's
Equeue.cppwith this PR's tests: both abort (equeue after 5.5 s, kqueue after 7.3 s, vs 0.6 s / 5.0 s green), which fits the second waiter sitting out its 5 s limit.
Checked on the merged tree:
- The three sites here plus
Closeare still the onlym_condnotifies.DeleteEventandRemoveDescriptorEventsonly wake pollers, and they don't make anything ready, so nothing's missing. The kqueueNOTE_TRIGGERpath ends up inEditEvent. - No double delivery with
NotifyAll: inCollectTriggeredandCollectOncePerWait,EV_CLEAR/EV_DISPATCH/EV_ONESHOTget cleared or erased by whichever waiter collects first, and the others loop back to sleep. AMPR'spendingEventshands the next queued completion to the second waiter, not the same one again. - Timer case:
sceKernelAddTimerEventisEV_CLEARwith a periodic interval, so the second waiter gets the next expiry, not the same one. That matches the 100 ms / 150 ms in the description. - FreeBSD 11.4
kern_event.c:kqueue_wakeupcallswakeup(kq),wakeupgoes throughsleepq_broadcast, there's nowakeup_oneanywhere in the file, and non-EV_CLEAR/EV_DISPATCHknotes get re-queued at the tail inkqueue_scan. Your links point at those lines.
Looks right to me.
Not checked: Windows (wachi35676 and screamm covered it on older bases), titles.
|
Static review plus an independent Linux run (CachyOS, kernel 7.2.10, GCC 16.2.1, Ninja, Release, What I checked
Focused test runs
Both 2/2 Passed on the PR head. Not an AGC/shader/VideoOut change — no paired VVL run required. |
|
Thanks for the run. On the failure message: the AI-assisted: yes (Claude Code). |
|
Go with the printing variant, but as the helper upgrade rather than per-check: |
What
sceKernelWaitEqueueandkeventsleep on the queue's condition variable inKernelEqueuePrivate::WaitForEventsUntil, andAddEvent,TriggerEventandEditEventwoke only one of the sleeping threads (m_cond.NotifyOne()). With two or more threads blocked on one equeue or kqueue:sceKernelAddUserEventandsceKernelTriggerUserEvent, or anEVFILT_USERknote withoutEV_CLEARtriggered withNOTE_TRIGGER) released one waiter. The others slept until their timeout, or for good with a null timeout, while a poll (timo0) of the same queue at that moment returned the event.sceKernelAddTimerEvent, orEVFILT_TIMERwithEV_ADD) reached only the thread woken to recompute its wait. The others returned at their own timeout, or not at all with a null timeout.On
main, with two waiters using a 5 s timeout and the event fired 50 ms after they start, the second waiter returns at 5000 ms in all three cases (user event, timer, kqueue user event), while the first returns at 50 ms, or at 100 ms for the 50 ms timer.FreeBSD 11.4 wakes every thread asleep on a kqueue when a knote becomes active.
knote_enqueuecallskqueue_wakeup, which callswakeup(kq), andwakeupmakes every thread sleeping on the queue runnable throughsleepq_broadcast;kern_event.cnever callswakeup_one. Each woken thread goes back toretryinkqueue_scan. A knote withoutEV_CLEARorEV_DISPATCHis put back at the tail after it is reported, so every waiter returns it; anEV_CLEARknote is taken by one waiter and the others findkq_countat 0 and sleep again. ANOTE_TRIGGERchange enqueues the knote inkqueue_register, and a timer expiry throughfilt_timerexpire. Not measured on a console.The three calls now use
m_cond.NotifyAll(), asClosealready does.WaitForEventsUntilcollects again after every wake and goes back to sleep when it finds nothing, so anEV_CLEARevent still reaches one waiter and a queue with a single waiter behaves as before. A queue with socket or pipe events waits inWaitForDescriptorsinstead, andWakePollersalready wakes every poller there. On Windows the non-coarseTimedWait::Condition::NotifyAllwalks the waiter list, the pathClosealready takes. I don't know of a title that blocks two threads on one queue; VideoOut, AGC, AMPR and libSceHttp2 completions reach waiters throughTriggerEvent.New checks:
VerifyEventWakesEveryWaiterinGuestEqueueEvents.cpp: two threads wait with a 5 s timeout, the main thread triggers a level user event (TriggerEvent), and both waiters must return within 2 s. The same follows on the emptied queue by adding a 50 ms timer (AddEvent).UserEventWakesEveryWaiterinGuestKqueue.cpp: the same with twokeventwaiters and aNOTE_TRIGGERchange (EditEvent).Tested
Gentoo Linux, kernel 7.2.8, GCC 16.2.1, glibc 2.43, Release,
BUILD_TESTING=ON, rebased ontomaincf2c667.guest_equeue_eventsandguest_kqueuewith the new checks pass (0.63 s and 3.81 s). With onlyEqueue.cppreverted tomainboth abort after 5.42 s and 6.66 s, at the< 2 scheck, because the second waiter returns at its 5 s timeout.ctest --repeat until-fail:15 -j2): all pass.ctest -j4 --timeout 300: 1806 tests, 6 failed, 16 skipped. The failures (agc_driver_sample_orderwith its_grid_referenceand_recipe_depth_locationsruns, andnet_dontwait_blocking_send,_sendtoand_sendmsg) fail the same way with onlyEqueue.cppfrommain, so this change doesn't cause them. The skips are the 13video_out_flip_*tests,video_out_frame_dump_queueandvideo_out_telemetry_captionunder the offscreen SDL driver, andagc_driver_stencil_ref_export.Equeue.cpp,GuestEqueueEvents.cppandGuestKqueue.cppcompile withx86_64-w64-mingw32-g++16.2 (-fsyntax-only -Wall -Wextra). This is a syntax check, not a Windows build or run.tools/check_conventions.pypasses againstmain.tools/pr_overlap.py --branchreports no overlapping lines. It could not compare test: organize library tests and label CTest suites #6044, feat(libkernel): support kevent read and write events on regular files #7088, fix(libkernel): bound equeue pending events #7509, fix(libkernel): handle kevent timer re-adds and the AIO, LIO, SENDFILE and PROCDESC filters as measured on a PS5 #7653 and feat(libkernel): answer sceKernelAddReadEvent and sceKernelAddWriteEvent on regular files #9099, which conflict withmain; merged with this branch, each of them has the same conflicts as withmainalone (feat(libkernel): answer sceKernelAddReadEvent and sceKernelAddWriteEvent on regular files #9099: 3 hunks inEqueue.cpp, inGatherInterestsandWaitForDescriptors, and 1 inEqueue.hpp), and none of them changesNotifyOneorNotifyAll.Not run on Windows, macOS or a console by me. @wachi35676 (Windows 11 and Ubuntu 24.04) and @screamm (Windows 11, stacked with #7509 and #7090) ran the head before this rebase, fd2eb35, which has the same change; see their comments.
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)🤖 Generated with Claude Code