Skip to content

[compiler] Allow ref access in PerformanceObserver / ReportingObserver callbacks - #37996

Merged
javache merged 1 commit into
react:mainfrom
zhoubiao188:fix-performance-observer-globals
Oct 26, 2026
Merged

javache merged 1 commit into
react:mainfrom
zhoubiao188:fix-performance-observer-globals

Conversation

@zhoubiao188

Copy link
Copy Markdown
Contributor

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.

…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.
@meta-cla meta-cla Bot added the CLA Signed label Oct 25, 2026
@javache
javache merged commit c050dbd into react:main Oct 26, 2026
25 checks passed
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants