Skip to content

The agent that grows with you

fix(desktop): auto-recover boundaries from React portal teardown races

liuhao10247a6407e4ff28
diff --git a/apps/desktop/src/components/error-boundary.test.tsx b/apps/desktop/src/components/error-boundary.test.tsx
index 853fe22036d..583126aeca0 100644
--- a/apps/desktop/src/components/error-boundary.test.tsx
+++ b/apps/desktop/src/components/error-boundary.test.tsx
@@ -218,3 +218,103 @@ describe('ErrorBoundary assistant-ui lookup recovery', () => {
expect(recoveryWarningCount(warnSpy.mock.calls)).toBe(0)
})
})
+
+// #98654: Radix portal teardown racing a host subtree swap makes vendor React
+// throw "Tried to unmount a fiber that is already unmounted" from INSIDE
+// whatever boundary hosts the portal (observed as `contrib:workspace`), so the
+// pane blanks until a manual Retry. The tree settles once the swap completes,
+// so a scoped boundary gets the same capped auto-recovery the root gets for
+// assistant-ui races.
+const PORTAL_UNMOUNT_ERROR = new Error('Tried to unmount a fiber that is already unmounted')
+
+const portalRecoveryWarningCount = (calls: unknown[][]) =>
+ calls.filter(call => call.some(value => String(value).includes('auto-recovering from portal teardown'))).length
+
+describe('ErrorBoundary portal teardown recovery', () => {
+ beforeEach(() => {
+ vi.useFakeTimers()
+ vi.setSystemTime(0)
+ vi.spyOn(console, 'error').mockImplementation(() => undefined)
+ })
+
+ afterEach(() => {
+ cleanup()
+ vi.useRealTimers()
+ vi.restoreAllMocks()
+ })
+
+ it('auto-recovers a scoped boundary that hosts the portal race (#98654)', () => {
+ const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => undefined)
+ const box: { error: Error | null } = { error: PORTAL_UNMOUNT_ERROR }
+ const Bomb = makeBomb(box)
+
+ render(
+ <ErrorBoundary fallback={() => <div>scoped fallback</div>} label="contrib:workspace">
+ <Bomb />
+ </ErrorBoundary>
+ )
+
+ box.error = null
+ act(() => vi.runOnlyPendingTimers())
+
+ expect(screen.getByText('recovered')).toBeTruthy()
+ expect(screen.queryByText('scoped fallback')).toBeNull()
+ expect(portalRecoveryWarningCount(warnSpy.mock.calls)).toBe(1)
+ })
+
+ it('stops retrying a persistent portal race after the recovery budget is exhausted', () => {
+ const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => undefined)
+ const Bomb = makeBomb({ error: PORTAL_UNMOUNT_ERROR })
+
+ render(
+ <ErrorBoundary fallback={() => <div>scoped fallback</div>} label="contrib:workspace">
+ <Bomb />
+ </ErrorBoundary>
+ )
+
+ for (let attempt = 0; attempt < 4; attempt += 1) {
+ act(() => vi.runOnlyPendingTimers())
+ }
+
+ expect(screen.getByText('scoped fallback')).toBeTruthy()
+ expect(portalRecoveryWarningCount(warnSpy.mock.calls)).toBe(3)
+ expect(vi.getTimerCount()).toBe(0)
+ })
+
+ it.each([
+ ['a near-miss unmount message', new Error('Tried to unmount a fiber that was painted twice')],
+ ['an unrelated render error', new Error('some unrelated application error')]
+ ])('does not auto-recover %s in a scoped boundary', (_label, error) => {
+ const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => undefined)
+ const Bomb = makeBomb({ error })
+
+ render(
+ <ErrorBoundary fallback={() => <div>scoped fallback</div>} label="contrib:workspace">
+ <Bomb />
+ </ErrorBoundary>
+ )
+
+ act(() => vi.runAllTimers())
+
+ expect(screen.getByText('scoped fallback')).toBeTruthy()
+ expect(portalRecoveryWarningCount(warnSpy.mock.calls)).toBe(0)
+ })
+
+ it('recovers at root as well, sharing the same budget as scoped boundaries', () => {
+ const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => undefined)
+ const box: { error: Error | null } = { error: PORTAL_UNMOUNT_ERROR }
+ const Bomb = makeBomb(box)
+
+ render(
+ <RootErrorBoundary>
+ <Bomb />
+ </RootErrorBoundary>
+ )
+
+ box.error = null
+ act(() => vi.runOnlyPendingTimers())
+
+ expect(screen.getByText('recovered')).toBeTruthy()
+ expect(portalRecoveryWarningCount(warnSpy.mock.calls)).toBe(1)
+ })
+})
diff --git a/apps/desktop/src/components/error-boundary.tsx b/apps/desktop/src/components/error-boundary.tsx
index 841f45a4b9f..0851473fa28 100644
--- a/apps/desktop/src/components/error-boundary.tsx
+++ b/apps/desktop/src/components/error-boundary.tsx
@@ -25,10 +25,18 @@ interface ErrorBoundaryState {
// the root. Retry only that exact transient error class, never arbitrary render
// failures, and cap retries so a persistent failure still exposes the fallback.
const ASSISTANT_UI_LOOKUP_ERROR = /(useClientLookup|tapClient(Lookup|Resource)).*out of bounds/
+// Vendor React throws this when portal teardown races a host subtree swap
+// (Radix menus/dialogs unmounting mid-commit — #98654, also seen in #41693).
+// The fiber graph settles once the swap completes, so a scheduled re-render
+// self-heals. Unlike the assistant-ui class, the throw comes from React
+// itself inside whatever boundary hosts the portal, so every boundary — not
+// just the root — gets the same capped recovery.
+const PORTAL_UNMOUNT_ERROR = /^Tried to unmount a fiber (?:that is already|that was not) unmounted/u
const MAX_AUTO_RECOVERIES = 3
const AUTO_RECOVERY_WINDOW_MS = 5_000
const isTransientAssistantUiLookupError = (error: Error): boolean => ASSISTANT_UI_LOOKUP_ERROR.test(error.message)
+const isTransientPortalUnmountError = (error: Error): boolean => PORTAL_UNMOUNT_ERROR.test(error.message)
export class ErrorBoundary extends Component<ErrorBoundaryProps, ErrorBoundaryState> {
state: ErrorBoundaryState = { error: null }
@@ -71,8 +79,12 @@ export class ErrorBoundary extends Component<ErrorBoundaryProps, ErrorBoundarySt
this.props.onError?.(error, info)
- if (this.props.label === 'root' && isTransientAssistantUiLookupError(error) && this.takeAutoRecoveryAttempt()) {
- console.warn(`${tag} auto-recovering from assistant-ui lookup render race`, error.message)
+ const assistantUiLookupRace = this.props.label === 'root' && isTransientAssistantUiLookupError(error)
+ const portalTeardownRace = isTransientPortalUnmountError(error)
+
+ if ((assistantUiLookupRace || portalTeardownRace) && this.takeAutoRecoveryAttempt()) {
+ const race = portalTeardownRace ? 'portal teardown' : 'assistant-ui lookup'
+ console.warn(`${tag} auto-recovering from ${race} render race`, error.message)
this.autoRecoveryPending = true
this.scheduleAutoRecovery()
}