From 69cd5ea556684531f0f0be60009337bb70b683e2 Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Sun, 31 May 2026 03:48:50 -0700 Subject: [PATCH] fix: bound unwatchable filesystem roots (#4086) --- ...lesystem-watcher-unwatchable-roots.test.ts | 69 +++++++++++++++++++ src/main/ipc/filesystem-watcher.ts | 23 ++++++- 2 files changed, 89 insertions(+), 3 deletions(-) create mode 100644 src/main/ipc/filesystem-watcher-unwatchable-roots.test.ts diff --git a/src/main/ipc/filesystem-watcher-unwatchable-roots.test.ts b/src/main/ipc/filesystem-watcher-unwatchable-roots.test.ts new file mode 100644 index 000000000..5e625de19 --- /dev/null +++ b/src/main/ipc/filesystem-watcher-unwatchable-roots.test.ts @@ -0,0 +1,69 @@ +import { beforeEach, describe, expect, it, vi } from 'vitest' + +const { handleMock } = vi.hoisted(() => ({ + handleMock: vi.fn() +})) + +vi.mock('electron', () => ({ + ipcMain: { + handle: handleMock + } +})) + +vi.mock('fs/promises', () => ({ + stat: vi.fn() +})) + +vi.mock('@parcel/watcher', () => ({ + subscribe: vi.fn() +})) + +vi.mock('./filesystem-watcher-wsl', () => ({ + createWslWatcher: vi.fn() +})) + +vi.mock('../providers/ssh-filesystem-dispatch', () => ({ + getSshFilesystemProvider: vi.fn() +})) + +import { closeAllWatchers, registerFilesystemWatcherHandlers } from './filesystem-watcher' +import { stat } from 'fs/promises' + +type HandlerMap = Record Promise | unknown> + +describe('filesystem watcher unwatchable root cache', () => { + const handlers: HandlerMap = {} + + beforeEach(async () => { + handleMock.mockReset() + vi.mocked(stat).mockReset() + for (const key of Object.keys(handlers)) { + delete handlers[key] + } + handleMock.mockImplementation((channel, handler) => { + handlers[channel] = handler + }) + registerFilesystemWatcherHandlers() + await closeAllWatchers() + }) + + it('evicts oldest failed local roots while suppressing recent retries', async () => { + const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}) + const sender = { isDestroyed: () => false, send: vi.fn(), once: vi.fn(), id: 1 } + vi.mocked(stat).mockRejectedValue(new Error('missing')) + + for (let i = 0; i < 257; i += 1) { + await handlers['fs:watchWorktree']({ sender }, { worktreePath: `/tmp/missing-${i}` }) + } + expect(stat).toHaveBeenCalledTimes(257) + + await handlers['fs:watchWorktree']({ sender }, { worktreePath: '/tmp/missing-0' }) + expect(stat).toHaveBeenCalledTimes(258) + + await handlers['fs:watchWorktree']({ sender }, { worktreePath: '/tmp/missing-256' }) + expect(stat).toHaveBeenCalledTimes(258) + + warnSpy.mockRestore() + await closeAllWatchers() + }) +}) diff --git a/src/main/ipc/filesystem-watcher.ts b/src/main/ipc/filesystem-watcher.ts index 2a3b27d97..7175fd12a 100644 --- a/src/main/ipc/filesystem-watcher.ts +++ b/src/main/ipc/filesystem-watcher.ts @@ -49,8 +49,23 @@ const watchedRoots = new Map() // @parcel/watcher's ReadDirectoryChangesW doesn't work) are cached so // we don't retry on every worktree switch and spam the console with // repeated "Failed to read changes" / "watchman not found" errors. +const UNWATCHABLE_ROOT_CACHE_MAX = 256 const unwatchableRoots = new Set() +function rememberUnwatchableRoot(rootKey: string): void { + // Why: missing/deleted worktrees can churn through unique paths during a long + // session; keep retry suppression useful without retaining every failed path. + unwatchableRoots.delete(rootKey) + unwatchableRoots.add(rootKey) + while (unwatchableRoots.size > UNWATCHABLE_ROOT_CACHE_MAX) { + const oldest = unwatchableRoots.keys().next().value + if (oldest === undefined) { + break + } + unwatchableRoots.delete(oldest) + } +} + // Why: watcher cleanup is keyed to the renderer WebContents, not to a specific // watched root. One listener per sender avoids MaxListeners warnings when a // workspace has many local and SSH-backed worktrees open. @@ -376,6 +391,7 @@ async function subscribe(worktreePath: string, sender: WebContents): Promise { senderCleanupRegistered.clear() + unwatchableRoots.clear() // Cancel any pending grace-period teardowns — we're tearing down everything. for (const timer of pendingTeardowns.values()) {