Skip to content

Conversation

@rickhanlonii
Copy link
Member

@rickhanlonii rickhanlonii commented Jan 15, 2026

Flights tests are failing locally and in CI non-deterministically because we're not disabling async hooks after tests, and GC can clear WeakRefs non-deterministically.

This PR fixes the issue by adding an afterEach to disable installed hooks, and normalizing the value to value: {value: undefined}} when snapshotting.

Claude found the fix, here's the explanation:

Summary of Both Flaky Test Issues

Issue 1: ReactFlightDOMEdge-test.js - Cross-Test Async Hook Pollution

Root Cause:

  • When tests run with --runInBand, they execute sequentially in the same process
  • ReactFlightAsyncDebugInfo-test.js loads the Node server which creates async hooks via createHook().enable() to track async operations
  • ReactFlightDOMEdge-test.js loads the Edge server which uses a Noop debug config (no async hooks created) - The async hooks from the Node server persisted across tests because:
    • The Edge server doesn't create new hooks, so the cleanup code in setupTests.js never triggered
    • Old hooks kept firing their callbacks, calling performance.now() on the Edge test's mocks
    • This caused the Edge test's time values to be 232, 259, 260 instead of expected 12, 13, 14

The Fix: scripts/jest/setupTests.js

afterEach(() => {
  if (installedHook) {
    installedHook.disable();
    installedHook = null;
  }
});

This ensures async hooks are disabled after each test, preventing cross-test pollution.

Issue 2: ReactFlightAsyncDebugInfo-test.js - WeakRef Garbage Collection

Root Cause:

  • React's async debug info uses WeakRef to hold references to promises without preventing garbage collection
  • When serializing debug info, the code calls weakRef.deref() to get the promise
  • If GC has run: deref() returns undefined, and the server doesn't include the value field at all
  • If GC hasn't run: deref() returns the promise, and the server includes value: { value: undefined }
  • Since GC timing is non-deterministic (depends on memory pressure, CPU, etc.), tests would randomly pass or fail

The Fix: packages/internal-test-utils/debugInfo.js

// In normalizeIOInfo()
} else if ('value' in ioInfo) {
  // If value exists but is undefined (WeakRef was GC'd)
  copy.value = { value: undefined };
} else if (ioInfo.name && ioInfo.name !== 'rsc stream') {
  // If value is completely missing (server didn't send it due to GC)
  copy.value = { value: undefined };
}

This normalizes the output so snapshots always see a consistent structure regardless of GC timing.


★ Insight ─────────────────────────────────────
Two Patterns for Flaky Tests:

  1. State Leakage Between Tests: When tests run sequentially, global state (like async hooks, timers, or singletons) can leak. Solution: Clean up thoroughly in afterEach.
  2. Non-Deterministic System Behavior: When tests depend on unpredictable behavior (GC timing, network timing, file system operations), normalize the data in test utilities rather than trying to control the underlying system.
    ─────────────────────────────────────────────────

Both issues shared a common theme: non-deterministic behavior in sequential test execution. The first was about test isolation failure, the second was about normalizing inherently non-deterministic runtime behavior.

@meta-cla meta-cla bot added the CLA Signed label Jan 15, 2026
@rickhanlonii rickhanlonii changed the title Fix flaky time values in ReactFlight-test.js [tests] Fix flaky time values in ReactFlight-test.js Jan 15, 2026
@github-actions github-actions bot added the React Core Team Opened by a member of the React Core Team label Jan 15, 2026
@react-sizebot
Copy link

react-sizebot commented Jan 15, 2026

Comparing: d87298a...e01bf9b

Critical size changes

Includes critical production bundles, as well as any change greater than 2%:

Name +/- Base Current +/- gzip Base gzip Current gzip
oss-stable/react-dom/cjs/react-dom.production.js = 6.84 kB 6.84 kB = 1.88 kB 1.88 kB
oss-stable/react-dom/cjs/react-dom-client.production.js = 608.48 kB 608.03 kB +0.01% 107.60 kB 107.61 kB
oss-experimental/react-dom/cjs/react-dom.production.js = 6.84 kB 6.84 kB = 1.88 kB 1.88 kB
oss-experimental/react-dom/cjs/react-dom-client.production.js = 670.95 kB 667.26 kB = 118.02 kB 117.51 kB
facebook-www/ReactDOM-prod.classic.js = 693.87 kB 693.38 kB +0.02% 121.97 kB 122.00 kB
facebook-www/ReactDOM-prod.modern.js = 684.25 kB 683.76 kB +0.02% 120.37 kB 120.40 kB
oss-stable/react/cjs/react.react-server.production.js = 13.83 kB 13.54 kB = 3.76 kB 3.74 kB
oss-stable-semver/react/cjs/react.react-server.production.js = 13.81 kB 13.51 kB = 3.74 kB 3.71 kB

Significant size changes

Includes any change greater than 0.2%:

