From d500ea0134a148bc5214b1d258fe031e52cc517b Mon Sep 17 00:00:00 2001 From: Adib Hanna Date: Fri, 20 Nov 2026 08:57:21 -0600 Subject: [PATCH] Fix(themes): a stopped themes watcher drops reports still in flight 30b9bc2b cleared the debounce on stop, and main's CI for the 2.89.0 release commit still failed on the Ubuntu arm64 runner: its new test saw a report 450 ms after the stop (`expected [ 450 ] to deeply equal []`), while the same commit's PR run and every local run passed. Clearing the timer covers only an edit that had not fired yet. Two paths stayed open: a debounce that had already fired was still reading the themes folder when the stop came, and its list resolved after it; and chokidar can report an edit while it closes, which armed a fresh timer. Each watcher now reports only while it is still the current one. The stop marks it gone before closing it, and the timer, the read it starts and the edit handler all check that before acting, so nothing a stopped or replaced watcher saw reaches the listener, however late it resolves. A failed read is logged instead of left as an unhandled rejection. The test is unchanged: it steps the stop across the whole window, and the slow runner that caught this is where it shows. --- apps/desktop/src/main/custom-themes.ts | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/apps/desktop/src/main/custom-themes.ts b/apps/desktop/src/main/custom-themes.ts index aaf4b6b0..21693871 100644 --- a/apps/desktop/src/main/custom-themes.ts +++ b/apps/desktop/src/main/custom-themes.ts @@ -378,8 +378,12 @@ export function resolveThemeAssetPath(slug: string, relPath: string): string | n } let watcher: ReturnType | null = null -// The debounce outlives no watcher: a stop or a restart clears it, or an edit -// just before the stop reports a list after it (into a torn-down test, once). +// Reports belong to the watcher that saw the edit, and only while it is still +// the current one. A stop or a restart clears the pending debounce, and a +// watcher that is no longer current drops whatever still reaches it: an edit +// chokidar reports while it closes, or a folder read that was already under +// way and resolves after the stop (into a torn-down test, once, on a slow +// runner). let pendingReport: ReturnType | null = null function clearPendingReport(): void { @@ -393,11 +397,21 @@ export function startWatchingCustomThemes(onChange: (themes: CustomTheme[]) => v const dir = getCustomThemesDir() void watcher?.close() clearPendingReport() + const current = (): boolean => watcher === w const fire = (): void => { + if (!current()) return clearPendingReport() pendingReport = setTimeout(() => { pendingReport = null - void listCustomThemes().then(onChange) + if (!current()) return + listCustomThemes().then( + (themes) => { + if (current()) onChange(themes) + }, + (error) => { + if (current()) console.warn('[zen] could not list custom themes:', error) + } + ) }, 200) } const w = chokidar.watch(dir, { @@ -420,9 +434,11 @@ export function startWatchingCustomThemes(onChange: (themes: CustomTheme[]) => v /** Test/teardown hook: stop the themes watcher. */ export async function stopWatchingCustomThemes(): Promise { - clearPendingReport() - await watcher?.close().catch(() => {}) + // No longer current before the close, so nothing it reports meanwhile lands. + const stopping = watcher watcher = null + clearPendingReport() + await stopping?.close().catch(() => {}) } /** What to reveal in the file manager: a theme's `theme.css` when the slug is