Skip to content

Bug: a portal that mounts in a transition with a not-yet-settled node as its own child throws when it is replayed (stateNode was reset to null), and the unwind then pops the fiber stack twice: a context's default value becomes null for the rest of the page's life #38389

Backlog

Description

@HYAZEUS

React version

19.3.1 (react and react-dom from npm, no framework), development and production builds.

Also reproduced, same result, on 19.0.0, 19.1.10, 19.2.9 and on 19.3.0-canary-89d7b2b4-20261123.
The code path is unchanged on main at 92bb0678c5 (24 November 2026).

Steps to reproduce

  1. Save the page below as index.html and open it in a browser (it loads React from esm.sh; nothing to build).
  2. Press "Mount the portal".

The whole reproduction (43 lines):

<!doctype html>
<meta charset="utf-8" />
<title>A portal whose own child settles a moment after it starts to mount</title>
<div id="root"></div>
<div id="portal"></div>
<pre id="log"></pre>
<script type="module">
  import { createContext, createElement as h, Fragment, startTransition, useContext, useState, version } from "https://esm.sh/[email protected]?dev";
  import { createPortal } from "https://esm.sh/[email protected]?dev";
  import { createRoot } from "https://esm.sh/[email protected]/client?dev";

  const log = (line) => (document.getElementById("log").textContent += line + "\n");
  const Theme = createContext("default");

  // Not under any <Theme.Provider>: must always read "default".
  function OutsideTheProviders() {
    return h("p", { id: "outside" }, "Theme outside the providers: " + String(useContext(Theme)));
  }

  function App() {
    const [late, setLate] = useState(null);
    // A node that is pending when React first renders it and settles one microtask
    // later. (A Server Components payload hands client components such nodes all
    // the time: a lazy node whose row arrives a moment after its parent's.)
    const mount = () => startTransition(() => setLate(Promise.resolve(h("b", null, "portal content"))));
    return h(
      Fragment,
      null,
      h("button", { id: "mount", onClick: mount }, "Mount the portal"),
      h(
        Theme.Provider,
        { value: "outer" },
        h(Theme.Provider, { value: "inner" }, late && createPortal(late, document.getElementById("portal"))),
      ),
      h(OutsideTheProviders),
    );
  }

  log("react " + version);
  createRoot(document.getElementById("root"), {
    onRecoverableError: (error) => log("onRecoverableError: " + error.message + "\n  cause: " + (error.cause && error.cause.stack)),
  }).render(h(App));
</script>

Remove ?dev from the three imports for the production build. The same component written in JSX in an
ordinary bundled app (esbuild, react and react-dom from npm) behaves identically; that is what the table
below was measured on.

The current behavior

Before the press the page reads Theme outside the providers: default. After it:

  • onRecoverableError is called with "There was an error during concurrent rendering but React was able to
    recover by instead synchronously rendering the entire root." (error Fix error in test logging #520 in production), whose cause is
    TypeError: Cannot read properties of null (reading 'containerInfo');
  • the development build also logs Unexpected Fiber popped. (7 times here) and Unexpected pop. (3 times);
  • the portal's content does appear, but the page now reads Theme outside the providers: null: a consumer
    that is under no provider no longer gets the context's default value. It stays null on every later
    render. In a larger tree the wrong values go on to break later renders in other ways (for example
    Cannot read properties of undefined (reading 'containerTagInScope') from isHostHoistableType, and
    Cannot read properties of undefined (reading 'signal') from retainCache in a later passive-effects
    commit), and a boundary that was waiting for data stays on its fallback.

Stack of the cause (development build, 19.3.1):

TypeError: Cannot read properties of null (reading 'containerInfo')
    at beginWork
    at replayBeginWork
    at runWithFiberInDEV
    at replaySuspendedUnitOfWork
    at renderRootConcurrent
    at performWorkOnRoot
    at performWorkOnRootViaSchedulerTask
    at MessagePort.performWorkUntilDeadline

Measured in headless Chromium, 10 page loads per cell, one press per load:

react build what the portal is given mounted in a transition TypeError (containerInfo) context default lost Unexpected Fiber popped.
19.3.1 development the late node itself yes 10/10 10/10 10/10
19.3.1 production the late node itself yes 10/10 10/10 n/a
19.3.0-canary-89d7b2b4-20261123 development the late node itself yes 10/10 10/10 10/10
19.3.0-canary-89d7b2b4-20261123 production the late node itself yes 10/10 10/10 n/a
19.2.9 development / production the late node itself yes 10/10 10/10 10/10 (dev)
19.1.10 development / production the late node itself yes 10/10 10/10 10/10 (dev)
19.0.0 development / production the late node itself yes 10/10 10/10 10/10 (dev)
every version above both <Wrapper>{late}</Wrapper> (a component that returns its children) yes 0/10 0/10 0/10
every version above both <span>{late}</span> yes 0/10 0/10 0/10
every version above both the late node itself no (setLate outside startTransition) 0/10 0/10 0/10

The portal's content was shown in every load of every cell.

The expected behavior

The portal mounts when its child settles, as it does when the same node is one level further down
(createPortal(<Wrapper>{late}</Wrapper>, target)): no error, nothing logged, and
Theme outside the providers: default before and after.

What happens, as far as I can read it

  1. beginWork for the mounting HostPortal fiber pushes the host container and reconciles its children.
    The child is a thenable that is still pending, so the reconciler suspends (SuspenseException) with the
    portal fiber as the suspended unit of work.
  2. The thenable settles before the work loop looks again (SuspendedOnImmediate ->
    SuspendedAndReadyToContinue), so replaySuspendedUnitOfWork runs. replayBeginWork has no case for
    HostPortal, so it takes the default branch (ReactFiberWorkLoop.js):
    unwindInterruptedWork(current, unitOfWork, ...), then resetWorkInProgress(unitOfWork, ...), then
    beginWork(...) again.
  3. resetWorkInProgress (ReactFiber.js) sets workInProgress.stateNode = null when current === null.
    A portal fiber's stateNode ({ containerInfo, pendingChildren, implementation }) is only ever set in
    createFiberFromPortal, so the replayed fiber has lost it.
  4. updatePortalComponent (ReactFiberBeginWork.js) starts with
    pushHostContainer(workInProgress, workInProgress.stateNode.containerInfo) and throws the TypeError
    before pushing anything.
  5. The error is handled as a render error: throwAndUnwindWorkLoop -> unwindWork, whose HostPortal
    case calls popHostContainer(workInProgress) again. Step 2 had already popped the portal's three
    entries, so these three pops take entries that belong to the portal's ancestors. Every pop after that
    restores a value into the wrong cursor; with two context providers above the portal, popProvider
    writes a wrong value (here null) into the context's _currentValue, which is what a consumer under no
    provider reads from then on.

updateOffscreenComponent already copes with the same reset for its own fiber (it re-creates stateNode
when it is null on mount); the portal has nothing like it. A <Profiler> has the same shape in development
builds: with the late node as the Profiler's own child the replay fails with
TypeError: Cannot set properties of null (setting 'effectDuration') (19.3.1, development; nothing in
production, and no stack warnings since a Profiler pushes nothing).

Where this comes from in practice

A client component that receives server-rendered content as a prop and hands it straight to createPortal
(createPortal(props.actions, place)). When part of that content is an async Server Component, the prop
is a lazy node whose row has not arrived yet. During a client navigation (a transition) the portal mounts
with that pending node as its own child; when the row arrives within the same tick, the portal is replayed
and fails as above. In an application it shows as an intermittent failure (about 1 navigation in 40 in the
case that led here), because it needs the row to arrive in exactly that window.

What I checked

A possible direction

Either keep the portal's stateNode across resetWorkInProgress (it describes where the portal draws, not
work in progress), or have replayBeginWork treat HostPortal without the full reset. Separately, a
beginWork that throws during a replay, after unwindInterruptedWork has already popped the fiber, should
not be unwound through unwindWork for that same fiber a second time.

Activity

  1. omerfarukgurbuz commented on Nov 25, 2026

    @omerfarukgurbuz

    I’ll investigate the mounting portal case on current main. I’ll start with a regression test that resolves the portal’s child during a transition and checks both the portal contents and the context value outside the providers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions