Skip to content

Detect cyclic values in dynamicFromValue in debug builds - #60297

Open
dalves wants to merge 1 commit into
react:mainfrom
dalves:export-D132584875
Open

dalves wants to merge 1 commit into
react:mainfrom
dalves:export-D132584875

Conversation

@dalves

@dalves dalves commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Summary:
A cyclic JavaScript value can leave prop conversion stuck while memory grows. Native debug builds now detect the cycle and throw a JSError, returning control to JavaScript instead of hanging. Release-build conversion is unchanged.

How the hang happens

  1. Fabric converts a component's raw props through RawProps::toDynamic and jsi::dynamicFromValue.
  2. The converter walks objects with an explicit stack. A cycle keeps expanding the same path instead of finishing.
  3. The call never returns, so the JavaScript thread remains stuck while the partial output and pending handles grow.

What changes
When NDEBUG is not defined, the converter uses Brent's algorithm to detect cycles without a visited set. Cyclic values throw, while acyclic values—including objects shared across paths—continue to convert normally.

When NDEBUG is defined, all cycle-detection code is excluded: depth fields and arguments, ancestor storage, comparisons and updates. The release converter retains its original code and behavior, including the existing hang on cyclic values. This is a native-build guard, not the JavaScript __DEV__ flag.

Changelog:
[General][Fixed] - Detect cyclic JavaScript values during debug-build conversion to folly::dynamic

Reviewed By: javache

Differential Revision: D132584875

Summary:
A cyclic JavaScript value can leave prop conversion stuck while memory grows. Native debug builds now detect the cycle and throw a `JSError`, returning control to JavaScript instead of hanging. Release-build conversion is unchanged.

**How the hang happens**
1. Fabric converts a component's raw props through `RawProps::toDynamic` and `jsi::dynamicFromValue`.
2. The converter walks objects with an explicit stack. A cycle keeps expanding the same path instead of finishing.
3. The call never returns, so the JavaScript thread remains stuck while the partial output and pending handles grow.

**What changes**
When `NDEBUG` is not defined, the converter uses Brent's algorithm to detect cycles without a visited set. Cyclic values throw, while acyclic values—including objects shared across paths—continue to convert normally.

When `NDEBUG` is defined, all cycle-detection code is excluded: depth fields and arguments, ancestor storage, comparisons and updates. The release converter retains its original code and behavior, including the existing hang on cyclic values. This is a native-build guard, not the JavaScript `__DEV__` flag.

Changelog:
[General][Fixed] - Detect cyclic JavaScript values during debug-build conversion to folly::dynamic

Reviewed By: javache

Differential Revision: D132584875
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Nov 25, 2026
@meta-codesync

meta-codesync Bot commented Nov 25, 2026

Copy link
Copy Markdown

@dalves has exported this pull request. If you are a Meta employee, you can view the originating Diff in D132584875.

@javache javache left a comment

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.

Review automatically exported from Phabricator review in Meta.

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

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. meta-exported p: Facebook Partner: Facebook Partner

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants