Skip to content

[DOM] Guard window.event reads in scheduler event tracking - #37115

Open
SidiEyel wants to merge 2 commits into
react:mainfrom
SidiEyel:guard-scheduler-window-event
Open

SidiEyel wants to merge 2 commits into
react:mainfrom
SidiEyel:guard-scheduler-window-event

Conversation

@SidiEyel

@SidiEyel SidiEyel commented Jul 25, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #37100.

The DEV-only Performance Tracks instrumentation added in 19.2.0 reads window.event unconditionally in trackSchedulerEvent(), resolveEventType(), and resolveEventTimeStamp(). trackSchedulerEvent() runs at the entry of Scheduler-driven tasks (performWorkOnRootViaSchedulerTask, processRootScheduleInImmediateTask, and the passive-effects flush scheduled by commitRoot). These tasks live on deferred setImmediate ticks and can legitimately fire after a per-test-file DOM environment (jsdom / happy-dom under vitest or jest) has been torn down. When that happens the task throws ReferenceError: window is not defined before doing any work, which fails entire CI suites with an unhandled error attributed to the next, innocent test file.

This guards the three reads with typeof window !== 'undefined', matching the bundle's existing convention for window accesses that may run outside a browser DOM (e.g. canUseDOM, the window.ErrorEvent detection in the error-reporting path). When window is gone the attribution resolves to undefined, i.e. "no event", which is accurate. shouldAttemptEagerTransition() and resolveUpdatePriority() are left untouched: they only run while an update is being dispatched, so window is alive by construction there.

How did you test this change?

  • Added a regression test (ReactDOMWindowTeardown-test.js, node environment + manually constructed JSDOM mirroring the issue's repro). It fails on main with ReferenceError: window is not defined from performWorkOnRootViaSchedulerTask and passes with this change.
  • yarn test ReactPerformanceTrack ReactDOMFiberAsync ReactDOMNativeEventHeuristic — all pass.
  • yarn linc, yarn flow dom-node, yarn prettier — clean.
  • Ran the repro script from Bug: [DEV] "ReferenceError: window is not defined" from Scheduler tasks after test DOM environment teardown — regression in 19.2.0 #37100 verbatim: against published react-dom@19.2.8 it crashes as reported; against a locally built NODE_DEV bundle with this change it prints done, no crash and exits 0.

@meta-cla meta-cla Bot added the CLA Signed label Jul 25, 2026
@github-actions

github-actions Bot commented Sep 7, 2026

Copy link
Copy Markdown

The build for this commit did not complete, so there is no size report. See the workflow run for details.

Generated by sizebot against a0b0880

@zhoubiao188

Copy link
Copy Markdown
Contributor

Two reads in the same family are left unguarded, and both are on the exact path this PR targets (a Scheduler task that fires after the environment is gone):

  • ReactFiberConfigDOM.shouldAttemptEagerTransition() (:785) reads window.event. It is called from processRootScheduleInMicrotask(), which is entered through processRootScheduleInImmediateTask() — i.e. from a setImmediate task, the same class of task as trackSchedulerEvent(). It only runs when currentEventTransitionLane !== NoLane (a transition was scheduled by that task), but then it throws the same ReferenceError.
  • ReactDOMUpdatePriority.resolveUpdatePriority() (packages/react-dom-bindings/src/client/ReactDOMUpdatePriority.js:40) reads window.event when no explicit priority is installed. It is called unconditionally from requestUpdateLane() (ReactFiberWorkLoop.js:862), so any setState dispatched by a leftover task — e.g. from a passive effect — reaches it.

Guarding only the three functions in this diff therefore leaves the crash reachable through the update path.

Also: this was re-reported as #38161 (it points at the unguarded reads on main as of d71b537), so you may want to reference it too so it gets closed along with this PR.

Separately, and unrelated to the fix: the diff also rewrites packages/shared/ReactVersion.js to export default '19.3.0-canary-b685b40d-20260724' and deletes the comment block above it. That looks like a build artifact left in the working tree; it should be dropped from the branch before merge.

// at runtime. We should instead inject the version number as part of the build
// process, and use the ReactVersions.js module as the single source of truth.
export default '19.3.0';
export default '19.3.0-canary-b685b40d-20260724';

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.

You might want to revert this file so that you will not have merge conflicts

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: [DEV] "ReferenceError: window is not defined" from Scheduler tasks after test DOM environment teardown — regression in 19.2.0

3 participants