Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(dashboard): stop the restart poll once the page unmounts
Yudhi Armyndharisb1aed0d5b81f
diff --git a/dashboard/src/hooks/useRestartFlow.test.ts b/dashboard/src/hooks/useRestartFlow.test.tsindex 66b7eaf20..042377517 100644--- a/dashboard/src/hooks/useRestartFlow.test.ts+++ b/dashboard/src/hooks/useRestartFlow.test.ts@@ -26,6 +26,14 @@ function htmlResponse(status: number): Response {return new Response(`<html><body>${status}</body></html>`, { status, headers: { 'Content-Type': 'text/html' } });}+function deferred(): { promise: Promise<Response>; resolve: (r: Response) => void } {+ let resolve!: (r: Response) => void;+ const promise = new Promise<Response>(r => (resolve = r));+ return { promise, resolve };+}++const wait = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));+before(async () => {const { installJsdomGlobals } = (await import('../test-helpers/jsdom.ts')) as {installJsdomGlobals: typeof installJsdomGlobalsFn;@@ -73,3 +81,57 @@ test('a proxy 503 page and a coded gateway 5xx stay refusals', async () => {const coded = jsonResponse({ message: 'Compose rejected the profile', code: 'SOME_CODE' }, 524);assert.equal(await statusAfterRestart(() => Promise.resolve(coded)), 'error');});++test(+ 'a restart answer that lands after unmount starts no countdown and no readiness poll',+ { timeout: 10_000 },+ async () => {+ const restart = deferred();+ restartAnswer = () => restart.promise;+ const live = new Set<unknown>();+ const { setInterval: realSet, clearInterval: realClear } = globalThis;+ globalThis.setInterval = ((fn: () => void, ms?: number) => {+ const handle = realSet(fn, ms);+ live.add(handle);+ return handle;+ }) as typeof setInterval;+ globalThis.clearInterval = ((handle: ReturnType<typeof setInterval>) => {+ live.delete(handle);+ realClear(handle);+ }) as typeof clearInterval;+ try {+ const { result, unmount } = rtl.renderHook(() => useRestartFlow());+ rtl.act(() => void result.current.start());+ await rtl.waitFor(() => assert.ok(calls.includes('POST /api/infra/restart')));++ unmount();+ restart.resolve(jsonResponse({ message: 'restarting', restarting: true, estimatedTime: 5 }));+ // Past the first readiness poll (3s after the answer).+ await wait(3500);++ assert.ok(!calls.includes('GET /api/health/ready'), 'the readiness poll ran after unmount');+ assert.equal(live.size, 0, 'a countdown interval outlived the component');+ } finally {+ for (const handle of live) realClear(handle as ReturnType<typeof setInterval>);+ globalThis.setInterval = realSet;+ globalThis.clearInterval = realClear;+ }+ },+);++test('a readiness check in flight at unmount does not schedule another one', { timeout: 12_000 }, async () => {+ restartAnswer = () => Promise.resolve(jsonResponse({ message: 'restarting', restarting: true, estimatedTime: 5 }));+ const ready = deferred();+ readyAnswer = () => ready.promise;+ const { result, unmount } = rtl.renderHook(() => useRestartFlow());+ rtl.act(() => void result.current.start());+ await rtl.waitFor(() => assert.ok(calls.includes('GET /api/health/ready')), { timeout: 5_000 });++ unmount();+ ready.resolve(jsonResponse({ status: 'error', details: {} }, 503));+ readyAnswer = () => Promise.resolve(jsonResponse({ status: 'error', details: {} }, 503));+ // Past the 1s retry a failed check schedules.+ await wait(1500);++ assert.equal(calls.filter(c => c === 'GET /api/health/ready').length, 1, 'the poll re-armed after unmount');+});diff --git a/dashboard/src/hooks/useRestartFlow.ts b/dashboard/src/hooks/useRestartFlow.tsindex f2c496937..7a6d30979 100644--- a/dashboard/src/hooks/useRestartFlow.ts+++ b/dashboard/src/hooks/useRestartFlow.ts@@ -60,12 +60,19 @@ export function useRestartFlow(): RestartFlow {// away mid-restart) cancels them instead of letting them fire setState on a dead component. The// trailing window.location.reload() on success is kept — a restart that already completed is meant// to reload the page.+ // mountedRef stops what an unmount cannot clear: a restart answer or a readiness check still in+ // flight would otherwise arm a new interval or poll after the cleanup ran, and that poll could end+ // by reloading whatever page the operator moved on to. Set in the effect body, not the initializer,+ // so StrictMode's dev unmount/remount leaves it true.const pollTimeoutsRef = useRef<Set<ReturnType<typeof setTimeout>>>(new Set());const countdownIntervalRef = useRef<ReturnType<typeof setInterval> | null>(null);+ const mountedRef = useRef(false);useEffect(() => {+ mountedRef.current = true;const pollTimeouts = pollTimeoutsRef.current;return () => {+ mountedRef.current = false;for (const handle of pollTimeouts) clearTimeout(handle);pollTimeouts.clear();if (countdownIntervalRef.current) {@@ -76,6 +83,7 @@ export function useRestartFlow(): RestartFlow {}, []);const schedulePollTimeout = (fn: () => void, ms: number) => {+ if (!mountedRef.current) return;const handle = setTimeout(() => {pollTimeoutsRef.current.delete(handle);fn();@@ -170,6 +178,7 @@ export function useRestartFlow(): RestartFlow {return;}}+ if (!mountedRef.current) return;setRestartStatus('waiting');stopCountdown();