Skip to content

WebView: recover when the content process dies (macOS) - #46

Open
jamierpond wants to merge 1 commit into
eyalamirmusic:mainfrom
jamierpond:jp/webview-content-process-terminate
Open

WebView: recover when the content process dies (macOS)#46
jamierpond wants to merge 1 commit into
eyalamirmusic:mainfrom
jamierpond:jp/webview-content-process-terminate

Conversation

@jamierpond

Copy link
Copy Markdown
Contributor

macOS kills WebContent processes routinely — memory pressure, sleep/wake cycles. When that happens, WebKit only tells the navigation delegate, via webViewWebContentProcessDidTerminate:. The eacp delegate didn't implement that selector, so nobody heard about it and the WKWebView stayed a permanently dead (opaque black) view until the app relaunched. We hit this in production: an app opened in the morning after the machine slept overnight shows a black rectangle where the page should be, and only a force-quit fixes it.

The fix implements the selector:

  • Reload synchronously inside the delegate callback. This ordering matters and cost some experimentation: a reload issued from inside the callback survives WebKit's own post-crash reset, while a load deferred to a later runloop turn (or issued fresh via loadRequest:) gets clobbered by it and you end up on an empty about:blank.
  • Notify the owner via a new onContentProcessTerminated callback, deferred like the other owner callbacks.
  • HTML-string loads leave no back-forward item, so reload can't restore them (and a re-issued loadHTMLString against the replacement process dedupes to an empty document — WebKit quirk). There the owner callback is the recovery path: a loadHTML embedder re-issues its load from it, which works.

Tests kill the live WebContent process for real (SIGKILL via the test-only _webProcessIdentifier SPI — the same death a jetsam delivers) and require the page to come back:

  • delegateImplementsTheSelector — the respondsToSelector: contract with WebKit
  • pageReloadsAfterContentProcessDies — custom-scheme URL load recovers on its own
  • htmlStringEmbedderRecoversViaCallback — loadHTML embedder recovers via the callback

All 65 WebView tests green, stable across repeated runs; full project builds clean.

The WebView2 backend has the analogous gap (add_ProcessFailed isn't wired) — left for a follow-up since I can't test Windows from here.

🤖 Generated with Claude Code

https://claude.ai/code/session_019fvd7ppoLmWHGmQrWUry9F

macOS jetsams WebContent processes routinely (memory pressure,
sleep/wake). The delegate did not implement
webViewWebContentProcessDidTerminate:, so WebKit told nobody and the
view stayed an opaque dead rectangle until the app relaunched.

Implement the selector: reload synchronously inside the callback (a
reload issued from there survives WebKit's post-crash reset; a deferred
load gets clobbered by it), and notify the owner via a new
onContentProcessTerminated callback. HTML-string loads leave no
back-forward item to reload — there the callback is the recovery path,
and a loadHTML embedder re-issues its load from it.

Tested by SIGKILLing the live WebContent process for real, both load
shapes.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019fvd7ppoLmWHGmQrWUry9F
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant