kilocode icon indicating copy to clipboard operation
kilocode copied to clipboard

JetBrains WebView first load can stall before navigation and retries can reuse a dead JS bridge

Open keepfit opened this issue 2 weeks ago • 1 comments

JetBrains WebView first load can stall before navigation and retries can reuse a dead JS bridge

Summary

The JetBrains WebView startup path currently treats several different readiness boundaries as one load problem:

  1. Extension Host resolves the WebView provider.
  2. Cline sets webview.html.
  3. Kotlin prepares the HTML and calls loadURL.
  4. JCEF creates the native browser and starts navigation.
  5. CEF finishes the document load.
  6. The React application bootstraps, hydrates state, and becomes interactive.

The reported navigation watchdog exhausted happens only after step 3. It does not cover a stall in steps 1-2, and it does not prove that the page or JS bridge is usable after step 5.

Current staged work already separates native browser creation from navigation start and registers onAfterCreated before createImmediately(). The remaining work is to make the full lifecycle explicit, correlate every callback with the navigation that caused it, and make replacement recover a usable browser rather than only a loaded document.

Evidence

  • The provider resolve call is currently fire-and-forget from Kotlin:
    • jetbrains/plugin/src/main/kotlin/ai/kilocode/jetbrains/webview/WebViewManager.kt
    • jetbrains/plugin/src/main/kotlin/ai/kilocode/jetbrains/ipc/proxy/interfaces/ExtHostWebviewViewsProxy.kt
  • The first HTML watchdog starts only inside startInitialHtmlLoad() after Core has pushed HTML.
  • The general initialization timeout stops at EXTENSION_ACTIVATED, before WebView resolve or HTML arrival.
  • Tool-window readiness timeout starts only after EXTENSION_READY.
  • JBCefBrowserBase.loadURL() stores only one deferred load before native creation and ignores a repeated identical URL.
  • CEF calls onLoadEnd for successful, failed, and canceled frames; the current handler does not validate httpStatusCode.
  • CEF may report onLoadError(ERR_ABORTED) for a replaced or canceled navigation; the current handler immediately transitions the state machine to terminal FAILED.
  • Replacement reloads HTML that was prepared with the old JCEF instance's JBCefJSQuery. The injected cefQuery_* function is namespaced by client and slot, so it is not valid for the replacement browser.
  • Local macOS/IDEA 2024.3.7 logs confirm onAfterCreated is visible and fires before onLoadStart; it does not prove page readiness.

Impact

  • First open can remain on a blank or "Resolving Webview" state indefinitely when provider resolve or HTML generation stalls.
  • A 404 document can be marked as HTML_LOADED, exposed as ready, and then fail later without a useful recovery path.
  • A transient canceled navigation can permanently fail initialization.
  • Browser replacement can produce a visible page with a dead host bridge, so commands, heartbeat, and state updates stop working.
  • Repeated replacements can leak ExtHost WebView objects and JCEF resources.
  • Retry behavior can amplify resource contention instead of restoring service.

Findings To Close

  • [ ] Add an end-to-end watchdog from provider resolve through first $setHtml, not only from loadURL.
  • [ ] Await resolveWebviewView completion/error and stop advancing state optimistically.
  • [ ] Rebuild prepared HTML with the replacement WebView's JS bridge.
  • [ ] Correlate lifecycle events by browser generation, load version, expected URL, and main frame.
  • [ ] Treat only managed main-frame 2xx responses as a successful document load.
  • [ ] Make ERR_ABORTED non-fatal and route expected initial-load failures through the bounded retry policy.
  • [ ] Notify ExtHost when a replaced local WebView handle is disposed.
  • [ ] Apply the latest coalesced HTML after the active load settles.
  • [ ] Fail closed when critical JCEF handlers or createImmediately() fail.
  • [ ] Keep external navigation limited to intentional main-frame user gestures.
  • [ ] Add regression tests for stale events, 404, ERR_ABORTED, replacement bridge validity, coalesced HTML, and ExtHost disposal.
  • [ ] Verify Windows cold start, callback ordering, and helper-process cleanup.

Proposed Implementation Order

  1. Formalize readiness phases and retain raw HTML separately from per-browser prepared HTML.
  2. Add resolve and first-HTML watchdogs with stage-specific diagnostics.
  3. Correlate CEF callbacks and update completion/failure semantics.
  4. Make replacement rebuild the bridge, apply latest content, and dispose the old ExtHost handle.
  5. Fail closed on invalid handler setup.
  6. Add a local shell only after the lifecycle contract is stable; do not issue shell and app loadURL calls before native creation because only one deferred load is retained.

Acceptance Criteria

  • A provider resolve that never returns fails with a specific diagnostic within a bounded time.
  • A provider that resolves without pushing HTML fails with a specific diagnostic within a bounded time.
  • A missing HTML/resource returns 404 and never transitions to HTML_LOADED.
  • Replaced or canceled navigations do not terminate the current initialization.
  • A late event from an old generation cannot complete or fail the active load.
  • A replacement browser can exchange at least one inbound and one outbound message through the JS bridge.
  • Replacement disposes the old ExtHost handle and does not leave an unbounded WebView map.
  • Existing successful cold-start behavior remains unchanged on macOS.

Verification Boundary

  • Observed locally on macOS ARM64 with IntelliJ IDEA 2024.3.7.
  • Windows callback timing and helper-process behavior are still unverified.
  • External JCEF issues are corroborating evidence only; they do not by themselves reproduce this plugin failure.

References

keepfit avatar Nov 17 '26 07:11 keepfit

Closing: this analysis is being tracked only in the local repository, per maintainer request. No implementation tracking will continue on GitHub.

keepfit avatar Nov 17 '26 07:11 keepfit