Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 36 additions & 7 deletions src/main/window/createMainWindow-renderer-launch-failure.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ const LAUNCH_FAILED: Electron.RenderProcessGoneDetails = { reason: 'launch-faile
const CRASHED: Electron.RenderProcessGoneDetails = { reason: 'crashed', exitCode: 5 }

/** Reproduce spawn refusal: launch-failed then ERR_FAILED before a document; the same WebContents loads once process headroom returns. */
function createSpawnRefusingWindow(platform: NodeJS.Platform = 'darwin') {
function createSpawnRefusingWindow(
platform: NodeJS.Platform = 'darwin',
failure: Electron.RenderProcessGoneDetails = LAUNCH_FAILED
) {
const handlers: Record<string, (...args: any[]) => void> = {}
const spawn = { refused: true }
const webContents = {
Expand All @@ -58,7 +61,7 @@ function createSpawnRefusingWindow(platform: NodeJS.Platform = 'darwin') {
return Promise.resolve()
}
queueMicrotask(() =>
withPlatform(platform, () => handlers['render-process-gone']?.({}, LAUNCH_FAILED))
withPlatform(platform, () => handlers['render-process-gone']?.({}, failure))
)
return Promise.reject(
new Error("ERR_FAILED (-2) loading 'file:///opt/orca/renderer/index.html'")
Expand Down Expand Up @@ -186,14 +189,40 @@ describe('renderer launch-failed recovery', () => {
expect(onRendererRecoveryExhausted).not.toHaveBeenCalled()
})

it('keeps Windows launch failures on the short recovery schedule', async () => {
const { load } = createSpawnRefusingWindow('win32')
it('rides out a Windows sandbox CreateProcess refusal in place instead of prompting', async () => {
// Windows SBOX_ERROR_CREATE_PROCESS; awin repro (job capped at 1 process for 5s) prompted after ~1s on the 750ms breaker.
const { spawn } = createSpawnRefusingWindow('win32', {
reason: 'launch-failed',
exitCode: 18
})
const { onRendererRecoveryExhausted, onRecoveryReloadOutcome } = open()
await vi.advanceTimersByTimeAsync(5_000)
expect(onRendererRecoveryExhausted).not.toHaveBeenCalled()
spawn.refused = false

await vi.advanceTimersByTimeAsync(4_000)
expect(onRecoveryReloadOutcome).toHaveBeenLastCalledWith(
expect.objectContaining({ status: 'loaded' })
)
expect(onRendererRecoveryExhausted).not.toHaveBeenCalled()
})

it('still prompts on Windows once a persistent CreateProcess refusal spends the schedule', async () => {
const SANDBOX_REFUSED = { reason: 'launch-failed', exitCode: 18 } as const
const { load } = createSpawnRefusingWindow('win32', SANDBOX_REFUSED)
const { onRendererRecoveryExhausted } = open()
await vi.advanceTimersByTimeAsync(1_000)
expect(load).toHaveBeenCalledTimes(4)
await vi.advanceTimersByTimeAsync(BACKOFF_TOTAL_MS - 1)
expect(onRendererRecoveryExhausted).not.toHaveBeenCalled()

await vi.advanceTimersByTimeAsync(251)
expect(load).toHaveBeenCalledTimes(1 + RENDERER_LAUNCH_FAILURE_RETRY_DELAYS_MS.length)
expect(onRendererRecoveryExhausted).toHaveBeenCalledOnce()
expect(onRendererRecoveryExhausted).toHaveBeenCalledWith(
expect.objectContaining({ cause: 'launch-failed', recentRecoveryCount: 3 })
expect.objectContaining({
details: SANDBOX_REFUSED,
cause: 'launch-failed',
recentRecoveryCount: RENDERER_LAUNCH_FAILURE_RETRY_DELAYS_MS.length
})
)
})

Expand Down
7 changes: 3 additions & 4 deletions src/main/window/main-window-focus-lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,7 @@ export function installMainWindowFocusLifecycle(args: {
return
}
const launchFailed = details.reason === 'launch-failed'
const useLaunchBackoff = launchFailed && process.platform !== 'win32'
const launchRetryDelayMs = useLaunchBackoff ? launchFailureBackoff.nextDelayMs() : null
const launchRetryDelayMs = launchFailed ? launchFailureBackoff.nextDelayMs() : null
const goneAt = Date.now()
// Read commit at death; the next sampler tick would see memory the dead renderer released.
const lowCommit = lowCommitOomGate.assess(details, goneAt)
Comment on lines 208 to 214

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Major · Stability & Availability · Quick win

Supported by static analysis

Script executed:

sed -n '1,180p' src/main/window/renderer-liveness-at-death.ts
sed -n '175,310p' src/main/window/main-window-focus-lifecycle.ts
sed -n '35,85p' src/main/window/createMainWindow.ts

Repository: stablyai/orca

Length of output: 12743


Keep the launch-failure retry when the current document failed to load.

After launch-failed, a failed main-frame load can be followed by did-finish-load for the error document. The handler clears the pending retry and resets the backoff without checking whether the document loaded. The window can then stay on the error document with no reload and no escalation. The existing tracker already records this state in documentLoadFailed, but does not return it. Expose that flag, and clear the retry and reset the backoff only when the document did not fail.

Suggested fix
 export type RendererLivenessTracker = {
   /** Snapshot death and clear hung state so replacement responsiveness is not attributed to the dead renderer. */
   takeAtRendererGone: () => RendererLivenessAtDeath
+  /** True while the current main-frame document has a failed load and no newer navigation has started. */
+  isDocumentLoadFailed: () => boolean
 }
       unresponsiveSince = null
       return state
-    }
+    },
+    isDocumentLoadFailed: () => documentLoadFailed
   }
   rendererWebContents.on('did-finish-load', () => {
     rendererProcessGone = false
     attachBrowserClientPageRenderer(rendererWebContents)
-    clearRendererRecoveryTimer()
-    launchFailureBackoff.reset()
+    // Why: Electron fires did-finish-load for a failed main-frame load (error document).
+    // Keep the pending launch-failure retry and its backoff in that case.
+    if (!rendererLiveness.isDocumentLoadFailed()) {
+      clearRendererRecoveryTimer()
+      launchFailureBackoff.reset()
+    }
     recoveryReloadWatchdog.notifyDocumentLoaded()
   })

The retry timer still reloads the document after its backoff delay. A later successful load resets the flag through did-start-navigation, so the success path still clears the retry.

Add a regression case to createMainWindow-renderer-launch-failure.test.ts. The case should emit did-fail-load and then did-finish-load while spawn is still refused, and assert that the retry still fires.

Implementation effort is low. The change touches one type, one return value, and one handler, plus one test case.

Expand Down Expand Up @@ -237,7 +236,7 @@ export function installMainWindowFocusLifecycle(args: {
return
}
// Spawn refusal needs its own bounded backoff, independent of crash-loop accounting.
if (useLaunchBackoff) {
if (launchFailed) {
const subject = { details, recentRecoveryCount: launchFailureBackoff.attempts() }
if (launchRetryDelayMs === null) {
recoveryReloadWatchdog.escalate(subject, 'launch-failed')
Expand All @@ -251,7 +250,7 @@ export function installMainWindowFocusLifecycle(args: {
// Stop repeated crashes through the one-prompt watchdog; it also watches manual retry so another stall cannot strand a blank window.
recoveryReloadWatchdog.escalate(
{ details, recentRecoveryCount: recovery.recentRecoveryCount },
launchFailed ? 'launch-failed' : 'crash-loop'
'crash-loop'
)
return
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/window/renderer-launch-failure-backoff.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Refused spawns can last minutes (macOS 1003/process limit); spread retries over ~2min instead of the crash breaker's 750ms.
// Refused spawns last seconds to minutes (macOS 1003 process limit, Windows sandbox CreateProcess 18); spread retries over ~2min, not the crash breaker's 750ms.
export const RENDERER_LAUNCH_FAILURE_RETRY_DELAYS_MS: readonly number[] = [
250, 1_000, 2_000, 4_000, 8_000, 15_000, 30_000, 60_000
]
Expand Down
Loading