Skip to content

fix(watch): one change makes one deadline for every service it concerns (#233) - #239

Merged
7schmiede merged 6 commits into
mainfrom
fix/fsevents-twice
Oct 23, 2026
Merged

7schmiede merged 6 commits into
mainfrom
fix/fsevents-twice

Conversation

@7schmiede

@7schmiede 7schmiede commented Oct 23, 2026 •

Copy link
Copy Markdown
Member

On macOS, a_job_runs_once_for_two_candidates failed now and then (seed ran 3 times, expected 2), and once in this PR's CI lifecycle_services_only did too (prep 3 times). Both come from how the watcher's reports of one edit meet the waiting in the daemon. Neither was really FSEvents reporting the same change twice, 200 ms apart.

Cause

1. One change, two deadlines. A candidate's pipeline (and Supervisor::debounce) checked every 200 ms whether its quiet period was over, counting from when its thread started. The OS reports one edit several times within milliseconds: notify sends one event per FSEvents flag (created, modified, metadata), and inotify reports a save's truncate and its write separately. Each report re-arms every service it concerns, at about the same moment. In the failing run, b fired exactly 2.010 s after its first report, so b's wait was effectively not moved. a's thread had started about 1 ms before the repeat report, so it missed its deadline at the tenth check and fired at the eleventh, 240 ms later. By then the seed run for b had already started, so a counted it as older than its own build and ran seed again.

2. A report that arrives after a restart has begun. lifecycle_services_only edits Program.cs and runs devstack restart api straight away. The restart takes in the changes reported so far, then builds the tree including the edit. FSEvents reported the edit 6 ms after that, so the change looked new. Its candidate's 2 s wait ended while the restart was still switching over. Because the restart's build was not main yet, the candidate was not dropped as unchanged: it took over again and ran prep a third time. Polling every 200 ms would have fired at the same moment, so this race was already there. It only bites when the restart takes longer than the 2 s wait.

Fix

  1. The pipeline and the debounce now sleep until the deadline itself (quiet_left), wherever a later report moved it. A repeated report moves the deadline by the gap between the reports, and by the same amount for every service, so services that see the same edit still fire together. This holds on every OS, and restart_on restarts, the lane copy and the .gitignore reload use the same debounce.
  2. Once the wait is over, a candidate waits while a restart of its own service is still under way. It then builds the build main already has and is dropped as unchanged; this is the "checked right after the switch" that lifecycle_services_only has expected since feat: macOS as a developer platform in preview, Homebrew, arm64 Linux (ADR-0054) #225. If the restart fails, the candidate proceeds as before.

Options I did not take (the reasoning is in the comment on quiet_left):

  • Drop a report whose file has the same mtime and length as at the last report. This would ask the file system about every event, which watch.rs avoids (LIST_NEW_DIRS). FAT/exFAT store mtime only to 2 s, and a lane's copy, which waits only 150 ms, would miss a second save of the same length. It would also not have prevented either failure.
  • Don't move a deadline for a path that is already waiting. On every OS, this would start a build while an editor is still saving.

Tests

  • a_change_reported_twice_makes_one_deadline_for_every_service: one path reported for a and b, and again ~200 ms later. Both services must take their change at the second report's time plus the quiet period, less than 100 ms late and less than 100 ms apart. With the old 200 ms polling, it fails deterministically: a fired 183ms after its deadline, 3 of 3 runs.
  • a_candidate_waits_for_a_restart_under_way: a candidate waits past its quiet period while a restart is under way, and fires within a second after the restart ends.

macOS

A temporary job on macos-14 (removed again in this branch) ran a_job_runs_once_for_two_candidates 10 times in a row with DEVSTACK_INTEGRATION=1, and lifecycle_services_only 5 times: 10/10 and 5/5 passed. A first round with only fix 1 was also 10/10. In that run integration (macos-14) showed the restart race (fix 2).

One run of check (macos-14) failed in devstack-proc's the_machine_reports_memory_and_cpu_time (the host's CPU ticks did not advance in 300 ms). That test is unrelated to this change.

Closes #233

🤖 Generated with Claude Code

7schmiede and others added 6 commits October 23, 2026 13:45
…ns (#233)

A candidate's pipeline and a debounce looked for the end of their quiet
period every 200 ms, counted from when their thread began. The OS reports
one change more than once (notify makes an event per FSEvents flag;
FSEvents may send a second record; inotify reports a save's truncation and
write apart). Each report re-arms every service it concerns at about the
same moment, but a thread that began just before the repeat missed its
deadline at one look and fired at the next, 200 ms later than another
service's. Two services after the same job then built for one edit 240 ms
apart, and the job ran again for the second (macOS CI).

The waiters now sleep until the deadline itself, wherever a later report
moved it: a repeat moves it by the time between the reports, the same for
every service.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…233)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…233)

`devstack restart` right after an edit builds the tree with the edit, but
FSEvents reported it a few milliseconds after the restart had taken in the
changes so far. Its candidate came out of the quiet period while the
restart was still switching, was no build of main yet, and took over
again, running the jobs the service comes after once more (macOS CI,
`lifecycle_services_only`). Once the quiet period is over, a candidate now
waits for a restart of its service under way; built after it, it is the
build main has and is dropped as unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@7schmiede
7schmiede marked this pull request as ready for review October 23, 2026 12:03
@7schmiede
7schmiede merged commit d62a229 into main Oct 23, 2026
13 checks passed
@7schmiede
7schmiede deleted the fix/fsevents-twice branch October 23, 2026 12:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

macOS: FSEvents reports a change twice; the second report re-arms a candidate's debounce

1 participant