react icon indicating copy to clipboard operation
react copied to clipboard

Bug: [DEV] "ReferenceError: window is not defined" from Scheduler tasks after DOM environment teardown — still present in 19.3.1

Open tk1024 opened this issue 3 weeks ago • 1 comments

This is a follow-up to #37100, which the stale bot closed for inactivity and which asked for a new issue if the problem is still present in the latest release. It is: the crash reproduces on 19.3.1, and the unguarded read is still on main.

The DEV build of react-dom reads window.event at the entry of Scheduler tasks. Those tasks run on setImmediate and can fire after a per-test-file DOM environment has been torn down. The task then throws, and the test run fails with an unhandled error even though every test passed.

React version: 19.2.0 through 19.3.1 (19.1.x is not affected, per #37100)

Steps To Reproduce

  1. npm i [email protected] [email protected] [email protected]
  2. Save the script below as repro.cjs and run node repro.cjs (verified on Node v24.10.0)

Link to code example:

The crash is deterministic and needs no test runner. jsdom stands in for the DOM environment that vitest and jest create and tear down per test file.

const { JSDOM } = require("jsdom");
const React = require("react");

const dom = new JSDOM('<div id="root"></div>');
globalThis.window = dom.window;
globalThis.document = dom.window.document;

const { createRoot } = require("react-dom/client");

const root = createRoot(document.getElementById("root"));
root.render(React.createElement("div", null, "hello"));

// Simulate a test runner tearing down the DOM environment between files
// while the scheduled task is still queued.
delete globalThis.window;
delete globalThis.document;

setTimeout(() => console.log("done, no crash"), 50);

The current behavior

node_modules/react-dom/cjs/react-dom-client.development.js:20644
      schedulerEvent = window.event;
      ^
ReferenceError: window is not defined
    at performWorkOnRootViaSchedulerTask (node_modules/react-dom/cjs/react-dom-client.development.js:20644:7)
    at Immediate.performWorkUntilDeadline (node_modules/scheduler/cjs/scheduler.development.js:45:48)
    at process.processImmediate (node:internal/timers:505:21)

Removing the two delete lines prints done, no crash and exits 0.

The reads are still unguarded on main at d71b537: ReactFiberConfigDOM.js#L809-L819.

The expected behavior

Work scheduled before the DOM environment went away should not crash the process.

New information since #37100: guarding only the window.event reads may not be enough. We carry a local patch with those guards in a large vitest + happy-dom suite, and on CI the same kind of leftover task later failed deeper in the commit:

TypeError: Right-hand side of 'instanceof' is not an object
  getActiveElementDeep (react-dom-client.development.js)
  commitBeforeMutationEffects (react-dom-client.development.js)
  ...
  Immediate.performWorkUntilDeadline (scheduler.development.js)

That was observed on 19.2.7 and is timing-dependent, so I don't have a minimal repro for it. Our patch now also returns early at the entry of the Scheduler task when window or document is undefined, so that the leftover task does not run at all.

#37115 proposes guarding the reads and is still open.

tk1024 avatar Nov 08 '26 18:11 tk1024

I'm going to send a PR for this. #37115 has had no follow-up since the review on Nov 9, so my PR will also guard the two reads that were pointed out there (shouldAttemptEagerTransition and resolveUpdatePriority) and add a test for each path.

tk1024 avatar Nov 27 '26 15:11 tk1024