MicrosoftEdge / MicrosoftEdge/WebView2Feedback

AddScriptToExecuteOnDocumentCreatedAsync takes unusually long to complete on some machines

Open
#5,716 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
PowerShell
Stars
526
Forks
67
PR merge metrics
No merged PRs in 30d

Description

Description

On some Windows machines, CoreWebView2.AddScriptToExecuteOnDocumentCreatedAsync can take an unexpectedly long time to complete after the WebView2 controller has been created. In our observation, the registration callback returned S_OK approximately 1.5 seconds after the API was called.

During this interval, the first navigation may already have started and the first document may begin executing page code. As a result, a script that is expected to run for the document can be missing from that first document, even though the registration API eventually completes successfully.

Observed timing
T+0 ms       CoreWebView2 controller creation completes
T+1 ms       AddScriptToExecuteOnDocumentCreatedAsync is called
T+210 ms     first navigation starts
T+1,498 ms   the target document starts / source changes
T+1,514 ms   AddScriptToExecuteOnDocumentCreatedAsync callback completes with S_OK
T+1,? ms     page code checks for the injected value and sees it as undefined

The delay is not consistently reproducible on every machine. On affected machines, the registration callback takes about 1.5 seconds; on other machines it completes quickly enough that the problem is not observed.

Reproduction outline
  1. Create a WebView2 controller asynchronously.
  2. When the controller is created, call AddScriptToExecuteOnDocumentCreatedAsync with a small script that sets a unique global value.
  3. Start the first navigation immediately after issuing the registration call.
  4. In the first page, record the time at which the document starts, the value of the injected global, and the registration callback completion time.
  5. Repeat on different machines or under different system load conditions.
Expected behavior

The document-created script registration should complete with low and predictable latency after the controller is ready, or WebView2 should provide a way to determine that the registration has become effective before the first document begins executing.

Questions
  1. Are there known conditions that can cause AddScriptToExecuteOnDocumentCreatedAsync to take around 1.5 seconds or longer?
  2. Does the registration operation involve browser-process initialization or other work that can be delayed by machine-specific conditions?
  3. Is there a recommended diagnostic or mitigation for this registration latency?
  4. Does WebView2 guarantee that a document-created script is applied to a navigation started while the registration operation is still pending?
Environment
  • Windows
  • WebView2
  • The issue is observed when the controller is created asynchronously and the first navigation is to a remote HTTPS page.
  • The registration API eventually completes successfully with S_OK; the concern is the unusually long completion latency.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the asynchronous controller-creation flow and the AddScriptToExecuteOnDocumentCreatedAsync call described in the reproduction; capture callback, first-navigation, document-start, and script-observation timestamps on affected and unaffected Windows machines. Done means identifying a reproducible cause or documenting the WebView2 guarantee or mitigation when registration remains pending during the first navigation.

Written by the indexing model from the issue text.

Assessment

Domain
desktop
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.