Repository navigation
fix(watcher): a deleted link target no longer pins a CPU core on Windows - #2938
Merged
Merged
Conversation
colbymchenry
force-pushed
the
claude/fix-watcher-gone-link-target
branch
3 times, most recently
from
October 27, 2026 22:58
21aa469 to
ece95fe
Compare
On Windows, libuv reports a watched folder that is being deleted by the folder's own absolute path (\\?\D:\...) and re-arms the read, which fails the same way at once: about 70,000 events in half a second until the stream is closed. The watcher gives each directory link in a project its own recursive stream, so deleting a link's target outside the project started that storm. handleDirEvent joined the absolute name onto the watched folder, got a path that doesn't exist, scheduled a full sync and nothing else. The stream was never closed, the server held a core until restart, and the storm kept re-arming the debounce, so no sync ever ran: the deleted files stayed in the index and later edits never auto-synced. The project root's stream had the same blind spot. Fix: handleDirEvent, which both stream callbacks use, now treats an absolute name as the watched folder itself reporting it is gone. The first such event on a stream arms one check 200 ms later; the rest of the burst finds it armed. The check closes a link's stream when the link no longer leads to the folder it watched (deleted, or pointed elsewhere) and re-reads the tree, whose full sync drops the link's files; a link that leads somewhere live is watched anew. A project root that is gone degrades the watcher with a "project folder was deleted or moved away" reason, which closes every stream. A normal event pays one path.isAbsolute check. Validation (Windows 11, Node 22.22, libuv 1.51): - Raw probe: fs.watch on a junction whose target is deleted gets 70,009 events named \\?\<the junction> in 500 ms (499 ms CPU), and none after close(). - Real MCP server over a project with a junction libs -> outside, outside deleted: main used 3484, 3406, 3391, 3281 ms of CPU per 3 s window and kept the linked symbol; an edit afterwards never synced. With the fix: 296, 0, 0, 79 ms; the linked symbol left the index; the edit synced. - New watcher tests: a fake fs.watch link storm (10,000 self-named events cost no fs call, the stream closes once, a full sync runs, the project stays watched), a fake root storm (degrades once, closes the stream), and a Windows-gated real junction whose storm must go quiet. All 3 fail on main's watcher.ts; watcher.test.ts passes 61/61. Reported in PR #2487 by @L0garithmic; this reimplements the fix independently. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
colbymchenry
force-pushed
the
claude/fix-watcher-gone-link-target
branch
from
October 27, 2026 23:34
ece95fe to
3dfb661
Compare
This was referenced Oct 27, 2026
Merged
bompus
added a commit
to bompus/codegraph
that referenced
this pull request
Nov 2, 2026
…ows (colbymchenry#2938) Ported from upstream 6162d87
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
On Windows, deleting the folder that a directory link in a project points to (a junction to a library outside the project, say) no longer makes the CodeGraph server pin a CPU core until it is restarted. Before, the storm also kept the server from picking up later edits. Now the link's files drop out of the index and the rest of the project keeps auto-syncing. A project folder that disappears while watched turns auto-sync off with a clear reason.
Root cause
On Windows, libuv reports a watched folder that is being deleted by the folder's own absolute path,
\\?\D:\…\proj\libs. It then re-arms the read, which fails the same way at once. Measured on Node 22.22 / libuv 1.51: 70,009 such events in 500 ms of a plainfs.watch, untilclose().The watcher gives each directory link in a project its own recursive stream, so deleting a link's target outside the project starts that storm.
FileWatcher.handleDirEventjoined the absolute name onto the watched folder, got a path that doesn't exist, and scheduled a full sync. Nothing closed the stream.The storm also re-armed the debounce on every event, so no sync ever ran: the deleted files stayed in the index, and later edits were never synced. The project root's stream (
startRecursive) had the same blind spot.Fix
handleDirEvent, which both stream callbacks (startRecursive,watchDirectory) use, now treats an absolute name as the watched folder itself reporting it is gone:unwatchDir(dir)andscheduleTreeRefresh(). The refresh's full sync drops the link's files, and a link that leads somewhere live is watched anew.A normal event pays one
path.isAbsolutecheck. Pending checks are cleared inunwatchDirandstop.Validation (Windows 11, Node 22.22, libuv 1.51)
Real MCP server (direct mode,
projectPath) over a fresh fixture:projwith a junctionlibs→outside, indexed.outsideis then deleted. CPU is for the server's process tree.src/a.tsafterwardsAuto-synced 1 file(s))An earlier run with the original probe gave the same picture: main +3376/+3124/+3094/+3125 ms, fix +375/+234/0/0 ms.
New tests in
watcher.test.ts:fs.watch): 10,000 self-named events cost no fs call; the stream closes once; a full sync runs; the dangling link is not watched again; the project stays watched and healthy. A self-named event while the link still leads to its folder keeps the stream.fs.watch): degrades once, with the reason, and closes the stream; a late burst changes nothing.watcher.ts, which made 30,000 fs calls for the burst, never degraded, and never went quiet. The file passes 61/61 with the fix.Full suite on this commit: 767 files passed, 6 skipped; 9,151 tests passed, 0 failed, 85 skipped (Windows 11, kernel built,
CODEGRAPH_KERNEL_EXPECT=1,CODEGRAPH_NO_DAEMONunset).Notes
fs.watchtest.Credit
Reported in #2487 by @L0garithmic; this reimplements the fix independently. Their PR identified the libuv storm and the throttle shape, and guarded the project root. This one also covers the directory-link streams, where the storm actually happens with an index open.
🤖 Generated with Claude Code