Skip to content

[Bug]: RSC dev: every server rebuild keeps the previous server bundle alive (react-server-dom-rspack's async hook and console wrappers) #16597

Open

Description

@Sheraff

Written by an AI coding agent (Claude) on behalf of a TanStack Router maintainer; reproduced locally.

System Info

System: Linux 6.12 x64 (Debian 13)
Binaries: Node 26.12.1, npm 12.2.0
npmPackages: @rsbuild/core 2.3.2, @rspack/core 2.3.2, rsbuild-plugin-rsc 0.1.1,
             react-server-dom-rspack 0.1.1 (also 0.1.1-canary-76155fe4-20261109), react 19.3.1

Details

In dev, loadBundle() evaluates the server bundle again after each rebuild, react-server-dom-rspack included. Each evaluation of react-server-dom-rspack/server.node (development build) enables a new async_hooks hook and wraps 13 console methods around the previous wrappers. Neither is ever undone, and both close over that evaluation's module scope, so every server bundle stays alive for the life of the dev server.

With rsbuild-plugin-rsc's own examples/server, unchanged, editing src/Todos.tsx 30 times (heap after GC):

edit  0: heap  23 MB, external  18 MB, server bundles alive 1/1, async hooks enabled 1
edit 10: heap  46 MB, external  33 MB, server bundles alive 11/11, async hooks enabled 11
edit 20: heap  69 MB, external  47 MB, server bundles alive 21/21, async hooks enabled 21
edit 30: heap  93 MB, external  62 MB, server bundles alive 31/31, async hooks enabled 31

Disabling the previous hook on each createHook() call is not enough (31/31 bundles still alive: the console wrappers hold them). Also putting back console's original methods keeps it flat:

edit 30: heap  26 MB, external  19 MB, server bundles alive 2/31, async hooks enabled 1

Both come from React's Flight dev code: createHook({...}).enable() in initAsyncDebugInfo and the module-level patchConsole(console, …) calls. React's own Jest setup works around the hook part by disabling the previous hook in a createHook mock (setupTests.js).

Suggested fix: make both installs idempotent per process in react-server-dom-rspack (keep the previous hook and console's original methods under a global symbol; disable the hook and wrap the originals on re-evaluation).

In a larger app (TanStack Start with RSC on Rsbuild) this is 7–12 MB per edit, and every enabled hook runs for each new async resource in the process.

Reproduce link

https://github.com/Sheraff/rsbuild-rsc-dev-retains-server-bundles

Reproduce Steps

  1. npm install
  2. npm run repro: starts rsbuild-plugin-rsc's examples/server with Rsbuild's JS API, edits src/Todos.tsx 30 times, prints the lines above; exits 1
  3. npm run repro -- --disable-hooks: disables the previous hook on each evaluation; still exits 1
  4. npm run repro -- --dispose: also restores console's methods; exits 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

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