From 5c37ddfca81405d9e5981eb0dfcea3e153cecb51 Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Sun, 31 May 2026 03:02:14 -0700 Subject: [PATCH] perf: clean mobile diagnostic fetch timeout (#4063) --- mobile/app/troubleshoot.tsx | 37 +++++++++++++++---- .../diagnostic-fetch-timeout.test.ts | 33 +++++++++++++++++ .../diagnostics/diagnostic-fetch-timeout.ts | 36 ++++++++++++++++++ 3 files changed, 98 insertions(+), 8 deletions(-) create mode 100644 mobile/src/diagnostics/diagnostic-fetch-timeout.test.ts create mode 100644 mobile/src/diagnostics/diagnostic-fetch-timeout.ts diff --git a/mobile/app/troubleshoot.tsx b/mobile/app/troubleshoot.tsx index be800b74c..56c2dcc5c 100644 --- a/mobile/app/troubleshoot.tsx +++ b/mobile/app/troubleshoot.tsx @@ -26,6 +26,10 @@ import { } from 'lucide-react-native' import { colors, spacing, typography } from '../src/theme/mobile-theme' import { loadHosts } from '../src/transport/host-store' +import { + startDiagnosticFetchTimeout, + type DiagnosticFetchTimeout +} from '../src/diagnostics/diagnostic-fetch-timeout' type DiagnosticStatus = 'idle' | 'running' | 'done' @@ -113,10 +117,15 @@ export default function TroubleshootScreen() { const [diagnosticStatus, setDiagnosticStatus] = useState('idle') const [checks, setChecks] = useState([]) const abortRef = useRef(false) + const diagnosticRunRef = useRef(0) + const activeInternetCheckRef = useRef(null) useEffect(() => { return () => { abortRef.current = true + diagnosticRunRef.current += 1 + activeInternetCheckRef.current?.dispose() + activeInternetCheckRef.current = null } }, []) @@ -125,11 +134,16 @@ export default function TroubleshootScreen() { }, []) const runDiagnostics = useCallback(async () => { + const runId = diagnosticRunRef.current + 1 + diagnosticRunRef.current = runId abortRef.current = false + activeInternetCheckRef.current?.dispose() + activeInternetCheckRef.current = null setDiagnosticStatus('running') setChecks([]) const results: CheckResult[] = [] + const isCurrentRun = () => !abortRef.current && diagnosticRunRef.current === runId try { const hosts = await loadHosts() @@ -142,33 +156,40 @@ export default function TroubleshootScreen() { results.push({ label: 'Paired hosts', status: 'warn', detail: 'Could not read host data' }) } - if (abortRef.current) return + if (!isCurrentRun()) return setChecks([...results]) + const internetCheck = startDiagnosticFetchTimeout(5000) + activeInternetCheckRef.current = internetCheck try { - const controller = new AbortController() - const timeout = setTimeout(() => controller.abort(), 5000) const resp = await fetch('https://dns.google/resolve?name=example.com&type=A', { - signal: controller.signal + signal: internetCheck.signal }) - clearTimeout(timeout) + if (!isCurrentRun()) return results.push( resp.ok ? { label: 'Internet', status: 'pass', detail: 'Connected' } : { label: 'Internet', status: 'warn', detail: 'Unexpected response' } ) } catch { + if (!isCurrentRun()) return results.push({ label: 'Internet', status: 'fail', detail: 'No connection' }) + } finally { + internetCheck.dispose() + if (activeInternetCheckRef.current === internetCheck) { + activeInternetCheckRef.current = null + } } - if (abortRef.current) return + if (!isCurrentRun()) return setChecks([...results]) try { const hosts = await loadHosts() for (const host of hosts) { - if (abortRef.current) return + if (!isCurrentRun()) return const reachable = await testHostReachability(host.endpoint) + if (!isCurrentRun()) return results.push({ label: host.name, status: reachable ? 'pass' : 'fail', @@ -182,7 +203,7 @@ export default function TroubleshootScreen() { results.push({ label: 'Hosts', status: 'warn', detail: 'Could not test' }) } - if (abortRef.current) return + if (!isCurrentRun()) return results.push({ label: 'Platform', diff --git a/mobile/src/diagnostics/diagnostic-fetch-timeout.test.ts b/mobile/src/diagnostics/diagnostic-fetch-timeout.test.ts new file mode 100644 index 000000000..6843f48ae --- /dev/null +++ b/mobile/src/diagnostics/diagnostic-fetch-timeout.test.ts @@ -0,0 +1,33 @@ +import { afterEach, describe, expect, it, vi } from 'vitest' +import { startDiagnosticFetchTimeout } from './diagnostic-fetch-timeout' + +describe('diagnostic fetch timeout', () => { + afterEach(() => { + vi.useRealTimers() + }) + + it('aborts when the timeout fires', () => { + vi.useFakeTimers() + const attempt = startDiagnosticFetchTimeout(5000) + + expect(attempt.signal.aborted).toBe(false) + expect(attempt.timedOut).toBe(false) + + vi.advanceTimersByTime(5000) + + expect(attempt.signal.aborted).toBe(true) + expect(attempt.timedOut).toBe(true) + }) + + it('clears the timeout when disposed early', () => { + vi.useFakeTimers() + const attempt = startDiagnosticFetchTimeout(5000) + + attempt.dispose() + expect(attempt.signal.aborted).toBe(true) + expect(attempt.timedOut).toBe(false) + + vi.advanceTimersByTime(5000) + expect(attempt.timedOut).toBe(false) + }) +}) diff --git a/mobile/src/diagnostics/diagnostic-fetch-timeout.ts b/mobile/src/diagnostics/diagnostic-fetch-timeout.ts new file mode 100644 index 000000000..8cc4fe2ca --- /dev/null +++ b/mobile/src/diagnostics/diagnostic-fetch-timeout.ts @@ -0,0 +1,36 @@ +export type DiagnosticFetchTimeout = { + readonly signal: AbortSignal + readonly timedOut: boolean + dispose: () => void +} + +export function startDiagnosticFetchTimeout(timeoutMs: number): DiagnosticFetchTimeout { + const controller = new AbortController() + let disposed = false + let timedOut = false + let timer: ReturnType | null = setTimeout(() => { + timer = null + timedOut = true + controller.abort() + }, timeoutMs) + + function dispose() { + if (disposed) return + disposed = true + if (timer) { + clearTimeout(timer) + timer = null + } + if (!controller.signal.aborted) { + controller.abort() + } + } + + return { + signal: controller.signal, + get timedOut() { + return timedOut + }, + dispose + } +}