Skip to content

Bug: destroying an unread Node prerender prelude leaves its abort listener attached #38177

Open

Description

@sameerihs

React version: react@19.3.1 and react-dom@19.3.1, reproduced using the published packages in both development and production.

Environment: Node 24.22.1, macOS arm64.

Steps To Reproduce

Install react@19.3.1 and react-dom@19.3.1, then run this as an .mjs file:

import React from 'react';
import {prerenderToNodeStream} from 'react-dom/static';
import {getEventListeners, once} from 'node:events';

const controller = new AbortController();
try {
  const {prelude} = await prerenderToNodeStream(
    React.createElement('div', null, 'hello'),
    {signal: controller.signal},
  );
  const before = getEventListeners(controller.signal, 'abort').length;
  const closed = once(prelude, 'close');
  prelude.destroy();
  await closed;
  await new Promise(resolve => setImmediate(resolve));
  await new Promise(resolve => setImmediate(resolve));

  console.log({
    before,
    after: getEventListeners(controller.signal, 'abort').length,
    destroyed: prelude.destroyed,
    closed: prelude.closed,
  });
} finally {
  controller.abort();
}

The current behavior

The output is {before: 1, after: 1, destroyed: true, closed: true}. The unread stream has closed, but the caller's signal still holds its abort listener. That listener closes over the Fizz request and retains it while the signal remains alive.

I also reproduced this with destroy(error) and with a prelude returned by resumeAndPrerenderToNodeStream. The latter uses postponed state returned by the public prerender API, consumed and then serialised before resuming. Fully consuming a Node prelude or cancelling a Web prelude from prerender removes the listener as expected.

The expected behavior

Destroying the Node prelude should notify Fizz that the consumer has cancelled, so the listener count becomes zero without also requiring the caller to abort its signal.

This is a follow-up to the lifetime cleanup in #37315 / #36763. The Node Readables in ReactDOMFizzStaticNode have a read hook but no destruction hook. Forwarding destruction to stopFlowing and abort makes the three regression cases pass while preserving the consumed-Node, cancelled-Web and stream-error controls.

The measurements above establish listener retention; they do not measure retained heap size.

Activity

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

      ⌂ Proxy