Skip to content

fix(libkernel): retire an EV_ONESHOT note through its delete callback and follow Windows $Deleted paths - #7049

Merged
adriadam10 merged 5 commits into
boykopovar:mainfrom
blawesom:followup/6510
Oct 25, 2026
Merged

adriadam10 merged 5 commits into
boykopovar:mainfrom
blawesom:followup/6510

Conversation

@blawesom

@blawesom blawesom commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

What

Carries the additive pieces of #6510 (as offered there after its feature was superseded by #5847), revised per the review of b64fd7377:

  • deleteEventFunc invocation when an EV_ONESHOT note retires in CollectTriggered
  • Windows delete detection through the POSIX recycle-bin path, now matching \$Extend$Deleted`exactly instead of any path containing$Deleted`
  • the vnode modify guard is dropped — FreeBSD's kqueue_register sets kn_sfflags unconditionally for filters without f_touch, matching main's unconditional SetVnodeNotes
  • the timer modify relaxation (data 0 keeps the period) stays, now with a debt-entry line in docs/dev/debt/libkernel.md

Tested

Linux (CachyOS, kernel 7.2.9, GCC 16.2.1, Ninja, Release, BUILD_TESTING=ON, AMD RX 9070 XT / RADV): full suite passes except the known main regression (agc_driver_tessellation segfault, #4727); conventions pass. New tests:

  • VnodeOneshotWatchStops: a vnode EV_ONESHOT fires once, the note is gone, and on Linux the retired watch's inotify descriptor count returns to its pre-watch level (with a bounded wait for the watch thread to exit) — the leak the review described
  • PeriodicTimerFiresTwice: a plain periodic timer fires twice (uncovered on main before)
  • TimerDisableEnableRearm: a disabled periodic timer's EV_ENABLE reports the expiries missed while disabled in one step and keeps the note's phase, and an EV_ADD | EV_ENABLE re-add still throws
  • RecycleBinDeleteWatchedFile (Windows-gated): a watched file renamed under \$Extend$Deleted`reportsNOTE_DELETE`

The requested timer test is in the branch now (TimerDisableEnableRearm): EV_DISABLE followed by EV_ENABLE with data == 0 on a periodic timer reports the expiries missed while disabled in one step and keeps the note's phase, as FreeBSD 11's filt_timerexpire counts a disabled note and keeps its p_nexttime. The next-wait abort described in the earlier round no longer reproduces: it was observed on the pre-rebase head, before main's equeue wait refactor (5cbc46029b). One guard change came out of writing it: a timer modify that sets fflags was silently ignored, where kqueue_register copies it onto the note — it now refuses with the other re-adds, waiting for the measured rules.

Checklist

@oneandonlydean

Copy link
Copy Markdown
Collaborator

Review of b64fd73: the EV_ONESHOT detach is correct (it matches FreeBSD kqueue_scan, and it's safe under the lock). Three things block merge:

  1. The vnode guard departs from FreeBSD. In kqueue_register (kern_event.c:1345-1357), every change to an existing knote reaches the "user may change some filter values" block, and filters without f_touch (vnode included) get kn->kn_sfflags = kev->fflags unconditionally. So on FreeBSD, EV_ENABLE/EV_DISABLE with fflags == 0 clears the vnode interest. Main matches that today (SetVnodeNotes(event, change.fflags) is unconditional). Please drop the guard, or point to console/libkernel evidence that the PS5 differs.
  2. None of the 4 production changes is tested. The new tests use only EVFILT_USER/READ/WRITE, which are already on main. Could you add:
    • a vnode EV_ONESHOT test (trigger once, then check the watch is stopped; on Linux, count the threads or inotify fds before and after, since on main each retired oneshot leaks a detached RunHostWatch thread);
    • EVFILT_TIMER EV_DISABLE/EV_ENABLE with data == 0: no firing while disabled, and the period is kept after EV_ENABLE;
    • a Windows variant of the delete test that exercises the $Deleted path (RenameAndDeleteWatchedFile is #ifndef _WIN32).
  3. CI hasn't run. The conventions check fails on the PR template (What/Tested/Checklist are missing), so linux and windows were skipped. Please fill in the template.

Minor:

  • path.find(L"$Deleted") matches any path containing that text. With POSIX delete semantics the file moves under \$Extend\$Deleted\, so match L"\\$Extend\\$Deleted\\".
  • Treating timer data == 0 as "keep the period" is a sane choice, but FreeBSD stores the 0. Please add a line to docs/dev/debt/libkernel.md.

When this lands, #6510 should be closed as superseded, since this carries its parts.

@github-actions

github-actions Bot commented Oct 17, 2026 •

Copy link
Copy Markdown

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.

Pull request Shared files not compared
PR 4630 docs/dev/debt/libkernel.md
PR 5021 core/libs/prx/libkernel/Equeue/src/FileEvent.cpp
docs/dev/debt/libkernel.md
PR 6149 docs/dev/debt/libkernel.md
PR 6740 docs/dev/debt/libkernel.md
PR 6830 docs/dev/debt/libkernel.md
PR 6936 docs/dev/debt/libkernel.md
PR 7088 core/libs/prx/libkernel/Equeue/src/FileEvent.cpp
core/libs/prx/libkernel/Equeue/src/Kqueue.cpp
core/libs/tests/GuestKqueue.cpp
docs/dev/debt/libkernel.md
PR 7105 docs/dev/debt/libkernel.md
PR 7121 core/libs/prx/libkernel/Equeue/src/FileEvent.cpp
core/libs/prx/libkernel/Equeue/src/Kqueue.cpp
core/libs/tests/GuestKqueue.cpp
docs/dev/debt/libkernel.md
PR 7149 docs/dev/debt/libkernel.md
PR 7192 docs/dev/debt/libkernel.md
PR 7447 docs/dev/debt/libkernel.md
PR 7509 core/libs/prx/libkernel/Equeue/Equeue.cpp
docs/dev/debt/libkernel.md
PR 7804 docs/dev/debt/libkernel.md
PR 8010 docs/dev/debt/libkernel.md
PR 8520 docs/dev/debt/libkernel.md
PR 8553 docs/dev/debt/libkernel.md
PR 8605 docs/dev/debt/libkernel.md
PR 9187 docs/dev/debt/libkernel.md
PR 9199 docs/dev/debt/libkernel.md
PR 9242 docs/dev/debt/libkernel.md
PR 9284 core/libs/prx/libkernel/Equeue/Equeue.cpp
docs/dev/debt/libkernel.md
PR 9293 core/libs/prx/libkernel/Equeue/Equeue.cpp
docs/dev/debt/libkernel.md
PR 9297 core/libs/prx/libkernel/Equeue/Equeue.cpp
docs/dev/debt/libkernel.md
PR 9302 core/libs/prx/libkernel/Equeue/Equeue.cpp
docs/dev/debt/libkernel.md
PR 9378 docs/dev/debt/libkernel.md
PR 9442 docs/dev/debt/libkernel.md
PR 9482 docs/dev/debt/libkernel.md
PR 9499 docs/dev/debt/libkernel.md
PR 9509 docs/dev/debt/libkernel.md
PR 9557 docs/dev/debt/libkernel.md
PR 9561 docs/dev/debt/libkernel.md
PR 9566 docs/dev/debt/libkernel.md
PR 9575 core/libs/prx/libkernel/Equeue/Equeue.cpp
core/libs/prx/libkernel/Equeue/src/Kqueue.cpp
core/libs/tests/GuestKqueue.cpp
docs/dev/debt/libkernel.md
PR 9669 docs/dev/debt/libkernel.md
PR 9682 docs/dev/debt/libkernel.md
PR 9693 docs/dev/debt/libkernel.md
PR 9732 docs/dev/debt/libkernel.md
PR 9823 core/libs/prx/libkernel/Equeue/Equeue.cpp
core/libs/prx/libkernel/Equeue/src/FileEvent.cpp
core/libs/prx/libkernel/Equeue/src/Kqueue.cpp
PR 9938 docs/dev/debt/libkernel.md
PR 10016 core/libs/prx/libkernel/Equeue/src/Kqueue.cpp
core/libs/tests/GuestKqueue.cpp
PR 10061 docs/dev/debt/libkernel.md
PR 10192 docs/dev/debt/libkernel.md
PR 10442 docs/dev/debt/libkernel.md
PR 10456 docs/dev/debt/libkernel.md
PR 10499 docs/dev/debt/libkernel.md
PR 10621 docs/dev/debt/libkernel.md
PR 10767 core/libs/tests/GuestKqueue.cpp
docs/dev/debt/libkernel.md

@blawesom

Copy link
Copy Markdown
Contributor Author

Addressed the review in b3e3e8118: the vnode guard is dropped, the $Deleted match is the exact recycle-bin path, the requested vnode-oneshot/periodic/recycle-bin tests are in (with the inotify release check), the debt line for the timer period is added, and the template is filled. One finding did not make it in — the timer EV_DISABLE/EV_ENABLE cycle aborts on the next wait after a single re-fire, so the relaxed guard needs the timer state to re-arm on enable before that test can land; details in the Tested section of the PR body.

@github-actions github-actions Bot added docs needs rebase Conflicts with main, rebase needed labels Oct 17, 2026
@blawesom

Copy link
Copy Markdown
Contributor Author

Re-anchored onto current main (53b717de6) — main had moved past the branch, so the needs-rebase label was fair. The one conflict was the kqueue test runner: main's new LowWaterMarks() call kept alongside this PR's test calls. Full suite green apart from the known main regression (agc_driver_tessellation, Mesa #5444); conventions pass; the kqueue tests including the review-requested ones all pass on the re-anchored head.

@github-actions github-actions Bot added needs rebase Conflicts with main, rebase needed and removed needs rebase Conflicts with main, rebase needed labels Oct 18, 2026

@jotitadev jotitadev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Static review plus a small arm64 repro on macOS (Apple M2 Max, macOS 27.2, Apple clang 21), head 53b717de6. Two problems in the new tests in core/libs/tests/GuestKqueue.cpp.

1. RecycleBinDeleteWatchedFile never runs on any platform. In main() its call is nested inside the non-Windows block:

#ifndef _WIN32                       // line 963
    RenameAndDeleteWatchedFile();
    VnodeOneshotWatchStops();
    PeriodicTimerFiresTwice();
#ifdef _WIN32                        // line 967: always false here
    RecycleBinDeleteWatchedFile();
#endif
#endif

The function itself is defined only under #ifdef _WIN32 (line 760), so the \$Extend\$Deleted\ change in FileEvent.cpp (IsDeleted/SnapshotHints) has no test that actually runs, even though the PR body lists it under Tested. Moving the #ifdef _WIN32 ... #endif block after the closing #endif fixes it.

2. The leak check in VnodeOneshotWatchStops passes vacuously on macOS. InotifyWatchCount (lines 705-716) iterates /proc/self/fd under #ifndef _WIN32. macOS has no /proc, so directory_iterator sets the error code and the count is always 0. That makes Require(InotifyWatchCount() == watchesBefore) (line 743) true regardless of the fix. The macOS host watch is a ::kqueue() per note (FileEvent.cpp:446), closed when StopVnode stops the watch thread, so the same leak is possible there.

Repro: the PR's InotifyWatchCount copied as is, plus 3 deliberately leaked kqueue() descriptors:

InotifyWatchCount before=0 after=0 (err: No such file or directory) -> check passes: yes (vacuous)
kqueue fds via proc_pidinfo before=0 after=3 -> leak detected: yes

Suggestion: gate the current helper on __linux__. On __APPLE__, count PROX_FDTYPE_KQUEUE entries from proc_pidinfo(getpid(), PROC_PIDLISTFDS, ...) (<libproc.h>), which detected the leaked descriptors above. Alternatively, skip the leak assertion where the count is not meaningful, so the test does not claim coverage it does not have.

(The branch also currently conflicts with main.)

AI-assisted: yes (Claude Code)

@adriadam10 adriadam10 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The production change looks right: the deleteEventFunc call mirrors the existing one in CollectOncePerWait, and the exact \$Extend\$Deleted\ match addresses the earlier review. The branch needs the following before it can merge (it also conflicts with main in core/libs/tests/GuestKqueue.cpp and docs/dev/debt/libkernel.md):

  • core/libs/tests/GuestKqueue.cpp (main(), ~line 967): RecycleBinDeleteWatchedFile() is called inside #ifndef _WIN32, so its #ifdef _WIN32 is never true and the $Deleted change has no test that runs. Move the #ifdef _WIN32 block after the closing #endif (as reported by @jotitadev).
  • InotifyWatchCount (~line 706) reads /proc/self/fd; on macOS that directory does not exist, the count is always 0 and the leak assertion passes without checking anything. Gate it on __linux__ and skip the assertion elsewhere (or count kqueue descriptors via proc_pidinfo).
  • The diff also adds DisableAndEnable, Dispatch, ForceOneshot and StressTest (~165 lines) that the description does not mention and that belong to the superseded #6510 feature already covered by DisableAndDispatch on main. Drop them, or move them to their own PR with their own justification. StressTest depends on 4 threads and 1–2 ms sleeps and asserts strict arrival order, which is a flake risk on the CI runners.
  • Cosmetics: the over-indented DisableAndEnable(); call and the double blank line before StressTest.

Please rebase on current main (keep main's LowWaterMarks()/DisableAndDispatch() calls) and re-run guest_kqueue. Happy to re-review once these are addressed.

@blawesom

Copy link
Copy Markdown
Contributor Author

Rebased onto current main (d34da5d69) and addressed the review in 895ca7c6:

  • RecycleBinDeleteWatchedFile() now runs on Windows: its call moved after the closing #endif of the #ifndef _WIN32 block, so the \$Extend\$Deleted\ path is exercised
  • InotifyWatchCount and both leak assertions gate on __linux__; on macOS and Windows the leak check is skipped instead of passing vacuously (macOS has no /proc)
  • DisableAndEnable, Dispatch, ForceOneshot and StressTest are dropped with their main() calls (superseded feat(libkernel): implement EV_DISABLE, EV_DISPATCH and EV_FORCEONESHOT in kevent #6510 feature; StressTest asserted strict arrival order across 4 threads with 1–2 ms sleeps — CI flake risk), and the two includes only they needed are gone
  • The kqueue test-runner conflict resolved keeping main's side (StandardInput, WatchKqueue, edge-triggered calls) alongside this PR's test calls
  • The debt-file conflict resolved keeping both entries (main's new entries plus this PR's EVFILT_TIMER line)

Tested: Linux (CachyOS, kernel 7.2.10, GCC 16.2.1, Ninja, Release, BUILD_TESTING=ON, AMD RX 9070 XT / RADV): guest_kqueue passes on the rebased head; tools/check_conventions.py --base origin/main passes. The Windows-gated RecycleBinDeleteWatchedFile still needs a Windows run to confirm it executes there.

@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 21, 2026
@blawesom

blawesom commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor Author

@adriadam10 — re-requesting your review, as you offered:

Addressed per your review; head is now 3ecd18feaf (rebased, same content as 072d780c66).

  • Rebased onto current main (d6eafa54d0); main's LowWaterMarks() and DisableAndDispatch() are kept.
  • RecycleBinDeleteWatchedFile() now runs on Windows: its #ifdef _WIN32 block sits after the closing #endif of the non-Windows block, so the $Extend$Deleted path is exercised.
  • InotifyWatchCount and both leak assertions gate on __linux__; elsewhere the count is not asserted. I took your "skip the assertion where the count is not meaningful" option rather than proc_pidinfo kqueue counting on macOS — happy to switch if you would rather have real coverage there.
  • DisableAndEnable, Dispatch, ForceOneshot and StressTest are dropped together with their main() calls, so the indentation and blank-line cosmetics went with them.
  • guest_kqueue re-run on the rebased head: Passed (4.2 s) — Linux (CachyOS, kernel 7.2.10, GCC 16.2.1, Ninja, Release, BUILD_TESTING=ON, Ryzen 7 9800X3D).
  • tools/check_conventions.py --base origin/main passes; tools/pr_overlap.py reports only the shared docs/dev/debt/libkernel.md line and the same-file PRs already known open (feat(libkernel): support kevent read and write events on regular files #7088, fix(libkernel): handle kevent timer re-adds and the AIO, LIO, SENDFILE and PROCDESC filters as measured on a PS5 #7653), none of which this builds on.

@blawesom

Copy link
Copy Markdown
Contributor Author

@adriadam10 — one more commit (de497d3563), answering the "happy to dig into the re-arm" offer from the description.

The disable/enable cycle works on this head. The next-wait abort from the earlier round no longer reproduces: it was observed on the pre-rebase head, before main's equeue wait refactor (5cbc46029b). What the cycle does now, measured on this machine (CachyOS, kernel 7.2.10, GCC 16.2.1, Release):

  • a 50 ms periodic timer, disabled immediately after the add, enabled 150 ms later: the first kevent returns at once with data == 3 — the expiries missed while disabled, summed in one step, exactly what FreeBSD 11's filt_timerexpire counts for a disabled note (kn_data++ before the KN_DISABLED check, p_nexttime keeps advancing)
  • the next kevent returns 49 ms later with data == 1 — the phase is kept, not re-armed from the enable

TimerDisableEnableRearm pins this: the missed count, the phase and the udata replacement, plus EV_ADD | EV_ENABLE still throwing. Verified 5 serial runs and 3 rounds of 8 parallel copies; guest_kqueue and guest_equeue_events pass; conventions pass.

One guard change came out of writing it: a timer modify that sets fflags was silently ignored, where FreeBSD 11's kqueue_register copies it onto the note — it now refuses with the other re-adds (measured rules pending), and the debt entry says so. The description's finding paragraph is rewritten accordingly.

@blawesom

Copy link
Copy Markdown
Contributor Author

@adriadam10 — head faece77735 is ready for re-review (it carries your faece77735 late-wake test tolerance; merge base is origin/main aeeaefad98, so the needs rebase label is stale).

Fresh tools/pr_overlap.py --branch followup/6510 run on this head, for the checkbox:

  • PR 9509 adds a debt entry to docs/dev/debt/libkernel.md at the same spot this PR edits — a merge-order conflict in one bullet, not a behaviour overlap.
  • The un-comparable PRs sharing this diff's files implement different behaviour: 7088 adds file data read/write notes, 7121 adds open/read/close notes on watched files, 9575 adds EV_CLEAR across nested kqueues, 9823 resolves stubs/RequireVnodeNotes sources. None touch the EV_ONESHOT retire callback, the \$Extend\$Deleted\$ transition or the timer modify/enable re-arm. Full "shared files not compared" list is in the workflow's earlier comment; I re-checked the four above against the whole diff.

Nawasfadili and others added 5 commits October 24, 2026 11:13
… and follow Windows $Deleted paths

Carries the additive pieces of boykopovar#6510 onto main after its feature was
superseded by boykopovar#5847: the deleteEventFunc invocation when an EV_ONESHOT
note retires in CollectTriggered, the Windows $Deleted path detection
in FileEvent (IsDeleted transitions), the relaxed timer-modify and
SetVnodeNotes guards, and the compatible kevent tests.
…perseded kevent tests

- RecycleBinDeleteWatchedFile now runs on Windows: its call moves out of the
  #ifndef _WIN32 block so the $Extend$Deleted$ path is exercised
- InotifyWatchCount and the two leak assertions gate on __linux__ (macOS has
  no /proc; the count was always 0 and the assertion vacuous)
- DisableAndEnable, Dispatch, ForceOneshot and StressTest drop with their
  main() calls: they belong to the superseded boykopovar#6510 feature, and StressTest
  asserts strict arrival order across 4 threads with 1-2 ms sleeps (CI flake risk)
…d timer's enable re-arm

- a timer change with EV_ADD unset and data 0 keeps the period; one that set
  fflags was silently ignored, where FreeBSD 11's kqueue_register copies it
  onto the note: it now throws with the other re-adds, waiting for the
  measured rules
- EV_ENABLE on a disabled timer reports the expiries missed while disabled in
  one step and keeps the note's phase, as FreeBSD 11's filt_timerexpire counts
  a disabled note and keeps its p_nexttime; TimerDisableEnableRearm pins it
  (5 serial runs and 3 rounds of 8 parallel copies pass)
- the next-wait abort from the review round was observed before main's
  5cbc460 wait refactor and does not reproduce
…he timer debt line off the section end

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 24, 2026
@blawesom

Copy link
Copy Markdown
Contributor Author

@adriadam10 — everything on our side is done: your approval is anchored to the current head 387f0697, conventions is green, mergeStateStatus is CLEAN and the overlap check is recorded above. Merging this needs write access the PR author does not have — could you merge it, or point us at who can? Then #6510's remaining parts land here and its follow-ups can close out.

@BunnyFl

BunnyFl commented Oct 24, 2026

Copy link
Copy Markdown
Contributor

Windows run of guest_kqueue for this PR. The windows CI job was skipped on this head (run 40081325328) and the Tested section lists Linux only, so RecycleBinDeleteWatchedFile had not run on Windows yet.

Build: head 387f069744 merged onto main 30be9b428b (75737b9d2a, no conflicts). Windows 11 Pro 26200, WinLibs GCC 15.2.0, Ninja, Release, BUILD_TESTING=ON. ctest -R '^guest_kqueue$' -V, three runs per variant.

variant result
main passes 3 of 3
main + this PR passes 3 of 3; the log shows RecycleBinDeleteWatchedFile opening its anyps5-kqueue-deleted-…\watched file
main + this PR, FileEvent.cpp back to main fails 3 of 3: abort (0xc0000409) inside RecycleBinDeleteWatchedFile, right after it opens that file

So on Windows the new test passes, and fails without the $Extend\$Deleted change.

One thing I noticed: main() still calls VnodeOneshotWatchStops, PeriodicTimerFiresTwice and TimerDisableEnableRearm inside the #ifndef _WIN32 block, so none of the three runs on Windows, although your 10-21 comment describes the oneshot leak check as skipped on Windows. As a local experiment (not pushed) I moved only those three calls after that block's #endif:

  • with this PR: passes 3 of 3 on Windows;
  • with Equeue.cpp and Kqueue.cpp back to main: fails 3 of 3 at TimerDisableEnableRearm with kevent: changing an existing EVFILT_TIMER not implemented. VnodeOneshotWatchStops and PeriodicTimerFiresTwice pass before it, as the oneshot leak check is Linux-only.

So the three tests run on Windows as they are, if you want them to.

@adriadam10
adriadam10 merged commit c2e087b into boykopovar:main Oct 25, 2026
10 checks passed
@adriadam10

Copy link
Copy Markdown
Collaborator

Checked the EV_ONESHOT retire callback against the existing deleteEventFunc uses in Equeue.cpp, the exact \$Extend\$Deleted\ match, the timer modify guard and its debt line, and that each new test fails without the change (including @BunnyFl's Windows run of guest_kqueue); merged.

adriadam10 added a commit that referenced this pull request Oct 25, 2026
Culprit #7049; failing run 40193493248 (linux guest_kqueue aborted after 6 s inside VnodeOneshotWatchStops).
Watch threads of the previous test close their inotify fds after Stop(), so the baseline count could drop below the
final one and the exact equality never held.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants