From 40132196980664bd7adbd78dc1cac5e00df23c29 Mon Sep 17 00:00:00 2001 From: Aleksey Fedorov <320380254+Dv2Corn@users.noreply.github.com> Date: Wed, 25 Nov 2026 22:08:34 +1000 Subject: [PATCH] fix(settings): wait for the panel to answer before reloading after a restart (#7205) After Restart Panel, and after a database import, the page slept a fixed 5 seconds and reloaded. When the panel took longer to come back, the reload reached a reverse proxy with nothing listening upstream and the browser showed its 502/504 page. Keep the 5 second delay, then poll the address the page is about to open every 2 seconds until it answers below 500, and give up after 90 seconds so the page never hangs. A new port or protocol is probed with no-cors; an https page whose target is http would be blocked as mixed content, so it probes its own origin, which is the proxy that answers 502 while the panel is down. Closes #7190 --- frontend/src/pages/index/BackupModal.tsx | 2 +- frontend/src/pages/settings/SettingsPage.tsx | 5 +- frontend/src/test/wait-for-panel.test.ts | 78 ++++++++++++++++++++ frontend/src/utils/index.ts | 29 ++++++++ 4 files changed, 111 insertions(+), 3 deletions(-) create mode 100644 frontend/src/test/wait-for-panel.test.ts diff --git a/frontend/src/pages/index/BackupModal.tsx b/frontend/src/pages/index/BackupModal.tsx index 473d30592..fbc09ed04 100644 --- a/frontend/src/pages/index/BackupModal.tsx +++ b/frontend/src/pages/index/BackupModal.tsx @@ -61,7 +61,7 @@ export default function BackupModal({ // importDB schedules the panel restart server-side; wait it out, then reload. onBusy({ busy: true, tip: `${t('pages.settings.restartPanel')}…` }); - await PromiseUtil.sleep(5000); + await PromiseUtil.waitForPanel(window.location.href); window.location.reload(); }); fileInput.click(); diff --git a/frontend/src/pages/settings/SettingsPage.tsx b/frontend/src/pages/settings/SettingsPage.tsx index 7f42169c0..c666bafd1 100644 --- a/frontend/src/pages/settings/SettingsPage.tsx +++ b/frontend/src/pages/settings/SettingsPage.tsx @@ -150,8 +150,9 @@ export default function SettingsPage() { try { const msg = (await HttpUtil.post('/panel/api/setting/restartPanel')) as ApiMsg; if (!msg?.success) return; - await PromiseUtil.sleep(5000); - window.location.replace(rebuildUrlAfterRestart()); + const target = rebuildUrlAfterRestart(); + await PromiseUtil.waitForPanel(target); + window.location.replace(target); } finally { setSpinning(false); } diff --git a/frontend/src/test/wait-for-panel.test.ts b/frontend/src/test/wait-for-panel.test.ts new file mode 100644 index 000000000..c16101226 --- /dev/null +++ b/frontend/src/test/wait-for-panel.test.ts @@ -0,0 +1,78 @@ +import { afterEach, describe, expect, it, vi } from 'vitest'; + +import { PromiseUtil } from '@/utils'; + +const fast = { delay: 0, interval: 1, timeout: 200 }; + +function answer(status: number, type: ResponseType = 'basic'): Response { + return { status, type } as Response; +} + +afterEach(() => { + vi.unstubAllGlobals(); +}); + +describe('PromiseUtil.waitForPanel (#7190)', () => { + it('keeps polling through refused connections and proxy 502s until the panel answers', async () => { + vi.stubGlobal('window', { + location: { href: 'https://panel.test/panel/settings', origin: 'https://panel.test' }, + }); + const fetchMock = vi + .fn() + .mockRejectedValueOnce(new TypeError('Failed to fetch')) + .mockResolvedValueOnce(answer(502)) + .mockResolvedValueOnce(answer(200)); + vi.stubGlobal('fetch', fetchMock); + + await expect(PromiseUtil.waitForPanel('https://panel.test/panel/settings', fast)).resolves.toBe( + true, + ); + expect(fetchMock).toHaveBeenCalledTimes(3); + expect(fetchMock.mock.calls[0][1]).toMatchObject({ mode: 'same-origin', cache: 'no-store' }); + }); + + it('gives up after the timeout so the caller still navigates', async () => { + vi.stubGlobal('window', { + location: { href: 'https://panel.test/', origin: 'https://panel.test' }, + }); + vi.stubGlobal('fetch', vi.fn().mockResolvedValue(answer(502))); + + await expect( + PromiseUtil.waitForPanel('https://panel.test/', { ...fast, timeout: 20 }), + ).resolves.toBe(false); + }); + + it('probes a new port or protocol with no-cors and accepts the opaque answer', async () => { + vi.stubGlobal('window', { + location: { + href: 'http://203.0.113.5:2053/panel/settings', + origin: 'http://203.0.113.5:2053', + }, + }); + const fetchMock = vi.fn().mockResolvedValue(answer(0, 'opaque')); + vi.stubGlobal('fetch', fetchMock); + + await expect( + PromiseUtil.waitForPanel('https://203.0.113.5:8443/panel/settings', fast), + ).resolves.toBe(true); + expect(fetchMock.mock.calls[0][1]).toMatchObject({ mode: 'no-cors' }); + }); + + it('probes the current origin when an https page would open an http target (mixed content)', async () => { + vi.stubGlobal('window', { + location: { + href: 'https://panel.test/panel/settings', + origin: 'https://panel.test', + protocol: 'https:', + }, + }); + const fetchMock = vi.fn().mockResolvedValue(answer(200)); + vi.stubGlobal('fetch', fetchMock); + + await expect(PromiseUtil.waitForPanel('http://panel.test/panel/settings', fast)).resolves.toBe( + true, + ); + expect(String(fetchMock.mock.calls[0][0])).toBe('https://panel.test/panel/settings'); + expect(fetchMock.mock.calls[0][1]).toMatchObject({ mode: 'same-origin' }); + }); +}); diff --git a/frontend/src/utils/index.ts b/frontend/src/utils/index.ts index e3f781620..84320e704 100644 --- a/frontend/src/utils/index.ts +++ b/frontend/src/utils/index.ts @@ -180,6 +180,35 @@ export class PromiseUtil { setTimeout(resolve, timeout); }); } + + // After the restart delay, polls the target until it answers below 500; false on timeout. + // https page -> http target is blocked as mixed content: probe this origin, the proxy's. + static async waitForPanel( + url: string, + { delay = 5000, interval = 2000, timeout = 90_000 } = {}, + ): Promise { + await PromiseUtil.sleep(delay); + const target = new URL(url, window.location.href); + const mixed = window.location.protocol === 'https:' && target.protocol === 'http:'; + const probe = mixed ? new URL(window.location.href) : target; + const sameOrigin = probe.origin === window.location.origin; + const deadline = Date.now() + timeout; + while (Date.now() < deadline) { + try { + const res = await fetch(probe, { + cache: 'no-store', + redirect: 'manual', + mode: sameOrigin ? 'same-origin' : 'no-cors', + signal: AbortSignal.timeout(Math.max(interval, 1000)), + }); + if (res.type === 'opaque' || res.type === 'opaqueredirect' || res.status < 500) return true; + } catch { + /* not listening yet */ + } + await PromiseUtil.sleep(interval); + } + return false; + } } export interface RandomSeqOptions {