Skip to content

Do not start the WebView to read cookies it cannot have on Android - #60067

Open
tomekzaw wants to merge 1 commit into
react:mainfrom
tomekzaw:startup/lazy-cookie-manager
Open

tomekzaw wants to merge 1 commit into
react:mainfrom
tomekzaw:startup/lazy-cookie-manager

Conversation

@tomekzaw

Copy link
Copy Markdown
Contributor

Summary

ForwardingCookieHandler calls android.webkit.CookieManager.getInstance() for every request made through NetworkingModule (fetch, XHR) and through Fresco's OkHttp client (remote images). The first call starts the WebView provider (Chromium) in the app's process: WebViewFactory: Loading com.google.android.webview, its dex and ICU data, CookieMonster*/CrAsyncTask threads, roughly 100 ms of CPU on background threads and tens of MB of memory. Every app whose startup does a fetch or shows a remote image pays this, whether or not it uses cookies or a WebView.

The WebView keeps its cookie store under app_webview* in the app's data directory, created the first time the provider runs in the app. When the handler is given a Context and that directory does not exist, there is nothing to read, so get() returns no cookies without starting the provider. The provider is started exactly as before as soon as a response sets a cookie (put()), or once the app has created a WebView (the directory then exists). NetworkingModule and FrescoModule pass the application context; handlers created without a context behave as before.

Changelog

[ANDROID] [CHANGED] - Network requests no longer start the WebView provider to read cookies while the WebView has never stored anything in the app

Benchmarks

RNTester release, arm64, real phones, dex state verify (OPPO: OEM speed-profile), app data cleared after each install so each arm starts from a fresh install, cold starts in interleaved blocks of 6 launches per arm. TTFD = reportFullyDrawn() on the first frame with React content, TTID = am start -W. Medians, bootstrap 95 % CIs of the median difference. RNTester triggers the path through a module-scope Image.prefetch of a facebook.com image whose response sets no cookie.

Phone n/arm Cold TTFD before → after Δ TTFD (CI95) Δ TTID (CI95)
Galaxy A16 (Helio G99, Android 16) 36 340 → 331 ms −9 ms, −2.6 % [−18, −2] −10 ms [−16, +6]
moto g57 power (Dimensity, Android 16) 36 268 → 260 ms −8 ms, −3.0 % [−14, 0] −9 ms [−15, −3]
Galaxy A57 5G (Exynos, Android 17) 36 199 → 201 ms +2 ms [−2, +4] −2 ms [−6, +3]
OPPO CPH2269 (Helio G35, Android 11) 36 1104 → 1114 ms +11 ms [−26, +45] (noise floor ≈ ±50 ms) +2 ms [−11, +19]
Pixel 9a (Tensor G4, Android 16) 18 150 → 151 ms +2 ms [−4, +12] +1 ms [−4, +6]

The gain is contention: the Chromium start runs on background threads while the main, JS and ReactHost threads are busy, so it shows on phones with fewer or slower cores and not on the A57. On the A16 the change removes WebViewFactory and libwebviewchromium.so loading from logcat and the CookieMonsterBa/CookieMonsterCl threads from the process; baseline shows both. Startup CPU drops by about 100 ms per cold start on the A16 (simpleperf, earlier measurement of the same path), independent of what reaches TTFD.

Trade-offs

  • Only fresh installs (and apps whose users never hit a cookie-setting response or a WebView) benefit. An app upgraded from a previous React Native version already has an app_webview directory, created by the old code path on its first request, and keeps the old behaviour. Over time, new installs cover the population.
  • The check relies on the WebView provider's data directory name (Context.getDir("webview") → app_webview, with an optional suffix from WebView.setDataDirectorySuffix). It has been that since the WebView became updatable; if a provider ever used a different directory, the only effect would be the old behaviour (provider started) once a cookie is set, or missing cookies until the first Set-Cookie if a provider stored cookies elsewhere. The context-less constructor keeps the old behaviour as an escape hatch.
  • One File.list() of the data directory per request until the directory exists (microseconds, on OkHttp's thread); after that a cached flag.

Test plan

  • ./gradlew :packages:react-native:ReactAndroid:testDebugUnitTest --tests "com.facebook.react.modules.network.ForwardingCookieHandlerTest" (new, Robolectric): no directory → empty, directory present → cookies returned, put then get works across handler instances, no-context constructor unchanged. ReactCookieJarContainerTest still passes.
  • RNTester release on the five phones above; Image examples load; the Image Loading Events example (prefetch/queryCache) works.
  • ReactAndroid.api updated for the new constructor (validation is disabled in this repo, snapshot edited by hand).
  • Benchmarks above (harness/launch.py, ab_states.sh from the measurement workspace).

🤖 Generated with Claude Code

ForwardingCookieHandler calls CookieManager.getInstance() on every request,
which starts the WebView provider (Chromium) in the process: about 100 ms of
CPU on background threads, several threads and tens of MB of memory, in every
app whose startup does a fetch or loads a remote image.

The WebView keeps its cookie store in app_webview* under the app's data
directory, created the first time the provider runs in this app. When the
handler is given a Context and that directory does not exist, there are no
cookies to send, so get() returns none without starting the provider. The
provider is started as before as soon as a response sets a cookie, or once a
WebView has been used in the app (the directory then exists).

NetworkingModule and FrescoModule pass the application context. Handlers
created without a context behave as before.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Nov 17, 2026
@facebook-github-tools facebook-github-tools Bot added p: Software Mansion Partner: Software Mansion Partner labels Nov 17, 2026
@github-actions

Copy link
Copy Markdown

Visual regression

No visual differences at d6dfdefe70a8.

ios: no capture

@tomekzaw
tomekzaw marked this pull request as ready for review November 17, 2026 09:17
@facebook-github-tools facebook-github-tools Bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Nov 17, 2026
private const val COOKIE_HEADER = "Cookie"

/** `Context.getDir("webview", …)` as used by the WebView provider, optionally with a suffix. */
private const val WEBVIEW_DATA_DIR_PREFIX = "app_webview"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How stable / future-proof is this?

@javache javache left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! We had previously also identified the impact to memory and startup of this component.

I wonder what the actual usage of cookie sharing is between webview and networking, and whether we'd be better off deprecating this functionality or making it opt-in, and have users explicitly share cookies to webviews.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. p: Software Mansion Partner: Software Mansion Partner Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants