Skip to content

[Flight] Allow cyclic references to be serialized when unwrapping lazy elements#312

Closed
everettbu wants to merge 2 commits into
mainfrom
fix-cycle-bug
Closed

[Flight] Allow cyclic references to be serialized when unwrapping lazy elements#312
everettbu wants to merge 2 commits into
mainfrom
fix-cycle-bug

Conversation

@everettbu

@everettbu everettbu commented Jan 8, 2026

Copy link
Copy Markdown

Mirror of facebook/react#35471
Original author: unstubbable


When renderModelDestructive unwraps a lazy element and subsequently calls renderModelDestructive again with the resolved model, we should preserve the parent connection so that cyclic references can be serialized properly. This can occur in an advanced scenario where the result from the Flight Client is serialized again with the Flight Server, e.g. for slicing a precomputed payload into multiple parts.

Note: The added test only fails when run with --prod. In dev mode, the component info outlining prevents the issue from occurring.

Fails with:

```
TypeError: Converting circular structure to JSON
 --> starting at object with constructor 'Object'
 --- property 'self' closes the circle
```
@everettbu everettbu added CLA Signed React Core Team Opened by a member of the React Core Team labels Jan 8, 2026

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Greptile Overview

Greptile Summary

Fixed cyclic reference serialization when unwrapping lazy elements by preserving the parent connection.

  • Changed renderModelDestructive call in REACT_LAZY_TYPE case to pass parent and parentPropertyName instead of emptyRoot and empty string
  • This enables the writtenObjects WeakMap to properly track object references through the parent chain for cycle detection
  • Added comprehensive test that verifies cyclic objects survive two rounds of Flight serialization/deserialization
  • The fix specifically addresses an advanced scenario where Flight Client results are re-serialized with Flight Server (e.g., for payload slicing)

Confidence Score: 5/5

  • This PR is safe to merge with no issues found
  • The change is minimal, targeted, and logically correct - it preserves the parent chain needed for cycle detection when unwrapping lazy elements. The implementation aligns with how other parts of the codebase handle object tracking via writtenObjects. The test thoroughly validates the fix by testing the exact failure scenario (cyclic references through two serialization rounds).
  • No files require special attention

Important Files Changed

File Analysis

Filename Score Overview
packages/react-server/src/ReactFlightServer.js 5/5 Correctly preserves parent chain when unwrapping lazy elements to enable cyclic reference serialization
packages/react-server-dom-webpack/src/tests/ReactFlightDOMEdge-test.js 5/5 Comprehensive test verifying cyclic references survive two rounds of serialization/deserialization

@everettbu

Copy link
Copy Markdown
Author

Upstream PR was closed or merged. Code is synced via branch mirror.

@everettbu everettbu closed this Jan 16, 2026
@everettbu everettbu deleted the fix-cycle-bug branch January 16, 2026 18:13
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.

2 participants