Expand to show
Name +/- Base Current +/- gzip Base gzip Current gzip
oss-experimental/react-debug-tools/cjs/react-debug-tools.development.js = 33.37 kB 33.29 kB = 5.93 kB 5.92 kB
oss-stable-semver/react-debug-tools/cjs/react-debug-tools.development.js = 33.37 kB 33.29 kB = 5.93 kB 5.92 kB
oss-stable/react-debug-tools/cjs/react-debug-tools.development.js = 33.37 kB 33.29 kB = 5.93 kB 5.92 kB
oss-experimental/react-debug-tools/cjs/react-debug-tools.production.js = 29.78 kB 29.70 kB = 5.81 kB 5.80 kB
oss-stable-semver/react-debug-tools/cjs/react-debug-tools.production.js = 29.78 kB 29.70 kB = 5.81 kB 5.80 kB
oss-stable/react-debug-tools/cjs/react-debug-tools.production.js = 29.78 kB 29.70 kB = 5.81 kB 5.80 kB
oss-experimental/react-art/cjs/react-art.production.js = 358.82 kB 357.47 kB = 60.39 kB 60.22 kB
oss-experimental/react-dom/cjs/react-dom-profiling.development.js = 1,262.88 kB 1,257.62 kB = 209.88 kB 208.89 kB
oss-experimental/react-dom/cjs/react-dom-unstable_testing.development.js = 1,262.87 kB 1,257.62 kB = 210.68 kB 209.73 kB
oss-experimental/react-dom/cjs/react-dom-client.development.js = 1,246.33 kB 1,241.07 kB = 207.01 kB 206.04 kB
oss-experimental/react-dom/cjs/react-dom-unstable_testing.production.js = 685.36 kB 681.67 kB = 121.56 kB 121.07 kB
oss-experimental/react-reconciler/cjs/react-reconciler.development.js = 862.57 kB 857.88 kB = 134.34 kB 133.50 kB
oss-experimental/react-dom/cjs/react-dom-client.production.js = 670.95 kB 667.26 kB = 118.02 kB 117.51 kB
oss-experimental/react-dom/cjs/react-dom-profiling.profiling.js = 750.23 kB 745.88 kB = 129.58 kB 128.84 kB
oss-experimental/react-art/cjs/react-art.development.js = 734.08 kB 729.74 kB = 115.50 kB 114.68 kB
facebook-react-native/react/cjs/React-dev.js = 52.93 kB 52.61 kB = 11.64 kB 11.61 kB
oss-experimental/react-reconciler/cjs/react-reconciler.production.js = 491.86 kB 488.70 kB = 78.14 kB 77.71 kB
oss-experimental/react-reconciler/cjs/react-reconciler.profiling.js = 568.03 kB 564.15 kB = 88.36 kB 87.75 kB
facebook-www/React-profiling.classic.js = 20.87 kB 20.57 kB = 5.28 kB 5.25 kB
facebook-www/React-profiling.modern.js = 20.87 kB 20.57 kB = 5.28 kB 5.25 kB
facebook-www/React-prod.classic.js = 20.44 kB 20.14 kB = 5.20 kB 5.17 kB
facebook-www/React-prod.modern.js = 20.44 kB 20.14 kB = 5.20 kB 5.17 kB
facebook-react-native/react/cjs/React-profiling.js = 19.96 kB 19.66 kB = 5.06 kB 5.03 kB
oss-experimental/react/cjs/react.production.js = 19.76 kB 19.46 kB = 4.95 kB 4.92 kB
facebook-react-native/react/cjs/React-prod.js = 19.53 kB 19.23 kB = 4.98 kB 4.95 kB
oss-experimental/react/cjs/react.react-server.production.js = 19.41 kB 19.11 kB = 5.03 kB 5.00 kB
oss-stable/react/cjs/react.production.js = 18.07 kB 17.77 kB = 4.62 kB 4.59 kB
oss-stable-semver/react/cjs/react.production.js = 18.04 kB 17.74 kB = 4.60 kB 4.57 kB
oss-stable/react/cjs/react.react-server.production.js = 13.83 kB 13.54 kB = 3.76 kB 3.74 kB
oss-stable-semver/react/cjs/react.react-server.production.js = 13.81 kB 13.51 kB = 3.74 kB 3.71 kB

Generated by 🚫 dangerJS against e01bf9b

Copy link
Collaborator

@unstubbable unstubbable left a comment

Choose a reason for hiding this comment

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

As far as I understand these tests do care about the debug info entries with the mocked time (introduced in #31716). For example, you want to assert that they are sequential and not overlapping, which is why there are also comments like Clamped to the start. So they are roughly asserting how this would lay out in the performance tracks. I've also never seen them flake. Under which circumstances did that happen to you?

@rickhanlonii
Copy link
Member Author

@unstubbable I've seen CI fail quite a few times from this lately on my PRs, and on main.

For example, this commit from main failed from this in ReactFlightDOMEdge.

This test failed on my latest PR also from ReactFlightDOMEdge.

But aside from CI, I can basically repro locally any time I run yarn test, which makes it really annoying to test my changes before pushing.

@rickhanlonii
Copy link
Member Author

rickhanlonii commented Jan 18, 2026

The root cause seems to be that when those tests are run alongside other tests, the timing changes due to subtle delays in execution caused by the other test runs.

@rickhanlonii rickhanlonii force-pushed the fix-reactflight-test-flaky-time-values branch from a738e51 to e01bf9b Compare January 18, 2026 19:17
@rickhanlonii rickhanlonii changed the title [tests] Fix flaky time values in ReactFlight-test.js [tests] Fix flaky flight tests Jan 18, 2026
@rickhanlonii
Copy link
Member Author

@unstubbable I found the root cause, see the new description and fixes - none of the test snapshots need to change now.

Copy link
Collaborator

@unstubbable unstubbable left a comment

Choose a reason for hiding this comment

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

Makes sense!

@rickhanlonii rickhanlonii merged commit 195fd22 into facebook:main Jan 18, 2026
234 checks passed
@rickhanlonii rickhanlonii deleted the fix-reactflight-test-flaky-time-values branch January 18, 2026 20:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed React Core Team Opened by a member of the React Core Team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants