Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): treat a Cloudflare 52x restart answer as unknown

Yudhi Armyndharisa24f82adda67
diff --git a/dashboard/src/hooks/useRestartFlow.test.ts b/dashboard/src/hooks/useRestartFlow.test.ts
new file mode 100644
index 000000000..66b7eaf20
--- /dev/null
+++ b/dashboard/src/hooks/useRestartFlow.test.ts
@@ -0,0 +1,75 @@
+// The restart flow driven directly through renderHook, for outcomes that turn on the exact answer to
+// 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 assert from 'node:assert/strict';
+import type { RestartStatus, useRestartFlow as useRestartFlowFn } from './useRestartFlow.ts';
+import type { installJsdomGlobals as installJsdomGlobalsFn } from '../test-helpers/jsdom.ts';
+
+type RTL = typeof import('@testing-library/react');
+
+let rtl: RTL;
+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(() => {});
+const calls: string[] = [];
+
+function jsonResponse(data: unknown, status = 200): Response {
+ return new Response(JSON.stringify(data), { status, headers: { 'Content-Type': 'application/json' } });
+}
+
+function htmlResponse(status: number): Response {
+ return new Response(`<html><body>${status}</body></html>`, { status, headers: { 'Content-Type': 'text/html' } });
+}
+
+before(async () => {
+ const { installJsdomGlobals } = (await import('../test-helpers/jsdom.ts')) as {
+ installJsdomGlobals: typeof installJsdomGlobalsFn;
+ };
+ await installJsdomGlobals();
+ globalThis.fetch = (input: RequestInfo | URL, init?: RequestInit): Promise<Response> => {
+ 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();
+ return Promise.resolve(jsonResponse({ message: `unstubbed ${path}` }, 404));
+ };
+ rtl = await import('@testing-library/react');
+ ({ useRestartFlow } = await import('./useRestartFlow.ts'));
+});
+
+afterEach(() => {
+ rtl.cleanup();
+ calls.length = 0;
+ restartAnswer = () => new Promise(() => {});
+ readyAnswer = () => new Promise(() => {});
+});
+
+async function statusAfterRestart(answer: () => Promise<Response>): Promise<RestartStatus> {
+ restartAnswer = answer;
+ const { result } = rtl.renderHook(() => useRestartFlow());
+ rtl.act(() => void result.current.start());
+ await rtl.waitFor(() => assert.notEqual(result.current.restartStatus, 'restarting'));
+ return result.current.restartStatus;
+}
+
+// Cloudflare answers a slow origin with 524 and a reset or refused one with 520/522, all as an HTML
+// page. The restart may still go ahead behind it, so this is the same unknown outcome as a 504.
+test('a proxy 52x page on the restart request is an unknown outcome, not a refusal', async () => {
+ for (const status of [520, 522, 524]) {
+ assert.equal(await statusAfterRestart(() => Promise.resolve(htmlResponse(status))), 'unknown', `HTTP ${status}`);
+ rtl.cleanup();
+ }
+});
+
+test('a proxy 503 page and a coded gateway 5xx stay refusals', async () => {
+ assert.equal(await statusAfterRestart(() => Promise.resolve(htmlResponse(503))), 'error');
+ rtl.cleanup();
+ const coded = jsonResponse({ message: 'Compose rejected the profile', code: 'SOME_CODE' }, 524);
+ assert.equal(await statusAfterRestart(() => Promise.resolve(coded)), 'error');
+});
diff --git a/dashboard/src/hooks/useRestartFlow.ts b/dashboard/src/hooks/useRestartFlow.ts
index c445356cb..f2c496937 100644
--- a/dashboard/src/hooks/useRestartFlow.ts
+++ b/dashboard/src/hooks/useRestartFlow.ts
@@ -2,7 +2,8 @@ import { useEffect, useRef, useState } from 'react';
import { infraApi } from '../services/api';
import { restartPollAttempts } from '../utils/restartPoll';
-// 'unknown': a proxy gave up waiting for the restart request, so whether it went through cannot be told.
+// 'unknown': a proxy answered in the gateway's place (a 504, a Cloudflare 52x, or a 502 with no gateway
+// code), so whether the restart went through cannot be told.
export type RestartStatus = 'idle' | 'restarting' | 'waiting' | 'success' | 'error' | 'unknown';
export interface RestartOpenRequest {
@@ -154,11 +155,13 @@ export function useRestartFlow(): RestartFlow {
if (typeof failure?.status === 'number') {
stopCountdown();
setRestartCountdown(0);
- // A 504, or a 502 the gateway did not stamp with a code, is a proxy answering in the gateway's place:
- // a timeout, or an upstream connection that failed or dropped. The client cannot tell which, and the
- // request may still be running (a first-time enable pulls an image before the restart), so this is
- // neither a refusal nor something a readiness poll can settle: the old process answers.
- if (failure.status === 504 || (failure.status === 502 && failure.code === undefined)) {
+ // A 502, a 504 or a Cloudflare 520-527 the gateway did not stamp with a code is a proxy answering in
+ // the gateway's place: a timeout, or an upstream connection that failed or dropped. The client cannot
+ // tell which, and the request may still be running (a first-time enable pulls an image before the
+ // restart), so this is neither a refusal nor something a readiness poll can settle: the old process
+ // answers.
+ const { status } = failure;
+ if (failure.code === undefined && (status === 502 || status === 504 || (status >= 520 && status <= 527))) {
setRestartStatus('unknown');
return;
}