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
This commit is contained in:
Aleksey Fedorov
2026-11-25 22:08:34 +10:00
committed by GitHub
parent efb6c6d579
commit 4013219698
4 changed files with 111 additions and 3 deletions
+1 -1
View File
@@ -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();
+3 -2
View File
@@ -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);
}
+78
View File
@@ -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' });
});
});
+29
View File
@@ -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<boolean> {
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 {