From 64487db71a743bc76868b0404c80d637bd9edc6a Mon Sep 17 00:00:00 2001 From: "Hyeonjae.Jeong" Date: Thu, 12 Nov 2026 13:47:10 +0900 Subject: [PATCH 1/2] fix(workspace-fs): wait for released watchers during shutdown --- .../src/watch-cancellation.test.ts | 43 +++++++++++++++++++ packages/workspace-fs/src/watch.ts | 19 +++++++- 2 files changed, 60 insertions(+), 2 deletions(-) diff --git a/packages/workspace-fs/src/watch-cancellation.test.ts b/packages/workspace-fs/src/watch-cancellation.test.ts index d63ba1ca84b..4c7918a8038 100644 --- a/packages/workspace-fs/src/watch-cancellation.test.ts +++ b/packages/workspace-fs/src/watch-cancellation.test.ts @@ -148,3 +148,46 @@ test("watcher readiness refreshes edits made during the background scan", async expect(entries.map((entry) => entry.name)).toContain("during-scan.txt"); await stream.return?.(); }); + +for (const phase of ["attaching", "attached"] as const) { + test(`close waits for the last aborted listener's ${phase} native watch`, async () => { + const rootPath = await root(); + const controller = new AbortController(); + const entered = deferred(); + const gate = deferred(); + let detached = false; + const manager = new FsWatcherManager({ + backend: { + name: "test", + async subscribe() { + entered.resolve(); + if (phase === "attaching") await gate.promise; + return { + async unsubscribe() { + await gate.promise; + detached = true; + }, + }; + }, + }, + }); + managers.push(manager); + const subscription = manager + .subscribe( + { absolutePath: rootPath, signal: controller.signal }, + () => {}, + ) + .catch((error) => { + if (!(error instanceof Error && error.name === "AbortError")) + throw error; + }); + await entered.promise; + if (phase === "attached") await subscription; + controller.abort(); + const closedAfterDetach = manager.close().then(() => detached); + await new Promise((resolve) => setImmediate(resolve)); + gate.resolve(); + expect(await closedAfterDetach).toBe(true); + await subscription; + }); +} diff --git a/packages/workspace-fs/src/watch.ts b/packages/workspace-fs/src/watch.ts index bb52ec4b2b5..830fd556803 100644 --- a/packages/workspace-fs/src/watch.ts +++ b/packages/workspace-fs/src/watch.ts @@ -277,6 +277,8 @@ export class FsWatcherManager { private readonly ignore: string[]; private readonly listGitIgnoredDirs?: FsWatcherManagerOptions["listGitIgnoredDirs"]; private readonly findNestedRoots: typeof findNestedRepoRoots; + // Last-listener cleanup leaves the path maps immediately so it can be rewatched. + private readonly releasing = new Set>(); private readonly pending = new Map< string, { @@ -381,11 +383,12 @@ export class FsWatcherManager { if (pending && this.pending.get(absolutePath) === pending) { this.pending.delete(absolutePath); pending.controller.abort(); + await this.trackRelease(Promise.allSettled([pending.promise])); } const current = this.watchers.get(absolutePath); if (current?.listeners === listeners) { this.watchers.delete(absolutePath); - await this.disposeWatcherState(current); + await this.trackRelease(this.disposeWatcherState(current)); } }; const onAbort = () => { @@ -402,6 +405,15 @@ export class FsWatcherManager { } } + private async trackRelease(work: Promise): Promise { + this.releasing.add(work); + try { + await work; + } finally { + this.releasing.delete(work); + } + } + async close(): Promise { const pending = [...this.pending.values()]; this.pending.clear(); @@ -409,7 +421,10 @@ export class FsWatcherManager { await Promise.allSettled(pending.map((entry) => entry.promise)); const states = Array.from(this.watchers.values()); this.watchers.clear(); - await Promise.all(states.map((state) => this.disposeWatcherState(state))); + await Promise.all([ + ...this.releasing, + ...states.map((state) => this.disposeWatcherState(state)), + ]); } private async disposeWatcherState(state: WatcherState): Promise { From cc818b2c3e78c9b9d51e56229e3584f64ad4cb72 Mon Sep 17 00:00:00 2001 From: Kiet <31864905+Kitenite@users.noreply.github.com> Date: Sun, 22 Nov 2026 02:33:45 +0000 Subject: [PATCH 2/2] fix(workspace-fs): bound the wait on an aborted watcher attach close() waits for an aborted attach's native subscribe. With parcel's one-at-a-time native calls, a hung unsubscribe can leave that subscribe stuck forever and hang host-service shutdown. Use the same 5 s limit that already bounds unsubscribe. --- .../src/watch-cancellation.test.ts | 28 +++++++++++++++ packages/workspace-fs/src/watch.ts | 34 ++++++++++++++----- 2 files changed, 53 insertions(+), 9 deletions(-) diff --git a/packages/workspace-fs/src/watch-cancellation.test.ts b/packages/workspace-fs/src/watch-cancellation.test.ts index 4c7918a8038..952a106a6eb 100644 --- a/packages/workspace-fs/src/watch-cancellation.test.ts +++ b/packages/workspace-fs/src/watch-cancellation.test.ts @@ -191,3 +191,31 @@ for (const phase of ["attaching", "attached"] as const) { await subscription; }); } + +test("close does not wait forever for an aborted listener's hung native subscribe", async () => { + const rootPath = await root(); + const controller = new AbortController(); + const entered = deferred(); + const manager = new FsWatcherManager({ + teardownTimeoutMs: 5, + backend: { + name: "test", + subscribe() { + entered.resolve(); + return new Promise(() => {}); + }, + }, + }); + managers.push(manager); + void manager + .subscribe({ absolutePath: rootPath, signal: controller.signal }, () => {}) + .catch(() => {}); + await entered.promise; + controller.abort(); + const hung = new Promise((resolve) => { + setTimeout(() => resolve("hung"), 500).unref?.(); + }); + expect(await Promise.race([manager.close().then(() => "closed"), hung])).toBe( + "closed", + ); +}); diff --git a/packages/workspace-fs/src/watch.ts b/packages/workspace-fs/src/watch.ts index 53409f2290f..bad44b53517 100644 --- a/packages/workspace-fs/src/watch.ts +++ b/packages/workspace-fs/src/watch.ts @@ -250,21 +250,31 @@ interface WatcherState { throttler: ThrottledWorker; } +const TEARDOWN_TIMEOUT_MS = 5_000; + +async function settleWithin( + work: Promise, + timeoutMs: number, +): Promise { + await Promise.race([ + work.catch(() => {}), + new Promise((resolve) => { + const timer = setTimeout(resolve, timeoutMs); + timer.unref?.(); + }), + ]); +} + // A dead FSEvents stream's unsubscribe can hang forever (observed after the // watch root is deleted out from under it); never let it block teardown. async function unsubscribeQuietly( subscription: NativeWatchSubscription | null, + timeoutMs = TEARDOWN_TIMEOUT_MS, ): Promise { if (!subscription) { return; } - await Promise.race([ - subscription.unsubscribe().catch(() => {}), - new Promise((resolve) => { - const timer = setTimeout(resolve, 5_000); - timer.unref?.(); - }), - ]); + await settleWithin(subscription.unsubscribe(), timeoutMs); } function internalToFsWatchEvent(event: InternalWatchEvent): FsWatchEvent { @@ -323,6 +333,8 @@ export interface FsWatcherManagerOptions { overflowRescanMaxMs?: number; overflowBackoffResetMs?: number; growthReattachMinMs?: number; + /** Limit on native subscribe and unsubscribe during teardown. Test-only override. */ + teardownTimeoutMs?: number; now?: () => number; } @@ -349,6 +361,7 @@ export class FsWatcherManager { private readonly overflowRescanMaxMs: number; private readonly overflowBackoffResetMs: number; private readonly growthReattachMinMs: number; + private readonly teardownTimeoutMs: number; private readonly now: () => number; private readonly watchers = new Map(); /** @@ -394,6 +407,7 @@ export class FsWatcherManager { options.overflowBackoffResetMs ?? OVERFLOW_BACKOFF_RESET_MS; this.growthReattachMinMs = options.growthReattachMinMs ?? IGNORE_GROWTH_REATTACH_MIN_MS; + this.teardownTimeoutMs = options.teardownTimeoutMs ?? TEARDOWN_TIMEOUT_MS; this.now = options.now ?? Date.now; } @@ -442,7 +456,9 @@ export class FsWatcherManager { if (pending && this.pending.get(absolutePath) === pending) { this.pending.delete(absolutePath); pending.controller.abort(); - await this.trackRelease(Promise.allSettled([pending.promise])); + await this.trackRelease( + settleWithin(pending.promise, this.teardownTimeoutMs), + ); } const current = this.watchers.get(absolutePath); if (current?.listeners === listeners) { @@ -510,7 +526,7 @@ export class FsWatcherManager { state.throttler.dispose(); const subscription = state.subscription; state.subscription = null; - await unsubscribeQuietly(subscription); + await unsubscribeQuietly(subscription, this.teardownTimeoutMs); } /**