Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): bound the restart readiness wait

Yudhi Armyndharis29f83b35fb80
diff --git a/dashboard/src/hooks/useRestartFlow.test.ts b/dashboard/src/hooks/useRestartFlow.test.ts
index 580e619fd..1a11e7304 100644
--- a/dashboard/src/hooks/useRestartFlow.test.ts
+++ b/dashboard/src/hooks/useRestartFlow.test.ts
@@ -2,7 +2,7 @@
// the restart POST or on timing around an unmount. Infrastructure.test.ts covers the same hook through
// the page.
import '../test-helpers/register-hooks.ts';
-import { test, before, afterEach } from 'node:test';
+import { test, before, afterEach, mock } from 'node:test';
import assert from 'node:assert/strict';
import type { RestartStatus, useRestartFlow as useRestartFlowFn } from './useRestartFlow.ts';
import type { installJsdomGlobals as installJsdomGlobalsFn } from '../test-helpers/jsdom.ts';
@@ -14,9 +14,10 @@ let useRestartFlow: typeof useRestartFlowFn;
// Per-test answers to the two requests the hook sends; a test that never sets one gets a hanging
// request, which is what an in-flight call looks like.
-let restartAnswer: () => Promise<Response> = () => new Promise(() => {});
-let readyAnswer: () => Promise<Response> = () => new Promise(() => {});
+let restartAnswer: (signal?: AbortSignal | null) => Promise<Response> = () => new Promise(() => {});
+let readyAnswer: (signal?: AbortSignal | null) => Promise<Response> = () => new Promise(() => {});
const calls: string[] = [];
+const readySignals: Array<AbortSignal | null | undefined> = [];
function jsonResponse(data: unknown, status = 200): Response {
return new Response(JSON.stringify(data), { status, headers: { 'Content-Type': 'application/json' } });
@@ -43,8 +44,11 @@ before(async () => {
const url = typeof input === 'string' ? input : input instanceof URL ? input.href : input.url;
const path = url.replace(/^https?:\/\/[^/]+/, '');
calls.push(`${init?.method ?? 'GET'} ${path}`);
- if (path === '/api/infra/restart') return restartAnswer();
- if (path === '/api/health/ready') return readyAnswer();
+ if (path === '/api/infra/restart') return restartAnswer(init?.signal);
+ if (path === '/api/health/ready') {
+ readySignals.push(init?.signal);
+ return readyAnswer(init?.signal);
+ }
return Promise.resolve(jsonResponse({ message: `unstubbed ${path}` }, 404));
};
rtl = await import('@testing-library/react');
@@ -53,7 +57,9 @@ before(async () => {
afterEach(() => {
rtl.cleanup();
+ mock.timers.reset();
calls.length = 0;
+ readySignals.length = 0;
restartAnswer = () => new Promise(() => {});
readyAnswer = () => new Promise(() => {});
});
@@ -152,3 +158,151 @@ test('the containers the restart could not start or stop are listed with the ans
await rtl.waitFor(() => assert.equal(result.current.restartStatus, 'waiting'));
assert.deepEqual(result.current.restartWarnings, ['postgres: port in use', 'minio: stop timed out']);
});
+
+function abortReady<T>(work: Promise<T>, signal?: AbortSignal | null): Promise<T> {
+ if (!signal) return work;
+ if (signal.aborted) return Promise.reject(new DOMException('Aborted', 'AbortError'));
+ return new Promise<T>((resolve, reject) => {
+ const aborted = () => reject(new DOMException('Aborted', 'AbortError'));
+ signal.addEventListener('abort', aborted, { once: true });
+ void work.then(resolve, reject).finally(() => signal.removeEventListener('abort', aborted));
+ });
+}
+
+const settleRestartReply = async () => {
+ for (let i = 0; i < 10; i++) await Promise.resolve();
+ await new Promise<void>(resolve => setImmediate(resolve));
+};
+
+for (const stalledAt of ['fetch', 'body']) {
+ test(`readiness ${stalledAt} is aborted at the elapsed restart deadline`, async () => {
+ mock.timers.enable({ apis: ['setTimeout', 'setInterval', 'Date'], now: 1800000000000 });
+ restartAnswer = () => Promise.resolve(jsonResponse({ message: 'Restarting', restarting: true, estimatedTime: 5 }));
+ const pending = deferred();
+ let bodyResolve!: (body: unknown) => void;
+ const body = new Promise<unknown>(resolve => {
+ bodyResolve = resolve;
+ });
+ readyAnswer = signal =>
+ stalledAt === 'fetch'
+ ? abortReady(pending.promise, signal)
+ : Promise.resolve({ ok: true, status: 200, json: () => abortReady(body, signal) } as Response);
+ const { result } = rtl.renderHook(() => useRestartFlow());
+ await rtl.act(async () => {
+ void result.current.start();
+ await settleRestartReply();
+ });
+ assert.equal(result.current.restartStatus, 'waiting');
+ await rtl.act(async () => {
+ mock.timers.tick(3000);
+ await settleRestartReply();
+ });
+ assert.equal(readySignals.length, 1);
+ await rtl.act(async () => {
+ mock.timers.tick(60000);
+ await settleRestartReply();
+ });
+ assert.equal(result.current.restartStatus, 'error');
+ assert.equal(result.current.restartCountdown, 0);
+ assert.equal(readySignals[0]?.aborted, true);
+ await rtl.act(async () => {
+ pending.resolve(jsonResponse({ status: 'ok', details: {} }));
+ bodyResolve({ status: 'ok', details: {} });
+ await settleRestartReply();
+ mock.timers.tick(120000);
+ });
+ assert.equal(result.current.restartStatus, 'error');
+ assert.equal(readySignals.length, 1);
+ });
+}
+
+test('a late readiness reply cannot replace the elapsed deadline result', async () => {
+ mock.timers.enable({ apis: ['setTimeout', 'setInterval', 'Date'], now: 1800000000000 });
+ restartAnswer = () => Promise.resolve(jsonResponse({ message: 'Restarting', restarting: true, estimatedTime: 5 }));
+ const pending = deferred();
+ // Keep the transport pending even after abort, so the state fence is exercised independently.
+ readyAnswer = () => pending.promise;
+ const { result } = rtl.renderHook(() => useRestartFlow());
+ await rtl.act(async () => {
+ void result.current.start();
+ await settleRestartReply();
+ });
+ await rtl.act(async () => {
+ mock.timers.tick(3000);
+ await settleRestartReply();
+ });
+ await rtl.act(async () => {
+ mock.timers.tick(60000);
+ await settleRestartReply();
+ });
+ assert.equal(result.current.restartStatus, 'error');
+ assert.equal(readySignals[0]?.aborted, true);
+ await rtl.act(async () => {
+ pending.resolve(jsonResponse({ status: 'ok', details: {} }));
+ await settleRestartReply();
+ mock.timers.tick(120000);
+ });
+ assert.equal(result.current.restartStatus, 'error');
+ assert.equal(readySignals.length, 1);
+});
+
+test('unmount aborts an in-flight readiness request without rescheduling', async () => {
+ mock.timers.enable({ apis: ['setTimeout', 'setInterval', 'Date'], now: 1800000000000 });
+ restartAnswer = () => Promise.resolve(jsonResponse({ message: 'Restarting', restarting: true, estimatedTime: 5 }));
+ const pending = deferred();
+ readyAnswer = signal => abortReady(pending.promise, signal);
+ const { result, unmount } = rtl.renderHook(() => useRestartFlow());
+ await rtl.act(async () => {
+ void result.current.start();
+ await settleRestartReply();
+ });
+ await rtl.act(async () => {
+ mock.timers.tick(3000);
+ await settleRestartReply();
+ });
+ assert.equal(readySignals.length, 1);
+ assert.ok(readySignals[0]);
+ assert.equal(readySignals[0].aborted, false);
+ await rtl.act(async () => {
+ unmount();
+ await settleRestartReply();
+ });
+ assert.equal(readySignals[0].aborted, true);
+ await rtl.act(async () => {
+ pending.resolve(jsonResponse({ status: 'ok', details: {} }));
+ await settleRestartReply();
+ mock.timers.tick(120000);
+ });
+ assert.equal(readySignals.length, 1);
+});
+
+test('a successful readiness response retires its deadline', async () => {
+ mock.timers.enable({ apis: ['setTimeout', 'setInterval', 'Date'], now: 1800000000000 });
+ // A warning keeps the success view open instead of scheduling a page reload.
+ restartAnswer = () =>
+ Promise.resolve(
+ jsonResponse({
+ message: 'Restarting',
+ restarting: true,
+ estimatedTime: 5,
+ orchestration: { errors: ['A service needs attention'] },
+ }),
+ );
+ readyAnswer = () => Promise.resolve(jsonResponse({ status: 'ok', details: {} }));
+ const { result } = rtl.renderHook(() => useRestartFlow());
+ await rtl.act(async () => {
+ void result.current.start();
+ await settleRestartReply();
+ });
+ await rtl.act(async () => {
+ mock.timers.tick(3000);
+ await settleRestartReply();
+ });
+ assert.equal(result.current.restartStatus, 'success');
+ await rtl.act(async () => {
+ mock.timers.tick(120000);
+ await settleRestartReply();
+ });
+ assert.equal(result.current.restartStatus, 'success');
+ assert.equal(readySignals.length, 1);
+});
diff --git a/dashboard/src/hooks/useRestartFlow.ts b/dashboard/src/hooks/useRestartFlow.ts
index d40a4cca2..84a4b8f7c 100644
--- a/dashboard/src/hooks/useRestartFlow.ts
+++ b/dashboard/src/hooks/useRestartFlow.ts
@@ -72,12 +72,14 @@ export function useRestartFlow(): RestartFlow {
const pollTimeoutsRef = useRef<Set<ReturnType<typeof setTimeout>>>(new Set());
const countdownIntervalRef = useRef<ReturnType<typeof setInterval> | null>(null);
const mountedRef = useRef(false);
+ const readinessControllerRef = useRef<AbortController | null>(null);
useEffect(() => {
mountedRef.current = true;
const pollTimeouts = pollTimeoutsRef.current;
return () => {
mountedRef.current = false;
+ readinessControllerRef.current?.abort();
for (const handle of pollTimeouts) clearTimeout(handle);
pollTimeouts.clear();
if (countdownIntervalRef.current) {
@@ -122,22 +124,41 @@ export function useRestartFlow(): RestartFlow {
if (restartStatus === 'idle') setShowRestartModal(false);
};
- const checkServerHealth = (estimatedTime: number | undefined, hasWarnings: boolean) => {
+ const checkServerHealth = (estimatedTime: number | undefined, hasWarnings: boolean, controller: AbortController) => {
let attempts = 0;
const maxAttempts = restartPollAttempts(estimatedTime);
+ const isCurrent = () =>
+ mountedRef.current && readinessControllerRef.current === controller && !controller.signal.aborted;
+ const settle = (status: 'success' | 'error') => {
+ if (!isCurrent()) return false;
+ controller.abort();
+ clearTimeout(deadline);
+ pollTimeoutsRef.current.delete(deadline);
+ stopCountdown();
+ setRestartCountdown(0);
+ setRestartStatus(status);
+ return true;
+ };
+ // Elapsed time also bounds a request or response body that never finishes.
+ const deadline = setTimeout(() => settle('error'), maxAttempts * 1000);
+ pollTimeoutsRef.current.add(deadline);
const check = async () => {
+ if (!isCurrent()) return;
try {
- await infraApi.healthCheck();
- stopCountdown();
- setRestartCountdown(0);
- setRestartStatus('success');
+ await infraApi.healthCheck(controller.signal);
+ if (!settle('success')) return;
// With warnings on screen the operator reloads by hand, after reading them.
- if (!hasWarnings) schedulePollTimeout(() => window.location.reload(), 2000);
+ if (!hasWarnings) {
+ schedulePollTimeout(() => {
+ if (readinessControllerRef.current === controller) window.location.reload();
+ }, 2000);
+ }
} catch {
+ if (!isCurrent()) return;
attempts++;
if (attempts < maxAttempts) schedulePollTimeout(check, 1000);
- else setRestartStatus('error');
+ else settle('error');
}
};
@@ -147,6 +168,14 @@ export function useRestartFlow(): RestartFlow {
const profilesToRemove = profiles.running.filter(p => !profiles.pending.includes(p));
const start = async () => {
+ readinessControllerRef.current?.abort();
+ const controller = new AbortController();
+ readinessControllerRef.current = controller;
+ const isCurrent = () =>
+ mountedRef.current && readinessControllerRef.current === controller && !controller.signal.aborted;
+ for (const handle of pollTimeoutsRef.current) clearTimeout(handle);
+ pollTimeoutsRef.current.clear();
+ stopCountdown();
setRestartStatus('restarting');
setRestartCountdown(30);
setRestartTotal(30);
@@ -157,6 +186,7 @@ export function useRestartFlow(): RestartFlow {
let warnings: string[] = [];
try {
const response = await infraApi.restart(profiles.pending, profilesToRemove);
+ if (!isCurrent()) return;
estimatedTime = response.estimatedTime;
if (response.estimatedTime) {
setRestartCountdown(response.estimatedTime);
@@ -165,6 +195,7 @@ export function useRestartFlow(): RestartFlow {
warnings = [...(response.orchestration?.errors ?? []), ...(response.removal?.errors ?? [])];
setRestartWarnings(warnings);
} catch (err) {
+ if (!isCurrent()) return;
// An HTTP status (a proxy 503 included) means no shutdown was confirmed and the old
// process may still be serving, so a readiness poll would report a restart that never happened.
// Only a status-less network failure is the expected sign of the server going down mid-answer.
@@ -187,7 +218,7 @@ export function useRestartFlow(): RestartFlow {
return;
}
}
- if (!mountedRef.current) return;
+ if (!isCurrent()) return;
setRestartStatus('waiting');
stopCountdown();
@@ -201,7 +232,7 @@ export function useRestartFlow(): RestartFlow {
});
}, 1000);
- checkServerHealth(estimatedTime, warnings.length > 0);
+ checkServerHealth(estimatedTime, warnings.length > 0, controller);
};
return {
diff --git a/dashboard/src/services/api.ts b/dashboard/src/services/api.ts
index d05f4c8fb..343940cbb 100644
--- a/dashboard/src/services/api.ts
+++ b/dashboard/src/services/api.ts
@@ -1406,7 +1406,8 @@ export const infraApi = {
// process it just asked to shut down: /infra/health answers 200 for the whole drain,
// while /health/ready reports 503 as soon as draining starts and stays 503 until both databases
// answer. Public (no API key), like /infra/health.
- healthCheck: () => request<{ status: 'ok' | 'error'; details: Record<string, { status: string }> }>('/health/ready'),
+ healthCheck: (signal?: AbortSignal) =>
+ request<{ status: 'ok' | 'error'; details: Record<string, { status: string }> }>('/health/ready', { signal }),
// Data migration: export all Data-DB tables (call while still on the OLD database, before switching),
// then import after the switch + restart. Used by the DB-switch migration guard so data isn't lost.
exportData: () =>
diff --git a/dashboard/src/utils/restartPoll.test.ts b/dashboard/src/utils/restartPoll.test.ts
index 0b94dfda8..95661c6d2 100644
--- a/dashboard/src/utils/restartPoll.test.ts
+++ b/dashboard/src/utils/restartPoll.test.ts
@@ -41,7 +41,7 @@ const read = (path: string): string => readFileSync(new URL(path, import.meta.ur
test('the restart poll targets readiness, not a ping that survives the drain', () => {
const api = read('../services/api.ts');
- const healthCheck = api.split('\n').find(line => line.trimStart().startsWith('healthCheck: () =>'));
+ const healthCheck = api.split(' healthCheck:')[1]?.split('\n //')[0];
assert.ok(healthCheck, 'infraApi.healthCheck not found — update this guard, do not delete it');
assert.match(
healthCheck,