YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context
Rust agentsagiaiai-memoryaudio-recordingcomputer-visionhermeshermes-agentllmlocal-ailocal-firstmachine-learningmcpmultimodalopenclawprivacyrewindscreen-recordingspeech-to-textycombinator
fix(health-check): fix duplicate intervals and broken debounce (#2891)
Divanshu4ab30a917ab2
diff --git a/apps/screenpipe-app-tauri/lib/hooks/use-health-check.tsx b/apps/screenpipe-app-tauri/lib/hooks/use-health-check.tsxindex 1e010936c..ff2b89c72 100644--- a/apps/screenpipe-app-tauri/lib/hooks/use-health-check.tsx+++ b/apps/screenpipe-app-tauri/lib/hooks/use-health-check.tsx@@ -166,11 +166,8 @@ export function useHealthCheck() {serverDownTimerRef.current = null;}, SERVER_DOWN_GRACE_PERIOD_MS);}-- // Start retry interval if not already running- if (!retryIntervalRef.current) {- retryIntervalRef.current = setInterval(fetchHealth, 10000);- }+ // Retry interval is started in onclose, which always fires after onerror.+ // Starting it here too would create duplicate intervals.};ws.onclose = (event) => {@@ -199,8 +196,14 @@ export function useHealthCheck() {}, SERVER_DOWN_GRACE_PERIOD_MS);}- // Only start retry if this wasn't a manual close- if (!retryIntervalRef.current && event.code !== 1000) {+ // Only start retry if this wasn't a manual close (code 1000).+ // Always clear any stale interval first — reconnect cycles can leave one+ // behind if onopen cleared it and then the fresh socket immediately dies.+ if (event.code !== 1000) {+ if (retryIntervalRef.current) {+ clearInterval(retryIntervalRef.current);+ retryIntervalRef.current = null;+ }retryIntervalRef.current = setInterval(fetchHealth, 10000);}};@@ -216,23 +219,22 @@ export function useHealthCheck() {}, SERVER_DOWN_GRACE_PERIOD_MS);}- if (!retryIntervalRef.current) {- retryIntervalRef.current = setInterval(fetchHealth, 10000);+ if (retryIntervalRef.current) {+ clearInterval(retryIntervalRef.current);+ retryIntervalRef.current = null;}+ retryIntervalRef.current = setInterval(fetchHealth, 10000);}}, [setServerDown]); // stable deps — no cycle- const debouncedFetchHealth = useCallback(() => {- return new Promise<void>((resolve) => {- debounce(() => {- if (wsRef.current && wsRef.current.readyState === WebSocket.OPEN) {- fetchHealth().then(resolve);- } else {- resolve();- }- }, 1000)();- });- }, [fetchHealth]);+ // Stable debounced wrapper — one instance for the lifetime of the hook.+ // fetchHealth is itself stable (useCallback with stable deps), so this ref+ // never needs to be recreated. Callers get proper 1-second coalescing.+ const debouncedFetchHealthRef = useRef(debounce(fetchHealth, 1000));+ const debouncedFetchHealth = useCallback((): Promise<void> => {+ debouncedFetchHealthRef.current();+ return Promise.resolve();+ }, []);useEffect(() => {fetchHealth();