inject request inspector JS at document start to fix race condition#29
Open
maxeoneo wants to merge 1 commit into
Open
inject request inspector JS at document start to fix race condition#29maxeoneo wants to merge 1 commit into
maxeoneo wants to merge 1 commit into
Conversation
acsbendi
reviewed
Jul 19, 2026
The fetch interceptor was injected via evaluateJavascript() inside onPageStarted(), which queues the script asynchronously on the WebView JS thread. Because the bundled webapp is served synchronously from local assets (no network latency), it could execute and make API fetch calls before the injection ran. This meant window.fetch was not yet overridden: no UUID header was injected, recordFetch() was never called, and shouldInterceptRequest() could not match the recorded request — resulting in an empty request body being proxied. We could fix it by using WebViewCompat.addDocumentStartJavaScript(), registered once at WebView init time. This API guarantees the script runs at document creation before any page scripts execute, eliminating the race condition. The solution falls back to the evaluateJavascript approach on WebView versions that do not support DOCUMENT_START_SCRIPT. Another race condition for the GeneratedUuidInHeaderRequestMatcher is, that before the first request of the webapp is done, the origin needs to be set. Since onPageStarted is executed asynchronously, it could still happen that it is executed after the first request. That would lead to the additional header being not set, because the matcher believes to deal with a CORS request. This change also makes sure that the origin is set within "onLoadMainFrame", because it is called when the webapp itself is loaded. This leads to the origin being set before the first request of the webapp can be done.
maxeoneo
force-pushed
the
fix/early-webapp-requests
branch
from
July 20, 2026 06:05
49705ac to
966924d
Compare
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.
The fetch interceptor was injected via evaluateJavascript() inside onPageStarted(), which queues the script asynchronously on the WebView JS thread. Because the bundled webapp is served synchronously from local assets (no network latency), it could execute and make API fetch calls before the injection ran. This meant window.fetch was not yet overridden: no UUID header was injected, recordFetch() was never called, and shouldInterceptRequest() could not match the recorded request — resulting in an empty request body being proxied.
We could fix it by using WebViewCompat.addDocumentStartJavaScript(), registered once at WebView init time. This API guarantees the script runs at document creation before any page scripts execute, eliminating the race condition. The solution falls back to the evaluateJavascript approach on WebView versions that do not support DOCUMENT_START_SCRIPT.
Another race condition for the GeneratedUuidInHeaderRequestMatcher is, that before the first request of the webapp is done, the origin needs to be set. Since onPageStarted is executed asynchronously, it could still happen that it is executed after the first request. That would lead to the additional header being not set, because the matcher believes to deal with a CORS request. So the fix is to check the request for
isMainFrame, indicating that the webapp itself gets loaded and set the origin there.This change also makes sure that the origin is set within "onLoadMainFrame", because it is called when the webapp itself is loaded. This leads to the origin being set before the first request of the webapp can be done.