Skip to content

Filter configuration watcher events without resolving symlinks per event - #1395

Merged
lujjjh merged 2 commits into
linearmouse:mainfrom
anandghegde:fix-1368-config-watcher-cpu
Sep 14, 2026
Merged

lujjjh merged 2 commits into
linearmouse:mainfrom
anandghegde:fix-1368-config-watcher-cpu

Conversation

@anandghegde

@anandghegde anandghegde commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor

Credit to @mhaslinsky for the diagnosis in #1368.

Cause

FileWatcher watches the parent of each configuration directory, so with ~/.config/linearmouse/linearmouse.json it receives every event under ~/.config (and under ~/Library/Application Support for the other candidate path). For each callback it canonicalized every event path (readlink/lstat per path component) on the main queue, and then rebuilt the watch roots, before filtering the events out.

Fix

The watch roots and the candidate paths stay as they are, so deleting the higher-priority file and retargeting a symlinked configuration directory keep working. Only the filtering changes:

  • FSEvents reports paths with their directories already resolved; only a symlink in the last component differs from its canonical form. The paths that matter (the file and its directory with the last symlink preserved, their canonical forms, and the exact parent) are resolved once when the stream is updated, and event paths are compared against them as reported.
  • The stream is refreshed only after an event that touches the watched files or their ancestors, because only those can move the roots.

Testing

  • New FileWatcherTests: sibling-directory events are not relevant; a symlinked configuration directory is; deleting the higher-priority file is reported; retargeting a symlinked directory is reported, and so is a later edit in the new target.

  • xcodebuild test -project LinearMouse.xcodeproj -scheme LinearMouse -destination 'platform=macOS': 924 tests, 0 failures. FileWatcherTests with -test-iterations 20: all passed.

  • swiftformat --lint and swiftlint lint on the changed files: clean.

  • Command-line harness compiled with FileWatcher.swift, watching <tmp>/a/linearmouse/linearmouse.json and <tmp>/b/linearmouse/linearmouse.json while a script rewrites files in a sibling other-app directory in a tight loop. CPU time from getrusage over 15 s:

    main this PR
    writer in a 58.5% of a core 0.3%
    writers in a and b (b/linearmouse missing) 106.3% 0.5%
  • Same harness, both regressions from review: deleting the higher-priority file fires a change callback, and retargeting a symlinked linearmouse directory fires one for the retarget and another for a later edit in the new target. Both match main.

Fixes #1368

🤖 Generated with Claude Code

https://claude.ai/code/session_01MA8JGs7pCoXFL2W6jEJ8sD

The watcher added in linearmouse#1209 watched the parent of the configuration
directory for every candidate path, so the whole of ~/.config (and
~/Library/Application Support) was streamed, and every unrelated event
was canonicalized on the main queue. Watch only the active configuration
file's directory and fall back to the nearest existing ancestor while it
is missing; the stream narrows back once the directory is recreated.

Fixes linearmouse#1368
@lujjjh

lujjjh commented Sep 14, 2026

Copy link
Copy Markdown
Collaborator

Thanks! It seems that there are two regressions:

  • When both config files exist, deleting the higher-priority file no longer reloads the fallback config.
  • When the config directory is a symlink, pointing it to a different directory can stop hot reload, even for later edits in the new directory.

…symlinks

Narrowing the watch to the active file's own directory broke two cases:
deleting the higher-priority configuration file no longer reloaded the
fallback, and retargeting a symlinked configuration directory stopped hot
reload. Restore the previous watch roots and both candidate paths.

The CPU cost in linearmouse#1368 came from canonicalizing every event path and
refreshing the stream after every callback. FSEvents reports paths with
their directories already resolved, so resolve the watched paths once
when the stream is updated and compare event paths as reported. Refresh
the stream only after an event that touches the watched files.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01MA8JGs7pCoXFL2W6jEJ8sD
@anandghegde anandghegde changed the title Watch the configuration directory instead of its parent for hot reload Filter configuration watcher events without resolving symlinks per event Sep 14, 2026
@anandghegde

Copy link
Copy Markdown
Contributor Author

Thanks, you're right on both. Narrowing the watch was the wrong lever. 7dcbacf goes back to the original watch roots and both candidate paths, and fixes the CPU where it was spent instead: event paths are now compared against paths resolved once when the stream is updated (FSEvents already reports directories resolved), and the stream is only refreshed after an event that touches the configuration files.

Both cases have tests now (testReportsDeletionOfHigherPriorityFile, testReportsChangesAfterSymlinkedDirectoryIsRetargeted), and I checked them against main and the previous commit with a small harness: the previous commit missed both, this one reports them like main. Under a busy sibling directory the watcher uses 0.3–0.5% of a core versus 58–106% on main. I've updated the description with the details.

@lujjjh
lujjjh merged commit 56c5ea7 into linearmouse:main Sep 14, 2026
1 check passed
@lujjjh

lujjjh commented Sep 14, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the update! Looks good to me.

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.

High CPU usage in LinearMouse 0.11.4 due to broad FSEvents configuration watches

2 participants