Skip to content

🀯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

πŸ› fix(desktop): wait for the replacement navigation to commit after ERR_ABORTED (#20489)

Arvin Xu3dc6ad1e0385
diff --git a/apps/desktop/src/main/controllers/BrowserSidebarCtr.ts b/apps/desktop/src/main/controllers/BrowserSidebarCtr.ts
index 76220feb503..4bdf23d3a2d 100644
--- a/apps/desktop/src/main/controllers/BrowserSidebarCtr.ts
+++ b/apps/desktop/src/main/controllers/BrowserSidebarCtr.ts
@@ -309,26 +309,100 @@ export default class BrowserSidebarCtr extends ControllerModule {
// pushState on the old page, and a same-document target settles loadURL
// right away anyway.
let committed = false;
- const onNavigate = () => {
+ let onCommit: (() => void) | undefined;
+ let committedUrl: string | undefined;
+ const onNavigate = (_event: unknown, navigatedUrl: string) => {
committed = true;
+ committedUrl = navigatedUrl;
+ onCommit?.();
};
webContents.on('did-navigate', onNavigate);
+ const deadline = Date.now() + NAVIGATION_SETTLE_TIMEOUT_MS;
let settleTimer: ReturnType<typeof setTimeout> | undefined;
- const outcome = await Promise.race([
- webContents.loadURL(url).then(
- () => ({ status: 'loaded' as const }),
- (error: Error & { errno?: number }) => ({ error, status: 'failed' as const }),
- ),
+ const waitUntilDeadline = () =>
new Promise<{ status: 'pending' }>((resolve) => {
settleTimer = setTimeout(
() => resolve({ status: 'pending' }),
- NAVIGATION_SETTLE_TIMEOUT_MS,
+ Math.max(0, deadline - Date.now()),
);
- }),
- ]).finally(() => {
+ });
+ let outcome:
+ { error: Error & { errno?: number }; status: 'failed' } | { status: 'loaded' | 'pending' };
+ try {
+ outcome = await Promise.race([
+ webContents.loadURL(url).then(
+ () => ({ status: 'loaded' as const }),
+ (error: Error & { errno?: number }) => ({ error, status: 'failed' as const }),
+ ),
+ waitUntilDeadline(),
+ ]);
+ clearTimeout(settleTimer);
+
+ // ERR_ABORTED can reject as soon as the replacement navigation (a redirect
+ // or a newer load) starts, before it commits. Wait for that replacement
+ // within the same budget rather than reporting the document it replaces β€”
+ // the previous page, or the requested one when it committed and then
+ // redirected (script or meta refresh) before finishing its load.
+ if (
+ outcome.status === 'failed' &&
+ outcome.error.errno === NAVIGATION_ABORTED_ERRNO &&
+ webContents.isLoading()
+ ) {
+ // Only a commit after the abort is the replacement's.
+ const committedBeforeAbort = committed;
+ committed = false;
+ // The replacement can also fail, return a 204 or be cancelled; it then
+ // stops loading without ever committing.
+ let failure: string | undefined;
+ const onFailLoad = (
+ _event: unknown,
+ errorCode: number,
+ errorDescription: string,
+ _validatedURL: string,
+ isMainFrame: boolean,
+ ) => {
+ if (isMainFrame && errorCode !== NAVIGATION_ABORTED_ERRNO) {
+ failure = `${errorDescription} (${errorCode})`;
+ }
+ };
+ let onStop: (() => void) | undefined;
+ webContents.on('did-fail-load', onFailLoad);
+ try {
+ outcome = await Promise.race([
+ new Promise<{ status: 'loaded' }>((resolve) => {
+ onCommit = () => resolve({ status: 'loaded' });
+ }),
+ new Promise<{ error: Error; status: 'failed' } | { status: 'loaded' }>((resolve) => {
+ onStop = () => {
+ if (committed) return;
+ // A redirect that was cancelled or got a 204 leaves the requested
+ // document showing; one that failed outright shows its error page,
+ // possibly under the same URL.
+ if (committedBeforeAbort && !failure && webContents.getURL() === committedUrl) {
+ resolve({ status: 'loaded' });
+ return;
+ }
+ resolve({
+ error: new Error(failure ?? 'the page stopped loading before it opened'),
+ status: 'failed',
+ });
+ };
+ webContents.on('did-stop-loading', onStop);
+ }),
+ waitUntilDeadline(),
+ ]);
+ // A slow replacement after the requested document committed is the
+ // ordinary still-loading case.
+ committed ||= committedBeforeAbort;
+ } finally {
+ webContents.removeListener('did-fail-load', onFailLoad);
+ if (onStop) webContents.removeListener('did-stop-loading', onStop);
+ }
+ }
+ } finally {
clearTimeout(settleTimer);
webContents.removeListener('did-navigate', onNavigate);
- });
+ }
this.updateSnapshot(params.sessionId);
diff --git a/apps/desktop/src/main/controllers/__tests__/BrowserSidebarCtr.test.ts b/apps/desktop/src/main/controllers/__tests__/BrowserSidebarCtr.test.ts
index 52cb01730e9..b6d02cd99d1 100644
--- a/apps/desktop/src/main/controllers/__tests__/BrowserSidebarCtr.test.ts
+++ b/apps/desktop/src/main/controllers/__tests__/BrowserSidebarCtr.test.ts
@@ -252,6 +252,220 @@ describe('BrowserSidebarCtr retained webview registration', () => {
}),
).resolves.toEqual({ success: true });
});
+
+ it('waits for a slow replacement navigation to commit after ERR_ABORTED', async () => {
+ vi.useFakeTimers();
+ const guest = createWebContents(7);
+ guest.getURL.mockReturnValue('http://127.0.0.1:16001/');
+ // A redirect rejects loadURL as the replacement starts; the replacement
+ // document only commits 10s later.
+ guest.isLoading.mockReturnValue(true);
+ guest.loadURL = vi.fn(async () => {
+ setTimeout(() => {
+ guest.getURL.mockReturnValue('https://example.com/landing');
+ guest.emit('did-navigate', {}, 'https://example.com/landing');
+ }, 10_000);
+ throw Object.assign(new Error('ERR_ABORTED (-3)'), { errno: -3 });
+ });
+ await register(guest);
+
+ let settled: unknown;
+ const pending = invokeIpc('browserSidebar.navigate', {
+ sessionId: 'topic:a',
+ url: 'https://example.com/redirects',
+ }).then((result) => (settled = result));
+ await vi.advanceTimersByTimeAsync(8000);
+ expect(settled).toBeUndefined();
+
+ await vi.advanceTimersByTimeAsync(2000);
+ await expect(pending).resolves.toEqual({ success: true });
+ expect(guest.listenerCount('did-navigate')).toBe(1);
+ vi.useRealTimers();
+ });
+
+ it('waits for a redirect that starts after the requested document committed', async () => {
+ vi.useFakeTimers();
+ const guest = createWebContents(7);
+ guest.getURL.mockReturnValue('http://127.0.0.1:16001/');
+ guest.isLoading.mockReturnValue(true);
+ // The requested page commits, then a script redirect aborts its load; the
+ // redirect target only commits 10s later.
+ guest.loadURL = vi.fn(async () => {
+ guest.getURL.mockReturnValue('https://example.com/start');
+ guest.emit('did-navigate', {}, 'https://example.com/start');
+ setTimeout(() => {
+ guest.getURL.mockReturnValue('https://example.com/landing');
+ guest.emit('did-navigate', {}, 'https://example.com/landing');
+ }, 10_000);
+ throw Object.assign(new Error('ERR_ABORTED (-3)'), { errno: -3 });
+ });
+ await register(guest);
+
+ let settled: unknown;
+ const pending = invokeIpc('browserSidebar.navigate', {
+ sessionId: 'topic:a',
+ url: 'https://example.com/start',
+ }).then((result) => (settled = result));
+ await vi.advanceTimersByTimeAsync(8000);
+ expect(settled).toBeUndefined();
+
+ await vi.advanceTimersByTimeAsync(2000);
+ await expect(pending).resolves.toEqual({ success: true });
+ vi.useRealTimers();
+ });
+
+ it('keeps the committed page when its redirect stops without committing', async () => {
+ vi.useFakeTimers();
+ const guest = createWebContents(7);
+ guest.isLoading.mockReturnValue(true);
+ // The requested page commits, then a redirect it starts gets a 204.
+ guest.loadURL = vi.fn(async () => {
+ guest.getURL.mockReturnValue('https://example.com/start');
+ guest.emit('did-navigate', {}, 'https://example.com/start');
+ setTimeout(() => guest.emit('did-stop-loading'), 1000);
+ throw Object.assign(new Error('ERR_ABORTED (-3)'), { errno: -3 });
+ });
+ await register(guest);
+
+ const pending = invokeIpc('browserSidebar.navigate', {
+ sessionId: 'topic:a',
+ url: 'https://example.com/start',
+ });
+ await vi.advanceTimersByTimeAsync(1000);
+
+ await expect(pending).resolves.toEqual({ success: true });
+ vi.useRealTimers();
+ });
+
+ it('reports a redirect from the committed page that fails to its error page', async () => {
+ vi.useFakeTimers();
+ const guest = createWebContents(7);
+ guest.isLoading.mockReturnValue(true);
+ guest.loadURL = vi.fn(async () => {
+ guest.getURL.mockReturnValue('https://example.com/start');
+ guest.emit('did-navigate', {}, 'https://example.com/start');
+ setTimeout(() => {
+ // Chromium commits the error page under the failed URL without did-navigate.
+ guest.getURL.mockReturnValue('http://127.0.0.1:18748/');
+ guest.emit(
+ 'did-fail-load',
+ {},
+ -102,
+ 'ERR_CONNECTION_REFUSED',
+ 'http://127.0.0.1:18748/',
+ true,
+ );
+ guest.emit('did-stop-loading');
+ }, 1000);
+ throw Object.assign(new Error('ERR_ABORTED (-3)'), { errno: -3 });
+ });
+ await register(guest);
+
+ const pending = invokeIpc('browserSidebar.navigate', {
+ sessionId: 'topic:a',
+ url: 'https://example.com/start',
+ });
+ await vi.advanceTimersByTimeAsync(1000);
+
+ await expect(pending).resolves.toEqual({
+ error:
+ 'Could not open https://example.com/start: ERR_CONNECTION_REFUSED (-102). The browser is still showing http://127.0.0.1:18748/.',
+ success: false,
+ });
+ vi.useRealTimers();
+ });
+
+ it('reports a same-URL reload of the committed page that fails', async () => {
+ vi.useFakeTimers();
+ const guest = createWebContents(7);
+ guest.isLoading.mockReturnValue(true);
+ guest.loadURL = vi.fn(async () => {
+ guest.getURL.mockReturnValue('https://example.com/start');
+ guest.emit('did-navigate', {}, 'https://example.com/start');
+ setTimeout(() => {
+ guest.emit(
+ 'did-fail-load',
+ {},
+ -105,
+ 'ERR_NAME_NOT_RESOLVED',
+ 'https://example.com/start',
+ true,
+ );
+ guest.emit('did-stop-loading');
+ }, 1000);
+ throw Object.assign(new Error('ERR_ABORTED (-3)'), { errno: -3 });
+ });
+ await register(guest);
+
+ const pending = invokeIpc('browserSidebar.navigate', {
+ sessionId: 'topic:a',
+ url: 'https://example.com/start',
+ });
+ await vi.advanceTimersByTimeAsync(1000);
+
+ await expect(pending).resolves.toEqual({
+ error:
+ 'Could not open https://example.com/start: ERR_NAME_NOT_RESOLVED (-105). The browser is showing its error page.',
+ success: false,
+ });
+ vi.useRealTimers();
+ });
+
+ it('does not report success when the replacement after ERR_ABORTED never commits', async () => {
+ vi.useFakeTimers();
+ const guest = createWebContents(7);
+ guest.getURL.mockReturnValue('http://127.0.0.1:16001/');
+ guest.isLoading.mockReturnValue(true);
+ guest.loadURL = vi.fn(async () => {
+ throw Object.assign(new Error('ERR_ABORTED (-3)'), { errno: -3 });
+ });
+ await register(guest);
+
+ const pending = invokeIpc('browserSidebar.navigate', {
+ sessionId: 'topic:a',
+ url: 'https://example.com/redirects',
+ });
+ await vi.advanceTimersByTimeAsync(15_000);
+
+ await expect(pending).resolves.toEqual({
+ error:
+ 'https://example.com/redirects has not responded within 15s, so the browser is still showing http://127.0.0.1:16001/. The load continues in the background β€” check with readPage or snapshot before acting on the page, or navigate again.',
+ success: false,
+ });
+ expect(guest.listenerCount('did-navigate')).toBe(1);
+ vi.useRealTimers();
+ });
+
+ it('reports a replacement after ERR_ABORTED that stops without committing', async () => {
+ vi.useFakeTimers();
+ const guest = createWebContents(7);
+ guest.getURL.mockReturnValue('http://127.0.0.1:16001/');
+ guest.isLoading.mockReturnValue(true);
+ guest.loadURL = vi.fn(async () => {
+ setTimeout(() => {
+ guest.emit('did-fail-load', {}, -102, 'ERR_CONNECTION_REFUSED', 'http://x/', true);
+ guest.emit('did-stop-loading');
+ }, 1000);
+ throw Object.assign(new Error('ERR_ABORTED (-3)'), { errno: -3 });
+ });
+ await register(guest);
+
+ const pending = invokeIpc('browserSidebar.navigate', {
+ sessionId: 'topic:a',
+ url: 'https://example.com/redirects',
+ });
+ await vi.advanceTimersByTimeAsync(1000);
+
+ await expect(pending).resolves.toEqual({
+ error:
+ 'Could not open https://example.com/redirects: ERR_CONNECTION_REFUSED (-102). The browser is still showing http://127.0.0.1:16001/.',
+ success: false,
+ });
+ // Only the page's own listeners remain.
+ expect(guest.listenerCount('did-fail-load')).toBe(1);
+ expect(guest.listenerCount('did-stop-loading')).toBe(1);
+ vi.useRealTimers();
+ });
});
it('keeps sessions isolated and activates the most recently registered host', async () => {