[Win32] Fix BrowserFunction missing on first page - #3479
Open
HeikoKlare wants to merge 2 commits into
Open
Conversation
Add two backend-agnostic regression tests for BrowserFunction lifecycle: - a disposed BrowserFunction must not be re-injected on a subsequently loaded page - redefining a function with the same name takes effect and survives navigation, and the previous definition is not resurrected Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Contributor
A BrowserFunction created while an Edge/WebView2 browser is still initializing (with a navigation queued via setUrl/setText) could be missing on the first loaded page: AddScriptToExecuteOnDocumentCreated was issued only after the queued navigation and raced with document creation, so the function was sometimes not yet registered when the page's document was created. With the change, we register the document-created scripts of all pending BrowserFunctions inside WebViewProvider.initializeWebView(), before completing the initialization future. Completing the future synchronously runs the queued navigation, so registering beforehand guarantees the functions are in place before the first document is created. This is the only point that is reliably "after init, before the first navigation", since a CompletableFuture navigation chain cannot be preempted retroactively. Registration is now issued asynchronously (fire-and-forget). What makes a function land on a page is issuing the registration before the navigation is issued to WebView2, not waiting for its completion. As a result the previous inCallback>0 deferral is no longer needed (an async call cannot deadlock inside a WebView2 callback) and the separate synchronous registration method is removed, leaving a single registration method and a single branch in createFunction. deregisterFunction stays correct for a registration whose script ID has not arrived yet: the async completion callback detects the removed function via the functions map and removes the just-registered script itself. Fixes eclipse-platform#3370 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
HeikoKlare
force-pushed
the
fix/edge-browserfunction-concurrent-init-issue20
branch
from
July 31, 2026 16:41
762e863 to
336e925
Compare
HeikoKlare
marked this pull request as ready for review
July 31, 2026 17:01
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #3370 — the sporadic failure of
test_BrowserFunction_availableOnLoad_concurrentInstances_issue20on the Edge/WebView2 backend.Builds on top of #3478 (BrowserFunction dispose/redefine regression tests) and employs the regression tests added there — #3478 should be merged first.
Problem
A
BrowserFunctioncreated while an Edge browser is still initializing (with a navigation queued viasetUrl/setText) could be missing on the first loaded page.AddScriptToExecuteOnDocumentCreatedwas issued only after the queued navigation and raced with document creation, so the function was sometimes not registered when the page's document was created — surfacing asSWTException: <function> is not defined.Fix
Register the document-created scripts of all pending
BrowserFunctions insideWebViewProvider.initializeWebView(), before completing the initialization future (which synchronously runs the queued navigation). This is the only point that is reliably "after init, before the first navigation", since theCompletableFuturenavigation chain cannot be preempted retroactively.Registration is now issued asynchronously: what makes a function land on a page is issuing the registration before the navigation is issued to WebView2, not waiting for its completion. This also removes the previous
inCallback > 0deferral (an async call cannot deadlock inside a WebView2 callback) and the separate synchronous registration method, leaving a single registration method and a single branch increateFunction.deregisterFunctionstays correct for a registration whose script ID has not arrived yet (its async completion detects the removed function and removes the script itself).Tests
Adds regression tests for function creation during concurrent initialization: creating a function from inside a callback, availability before the first page's inline scripts run, and multiple functions registered before initialization completes. These complement the dispose/redefine regression tests from #3478.
Reproducer
The underlying race is a sub-millisecond window and cannot be forced deterministically through the public API with a single browser. The following standalone reproducer makes it deterministic in practice by stressing concurrent initialization: before the fix it reports
options is not definedwithin a few iterations; after the fix it completes all iterations cleanly.Standalone reproducer (Windows/Edge)