Skip to content

Remote cache: fix write-back during hedged reads and a getParams edge case (#2251) - #2251

Open
GijsWeterings wants to merge 1 commit into
mainfrom
export-D133210770
Open

GijsWeterings wants to merge 1 commit into
mainfrom
export-D133210770

Conversation

@GijsWeterings

@GijsWeterings GijsWeterings commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Summary:

Follow-ups from the react/metro review of the two remote cache changes (#2249, #2246).

A hedged local transform no longer writes to a remote store that is about to hit. Cache.set skips the store that served a hit and the stores after it, using Cache.#hits, which is only filled once a get returns. With unstable_hedgeCacheReadsAfterMs, the local transform can win while the remote read is still pending, so set wrote to every store: with a read-write remote (HttpStore), one redundant upload per file that already exists remotely, on the congested link the option is for. HttpGetStore does not write, so the setup this was built for was not affected.

Cache now tracks the store each pending get is reading. A set during that read writes the stores the read has already missed at once, and the rest when the read settles, stopping at the store that hit: the same stores a set after the read would write. On a miss everything is still written; nothing changes when no read is pending. The bookkeeping is one small object per get, and a promise only when a set overlaps it.

getParams resolving to a plain object or nothing is a cache miss, not a build failure. The parameters are now built with new URLSearchParams(extra ?? undefined) inside the guarded part, so a plain object such as {token: 'abc'} is used as parameters and undefined/null adds none, and anything else that fails becomes a NetworkError. Errors from the request itself are still not wrapped.

Docs: Configuration.md said racing transforms run at most maxWorkers at a time; they share 2 * maxWorkers slots with cache-miss transforms. Caching.md describes the write order when a write overlaps a read.

Changelog: [Fix] metro-cache: a result written while a read of the same key is still under way no longer writes to the store that read then hits, and an HttpStore getParams that resolves to a plain object or nothing no longer fails the build

Differential Revision: D133210770

@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 19, 2026
@meta-codesync

meta-codesync Bot commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

@GijsWeterings has exported this pull request. If you are a Meta employee, you can view the originating Diff in D133210770.

… case (#2251)

Summary:

Follow-ups from the react/metro review of the two remote cache changes (#2249, #2246).

**A hedged local transform no longer writes to a remote store that is about to hit.** `Cache.set` skips the store that served a hit and the stores after it, using `Cache.#hits`, which is only filled once a `get` returns. With `unstable_hedgeCacheReadsAfterMs`, the local transform can win while the remote read is still pending, so `set` wrote to every store: with a read-write remote (`HttpStore`), one redundant upload per file that already exists remotely, on the congested link the option is for. `HttpGetStore` does not write, so the setup this was built for was not affected.

`Cache` now tracks the store each pending `get` is reading. A `set` during that read writes the stores the read has already missed at once, and the rest when the read settles, stopping at the store that hit: the same stores a `set` after the read would write. On a miss everything is still written; nothing changes when no read is pending. The bookkeeping is one small object per `get`, and a promise only when a `set` overlaps it.

**`getParams` resolving to a plain object or nothing is a cache miss, not a build failure.** The parameters are now built with `new URLSearchParams(extra ?? undefined)` inside the guarded part, so a plain object such as `{token: 'abc'}` is used as parameters and `undefined`/`null` adds none, and anything else that fails becomes a `NetworkError`. Errors from the request itself are still not wrapped.

**Docs:** `Configuration.md` said racing transforms run at most `maxWorkers` at a time; they share `2 * maxWorkers` slots with cache-miss transforms. `Caching.md` describes the write order when a write overlaps a read.

Changelog: [Fix] metro-cache: a result written while a read of the same key is still under way no longer writes to the store that read then hits, and an `HttpStore` `getParams` that resolves to a plain object or nothing no longer fails the build

Differential Revision: D133210770
@meta-codesync meta-codesync Bot changed the title Remote cache: fix write-back during hedged reads and a getParams edge case Remote cache: fix write-back during hedged reads and a getParams edge case (#2251) Nov 19, 2026
@github-actions

Copy link
Copy Markdown

Metro review: Looks good

Risk: low · Commit: 6366581 · 6 commands run

The PR records which store each pending Cache.get is reading. If a set overlaps that read, it writes the stores already missed right away and holds the rest until the read settles, so a hedged local transform no longer uploads to a remote store that then hits. It also makes HttpStore treat a getParams that returns a plain object, null or undefined as valid. A set after the read writes the same stores this fix does, and Transformer fires set without awaiting it, so holding the write until the read settles never delays a transform. The new tests fail on the base and pass on the head. I found no problems.

No findings.

Verified
  • ✅ yarn jest --no-watchman packages/metro-cache packages/metro/src/DeltaBundler/__tests__/Transformer-test.js — 8 suites, 93 tests pass on the head
  • ✅ yarn flow check — No errors
  • ✅ yarn eslint packages/metro-cache/src packages/metro/src/DeltaBundler/__tests__/Transformer-test.js
  • ✅ (base checkout) yarn jest --no-watchman Cache-review-test Transformer-review-test # head's test files copied over base source (failed as expected) — Fail on the base: the 2 new Cache overlap tests and the new Transformer hedged write-back test
  • ✅ (base checkout) yarn jest --no-watchman packages/metro-cache/src/stores/__tests__/HttpGetStore-review-test.js (failed as expected) — On the base, a getParams that resolves to a plain object causes an unhandled 'TypeError: extraParams is not iterable', which crashes the test process
  • ✅ node bench/cache-get.js — Paired base/head microbenchmark of Cache.get+set

Benchmarks (base vs this PR)

Benchmark Base This PR Change
200k Cache.get + Cache.set (no overlap), in-memory stores (one sync miss, one async hit) 236ms (233-246) 246ms (240-259) ~+4%, about 50ns per get+set
Method
  • 200k Cache.get + Cache.set (no overlap), in-memory stores (one sync miss, one async hit): One Node process loads both checkouts' Cache.js and alternates base/head runs. 3 warm-up pairs, then 15 measured pairs per side. Medians with min-max range. Script: bench/cache-get.js. In-memory stores give the worst case. Real stores do file or network I/O, which is far slower than this overhead.

Not run: Windows was not run because no Windows machine was available for this review, and the change uses no paths or other platform-specific behavior. I didn't run an end-to-end build against a real HttpStore remote. The overlap behavior is covered by the unit tests in Cache-test.js and Transformer-test.js.

Automated review by Claude. Advisory only: it never approves or blocks, and a maintainer makes the call.

@robhogan robhogan left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for following up!

@robhogan robhogan added the Auto review: Good The Metro review bot found nothing to change at this head label Nov 20, 2026

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

Auto review: Good The Metro review bot found nothing to change at this head CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. meta-exported

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants