Repository navigation
[compiler] Allow ref access in PerformanceObserver / ReportingObserver callbacks - #37996
Merged
Merged
Conversation
…r callbacks
`new PerformanceObserver(() => ref.current = ...)` and
`new ReportingObserver(() => ref.current = ...)` report a false positive:
```
Error: Cannot access refs during render
4 | return new PerformanceObserver(() => {
| ^^^^^^^^^^^^^^ Passing a ref to a function may read its value during render
```
Both constructors only *retain* the callback and invoke it asynchronously
after the observation fires — never during render — so reading or writing a
ref inside the callback is safe. `IntersectionObserver` (react#37764) and
`ResizeObserver` / `MutationObserver` (react#37895) are already modelled this way;
these two were simply missed, so they fell back to the unknown-constructor
default, which freezes the callback and trips ref validation.
Modelled with the same signature shape as the existing observers:
- `PerformanceObserver(callback)` -> `[Freeze]`
- `ReportingObserver(callback, options)` -> `[Freeze, Read]`
The second argument of `ReportingObserver` is a real constructor argument
(unlike the observers where options belong to `observe()`), so it is read
rather than retained.
Fixtures: two `allow-ref-access-in-*-observer` fixtures proving the callback
may touch a ref, plus two negative fixtures proving `Freeze` still applies —
mutating a `useMemo` value inside the callback keeps reporting
"`entries` cannot be modified".
Both the TypeScript and Rust backends are updated so the two stay in parity.
javache
approved these changes
Oct 26, 2026
github-actions Bot
pushed a commit
that referenced
this pull request
Oct 26, 2026
…r callbacks (#37996) ## Summary `new PerformanceObserver(cb)` and `new ReportingObserver(cb)` are compiled with a false positive when the callback touches a ref: ``` Error: Cannot access refs during render 4 | return new PerformanceObserver(() => { | ^^^^^^^^^^^^^^ Passing a ref to a function may read its value during render ``` Both constructors only *retain* the callback and invoke it asynchronously once the observation fires — never during render — so reading (or writing) a ref from the callback is safe. `IntersectionObserver` (#37764) and `ResizeObserver` / `MutationObserver` (#37895) are already modelled exactly this way; these two were just missed and fall back to the unknown-constructor default, whose `Freeze` on the callback trips `ValidateNoRefAccessInRender`. This is the same class of fix as #37895, which is now on `main`. ## Fix Add both to `TYPED_GLOBALS` (TypeScript) and `build_typed_globals` (Rust) using the same signature shape as the existing observers: | Global | Constructor signature | | --- | --- | | `PerformanceObserver` | `(callback)` → `[Freeze]` | | `ReportingObserver` | `(callback, options)` → `[Freeze, Read]` | `ReportingObserver`'s second argument is a genuine constructor argument (unlike the observers whose options belong to `observe()`), so it is read rather than retained. Both backends are updated so TS and Rust stay in parity. ## Tests Four new fixtures: - `allow-ref-access-in-performance-observer.ts` - `allow-ref-access-in-reporting-observer.ts` Both fail before the change with `Cannot access refs during render` and compile to memoized constructors after it. - `error.invalid-mutate-hook-value-in-performance-observer.js` - `error.invalid-mutate-hook-value-in-reporting-observer.js` Negative controls proving `Freeze` is not weakened: mutating a `useMemo` value inside the callback still reports `` `entries` cannot be modified ``. Validation run locally: - Full fixture suite (1840 fixtures): `25 changed / 3 failed` — identical to the pre-existing baseline (`fbt/*`, `debugger*`), no new diffs. - `tsc --noEmit`, `eslint src`, `prettier check-changed` all clean. - Rust side mirrors the TS change 1:1 but was not compiled locally (no toolchain in this environment); relying on the `babel-plugin-react-compiler-rust` CI job. Co-authored-by: zhoubiao188 <[email protected]> DiffTrain build for [c050dbd](c050dbd)
github-actions Bot
pushed a commit
that referenced
this pull request
Oct 26, 2026
…r callbacks (#37996) ## Summary `new PerformanceObserver(cb)` and `new ReportingObserver(cb)` are compiled with a false positive when the callback touches a ref: ``` Error: Cannot access refs during render 4 | return new PerformanceObserver(() => { | ^^^^^^^^^^^^^^ Passing a ref to a function may read its value during render ``` Both constructors only *retain* the callback and invoke it asynchronously once the observation fires — never during render — so reading (or writing) a ref from the callback is safe. `IntersectionObserver` (#37764) and `ResizeObserver` / `MutationObserver` (#37895) are already modelled exactly this way; these two were just missed and fall back to the unknown-constructor default, whose `Freeze` on the callback trips `ValidateNoRefAccessInRender`. This is the same class of fix as #37895, which is now on `main`. ## Fix Add both to `TYPED_GLOBALS` (TypeScript) and `build_typed_globals` (Rust) using the same signature shape as the existing observers: | Global | Constructor signature | | --- | --- | | `PerformanceObserver` | `(callback)` → `[Freeze]` | | `ReportingObserver` | `(callback, options)` → `[Freeze, Read]` | `ReportingObserver`'s second argument is a genuine constructor argument (unlike the observers whose options belong to `observe()`), so it is read rather than retained. Both backends are updated so TS and Rust stay in parity. ## Tests Four new fixtures: - `allow-ref-access-in-performance-observer.ts` - `allow-ref-access-in-reporting-observer.ts` Both fail before the change with `Cannot access refs during render` and compile to memoized constructors after it. - `error.invalid-mutate-hook-value-in-performance-observer.js` - `error.invalid-mutate-hook-value-in-reporting-observer.js` Negative controls proving `Freeze` is not weakened: mutating a `useMemo` value inside the callback still reports `` `entries` cannot be modified ``. Validation run locally: - Full fixture suite (1840 fixtures): `25 changed / 3 failed` — identical to the pre-existing baseline (`fbt/*`, `debugger*`), no new diffs. - `tsc --noEmit`, `eslint src`, `prettier check-changed` all clean. - Rust side mirrors the TS change 1:1 but was not compiled locally (no toolchain in this environment); relying on the `babel-plugin-react-compiler-rust` CI job. Co-authored-by: zhoubiao188 <[email protected]> DiffTrain build for [c050dbd](c050dbd)
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.
Summary
new PerformanceObserver(cb)andnew ReportingObserver(cb)are compiled with a false positive when the callback touches a ref:Both constructors only retain the callback and invoke it asynchronously once the observation fires — never during render — so reading (or writing) a ref from the callback is safe.
IntersectionObserver(#37764) andResizeObserver/MutationObserver(#37895) are already modelled exactly this way; these two were just missed and fall back to the unknown-constructor default, whoseFreezeon the callback tripsValidateNoRefAccessInRender.This is the same class of fix as #37895, which is now on
main.Fix
Add both to
TYPED_GLOBALS(TypeScript) andbuild_typed_globals(Rust) using the same signature shape as the existing observers:PerformanceObserver(callback)→[Freeze]ReportingObserver(callback, options)→[Freeze, Read]ReportingObserver's second argument is a genuine constructor argument (unlike the observers whose options belong toobserve()), so it is read rather than retained.Both backends are updated so TS and Rust stay in parity.
Tests
Four new fixtures:
allow-ref-access-in-performance-observer.tsallow-ref-access-in-reporting-observer.tsBoth fail before the change with
Cannot access refs during renderand compile to memoized constructors after it.error.invalid-mutate-hook-value-in-performance-observer.jserror.invalid-mutate-hook-value-in-reporting-observer.jsNegative controls proving
Freezeis not weakened: mutating auseMemovalue inside the callback still reports`entries` cannot be modified.Validation run locally:
25 changed / 3 failed— identical to the pre-existing baseline (fbt/*,debugger*), no new diffs.tsc --noEmit,eslint src,prettier check-changedall clean.babel-plugin-react-compiler-rustCI job.