Skip to content

Improve cyclic thenable detection in ReactFlightReplyServer #35368

@cvan20191

Description

@cvan20191

React version:
19.0.3

Steps To Reproduce

  1. In ReactFlightReplyServer.js, construct a chain of ReactPromise instances where each .value points to the next, and the last one points back to the first (e.g. A -> B -> C -> A), creating a multi‑node cycle.
  2. Trigger the cycle detection logic by resolving a chunk whose value is the head of this cycle and observing how the while (inspectedValue instanceof ReactPromise) loop behaves.

The current behavior

The cycle detection added in commit bd4289b only checks for a direct
self‑reference:

let cycleProtection = 0;
while (inspectedValue instanceof ReactPromise) {
  cycleProtection++;
  if (inspectedValue === chunk || cycleProtection > 1000) {
    if (typeof reject === 'function') {
      reject(new Error('Cannot have cyclic thenables.'));
    }
  }
}

inspectedValue === chunk catches a simple self‑loop, but a longer cycle like A -> B -> C -> A will continue iterating until the cycleProtection counter hits the $1000$ limit instead of detecting the cycle when a node is revisited.

The expected behavior

Any cyclic thenable chain, including multi‑node cycles (not just direct
self‑references), should be rejected as soon as a ReactPromise is encountered more than once in the traversal, rather than relying on the $1000$ cycleProtection cap to eventually bail out.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Status: UnconfirmedA potential issue that we haven't yet confirmed as a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions