Skip to content

reduce macos file monitor dropped-event rescan storms - #18508

Merged
kevinushey merged 5 commits into
mainfrom
bugfix/file-monitor-dropped-events
Aug 11, 2026
Merged

kevinushey merged 5 commits into
mainfrom
bugfix/file-monitor-dropped-events

Conversation

@kevinushey

Copy link
Copy Markdown
Collaborator

Addresses #18507.

On macOS, heavy filesystem churn in a monitored project (builds, git operations, npm ci, ...) can overflow fseventsd's per-client event queue, producing bursts of File monitor events were dropped; rescanning ... warnings, each accompanied by a full recursive rescan of the project tree. The bursts are self-amplifying: the recovery rescan runs inline in the FSEvents callback, blocking the run loop long enough for the queue to overflow again.

Three changes:

  • Source-level exclusions. file_monitor::registerMonitor now accepts a best-effort excludedPaths hint, applied on macOS via FSEventStreamSetExclusionPaths (Linux/Windows ignore it; inotify never watches filtered directories in the first place, and ReadDirectoryChangesW has no equivalent). The project monitor passes its always-ignored high-churn directories (.git, .Rproj.user, .quarto, node_modules, renv/library, packrat/lib), so their events are suppressed before they transit the queue rather than being discarded client-side after delivery. Callers must still exclude these paths via the filter; the hint only reduces queue pressure.

  • Debounced recovery rescans. Dropped-event rescans now run at most once per 10-second interval. Drops arriving inside the interval coalesce into a single deferred trailing rescan, scheduled via a one-shot CFRunLoopTimer on the monitor thread's run loop -- the trailing rescan is required for correctness, since dropped changes can postdate what the previous rescan observed. While a deferred rescan is pending, MustScanSubDirs recursion in per-path processing is also skipped (fseventsd attaches that flag alongside the drop flags, typically on the root path, so honoring it would perform exactly the scan being deferred). Delivered events continue to be processed normally throughout, so prompt updates are unaffected.

  • Longer stream latency. The FSEvents latency is raised from 1s to 2s, doubling fseventsd's temporal coalescing window during sustained churn. Streams use kFSEventStreamCreateFlagNoDefer, so the first event after an idle period is still delivered immediately; only follow-on events batch.

The new FileMonitorTest.RecursiveExcludedPathSuppressesEvents test covers the exclusion plumbing end-to-end (and empirically confirms fseventsd excludes events whose path equals the excluded directory, which is the .git case under directory-granularity streams). The debounce path requires genuine queue overflow and so is not deterministically unit-testable; verification there is by inspection plus observing warning cadence in real sessions.

All --scope core tests pass locally (484 passed, 8 environment-dependent skips).

Three changes to how the macOS file monitor copes with fseventsd queue
overflow (UserDropped / KernelDropped):

- registerMonitor now accepts a best-effort excludedPaths hint, applied
  on macOS via FSEventStreamSetExclusionPaths. The project monitor
  passes its always-ignored high-churn directories (.git, .Rproj.user,
  .quarto, node_modules, renv/library, packrat/lib) so their events are
  suppressed before delivery instead of after, reducing queue pressure.

- dropped-event recovery rescans are debounced: at most one full-tree
  rescan per interval, with drops arriving inside the interval coalesced
  into a single deferred trailing rescan (scheduled on the monitor
  thread's run loop). MustScanSubDirs recursion is skipped while a
  deferred rescan is pending, since it would perform the same scan.

- the FSEvents stream latency is raised from 1s to 2s for more temporal
  coalescing in fseventsd during sustained churn; NoDefer keeps isolated
  changes delivered immediately.

Addresses #18507
@kevinushey kevinushey added this to the 2026.09 (Autumn Hawkbit) milestone Aug 11, 2026
@github-actions

github-actions Bot commented Aug 11, 2026 •

Copy link
Copy Markdown

RStudio E2E - all platforms

All tests passed!

#################### 100% pass rate

Passed Failed Skipped Flaky
2076 0 385 6
Platform ✅ Passed ❌ Failed ⚠️ Skipped 🔁 Flaky
✅ auth-teardown 2 0 8 0
✅ desktop-linux 507 0 99 1
✅ desktop-macos 520 0 86 2
✅ desktop-windows 521 0 90 2
✅ server-linux 504 0 94 1
✅ setup 22 0 8 0
View / download the full report

View report online -- opens in the browser, no download needed. Available for 14 days.

Download playwright-report.zip -- extract and open index.html

Includes screenshots, traces, and step-by-step details for every test. Expires in 15 days.


All platforms - commit 78b3e865c4b6d44835b318f4ee3efe8378b2c757 - Run #531

- retry a failed deferred recovery rescan instead of silently dropping
  it: batches delivered while the timer was pending skipped their
  MustScanSubDirs recursion on the promise the rescan would cover them
- arm the rescan debounce only from a successful rescan, so a failed
  attempt no longer suppresses the prompt retry
- route MustScanSubDirs-only batches (delivered without the drop flags
  when fseventsd coalesces hierarchically) through the same debounced
  rescan, so they cannot trigger unbounded back-to-back full-tree scans
renv's staged installs (the default) write full package payloads into
renv/staging before promoting them into the cache; with the cache
enabled, renv/library mostly receives one symlink per package. staging
is therefore the heavy-churn directory in the renv-install scenario
from #18507. it is already unconditionally ignored by
fileMonitorFilter (via '/renv/staging/'), so source-level exclusion is
semantics-preserving, and the list stays within the FSEvents limit of
8 paths.
CFAbsoluteTimeGetCurrent() is wall-clock time. if the clock steps
backward between recovery rescans (ntp correction, vm snapshot
restore), 'elapsed' goes negative and the deferred rescan would be
scheduled interval + |step| in the future -- with MustScanSubDirs
recursion suppressed the whole time on the promise of that rescan.
clamp so the delay never exceeds one debounce interval.
@gtritchie

Copy link
Copy Markdown
Member

Code review findings

Findings from an automated review of this PR, limited to things a user would actually notice.

Important

  1. src/cpp/core/system/file_monitor/MacFileMonitor.cpp:530 — MustScanSubDirs is now treated the same as a dropped event, so a routine "this directory changed a lot" signal from fseventsd triggers a rescan of the whole project and logs a "File monitor events were dropped or coalesced" warning. Before this change, that flag caused a scan of just the flagged directory, and nothing at all when the project filter already ignored that directory. Concretely: during a long revdep check, churn under revdep/checks/... used to be free (the filter rejects those paths, so the old per-path loop skipped them); now it costs a full-project rescan and a warning roughly every 10 seconds for the length of the run — the same warning spam this PR is trying to remove, from events that aren't drops. Worth separating the two: keep the scoped MustScanSubDirs handling and route only the real drop flags into the debounced full rescan.

  2. src/cpp/core/system/file_monitor/MacFileMonitor.cpp:411 — when the deferred rescan fails, droppedRescanTimerCallback schedules another one, forever, with no attempt limit and no check for whether the error can ever succeed. If a user renames or deletes the project directory while a rescan is pending (or a permission change makes the scan fail), the monitor retries every 10 seconds for the rest of the session and logs a warning each time. The old code rescanned once and moved on, so a failed recovery never repeated. Suggest capping the retries, or not retrying on errors that won't clear (e.g. the root no longer exists).

Suggestions

  1. src/cpp/core/system/file_monitor/MacFileMonitor.cpp:241 — the 1s → 2s latency bump applies to every watch, not just the recursive project monitor this PR is about. The Files pane (SessionFilesListingMonitor.cpp:85) and the preferences file watcher (PrefLayer.cpp:340) both use non-recursive watches. NoDefer means the first change after an idle period still arrives immediately, but anything that follows within the window now shows up up to 2 seconds late instead of 1 — so a burst of file changes leaves the Files pane stale for twice as long. Gating the longer latency on recursive would keep the change where it's needed.

@gtritchie gtritchie left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM! Some non-blocking feedback.

- MustScanSubDirs without a drop flag returns to the scoped, filter-gated
  per-path recursive scan (pre-PR behavior), except when flagged on the
  stream root, where the scoped scan would be a full-tree scan anyway;
  only drops and root-level coalescing route through the debounced rescan
- deferred recovery rescans give up after 3 consecutive failures instead
  of retrying every 10 seconds forever; a later dropped event re-triggers
  recovery through the normal path
- non-recursive watches (Files pane, prefs) keep the historical 1s
  FSEvents latency; the 2s bump applies only to recursive watches
@kevinushey

Copy link
Copy Markdown
Collaborator Author

Thanks -- all three applied in 78b3e86.

  1. MustScanSubDirs without a drop flag is back to the scoped, filter-gated per-path recursion (pre-PR behavior), with one carve-out: when the flag lands on the stream root itself, the scoped scan would be a full-tree scan anyway, so that case still routes through the debounced rescan. That keeps filter-ignored churn (e.g. revdep/checks) free while preventing root-level coalescing from re-introducing unbounded back-to-back full-tree scans, which was why the flag got folded into the rescan path in the first place.

  2. Deferred recovery rescans now give up after 3 consecutive failures (kMaxDroppedRescanAttempts), with an error logged on the final attempt. The inline event-driven attempts were already non-self-sustaining (a failed inline rescan doesn't schedule a retry), so the cap fully bounds the loop; any later dropped event re-triggers recovery through the normal path.

  3. The 2s latency now applies only to recursive watches; non-recursive watches (Files pane listing, prefs monitors) keep the historical 1s.

@kevinushey
kevinushey merged commit 22fd5b1 into main Aug 11, 2026
15 of 16 checks passed
@kevinushey
kevinushey deleted the bugfix/file-monitor-dropped-events branch August 11, 2026 17:44
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.

2 participants