Skip to content

Dispose dev child entries after their parents are disposed - #96804

Draft
marcoshernanz wants to merge 1 commit into
canaryfrom
marcos/memleak-child-entry
Draft

Dispose dev child entries after their parents are disposed#96804
marcoshernanz wants to merge 1 commit into
canaryfrom
marcos/memleak-child-entry

Conversation

@marcoshernanz

@marcoshernanz marcoshernanz commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

What?

disposeInactiveEntries now disposes CHILD_ENTRY records once none of their parent entries are active anymore, instead of skipping them entirely (a TODO-APP), and the disposal machinery treats revival and cached restarts correctly.

Why?

The webpack dev server never disposed child entries: every visited app page left its client-side flight entry in the entries map and in the client webpack compilation for the lifetime of the dev server, accumulating compilation memory during long dev sessions (#54708).

A child entry is injected by its parent entries (e.g. the client flight entry of an app page) and is needed exactly as long as at least one of them. The disposal lifecycle now works in four places:

  • disposeInactiveEntries is two-phase: stale ENTRY records are marked first, the active set is computed after, and each child entry's disposal state is then recomputed from it — so orphaned children are scheduled in the same pass as their parents (a child that lagged a pass behind could survive a cleanup rebuild and persist indefinitely). The recompute is idempotent: parent links are kept, so revival is possible.
  • Revival is synchronous: the ping/ensure sites that clear a parent's disposal flag (handleAppDirPing, handlePing, ensurePageImpl) also clear matching disposed children via reviveChildEntriesFor, so a revived route never carries a child with a stale disposal flag — including into Rspack's built-entries cache.
  • hot-reloader-webpack's entry computation (the site that deletes flagged entries) consults live parent state computed after the awaited entry resolution: a child whose parent was revived mid-build is kept even while it still carries a stale disposal flag.
  • hot-reloader-rspack persists child parentEntries as arrays in built-entries.json and rehydrates them into Sets on read (tolerating legacy caches that stored them as empty objects): the parent links now survive restarts, and disposal iteration can no longer crash on a plain-object parentEntries.

Only ENTRY records count as parents: a child entry shares its parent's bundle path, so considering every record would keep the parent set alive through the child itself.

Note on #89587

The pre-existing open PR #89587 attempts this fix but compares parent names against the bundle paths of all undisposed records including the child itself, which always matches (parentEntries contains the server entry name, which equals the child's bundlePath), so it never disposes anything.

Tests

  • Unit tests in packages/next/src/server/dev/on-demand-entry-handler.test.ts (pnpm jest packages/next/src/server/dev/on-demand-entry-handler.test.ts): same-pass parent+child disposal, removal of gone parents, empty parent sets, shared children with mixed parent states, synchronous + pass-based revival, active-parent computation, selective child revival, middleware/instrumentation exemptions. The disposal tests fail without the fix; 11/11 pass with it.
  • Verified end to end in a webpack dev server (app router fixture) instrumented to dump the entries map per pass: after maxInactiveAge, parents and children of three visited app pages were scheduled in the same pass and removed on the next compilation; re-visiting a disposed page revived both records synchronously and rendered fine.
  • Four autoreview panel rounds of findings addressed (pruning semantics, deletion-site guard, Rspack Set persistence/rehydration, pass ordering, revival sites).

Fixes #54708

@github-actions

github-actions Bot commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

Tests Passed

Commit: 4df5d7c

@github-actions

github-actions Bot commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

Stats from current PR

🟢 2 improvements

Metric Canary PR Change Trend
Turbo Build Time 6.280s 5.648s 🟢 632ms (-10%) ▂▁▁▂▂
Turbo Build Time (cached) 3.224s 2.895s 🟢 329ms (-10%) ▂▁▁▂▂
📊 All Metrics
📖 Metrics Glossary

Dev Server Metrics:

  • Listen = TCP port starts accepting connections
  • First Request = HTTP server returns successful response
  • Cold = Fresh build (no cache)
  • Warm = With cached build artifacts

Build Metrics:

  • Fresh = Clean build (no .next directory)
  • Cached = With existing .next directory

Change Thresholds:

  • Time: Changes < 50ms AND < 10%, OR < 2% are insignificant
  • Size: Changes < 1KB AND < 1% are insignificant
  • All other changes are flagged to catch regressions

⚡ Dev Server

Metric Canary PR Change Trend
Cold (Listen) 813ms 813ms ▁▁▁▁▁
Cold (Ready in log) 799ms 785ms ▁▁▁▁▁
Cold (First Request) 1.338s 1.281s ▂▁▁▁▂
Warm (Listen) 814ms 813ms ▂▂▂▂▂
Warm (Ready in log) 793ms 782ms ▁▁▁▁▁
Warm (First Request) 1.298s 1.266s ▁▁▁▁▁
📦 Dev Server (Webpack) (Legacy)

📦 Dev Server (Webpack)

Metric Canary PR Change Trend
Cold (Listen) 812ms 811ms ▁████
Cold (Ready in log) 780ms 781ms ▂▆█▇▅
Cold (First Request) 3.364s 3.335s ▃▄█▆▄
Warm (Listen) 812ms 812ms █████
Warm (Ready in log) 779ms 777ms ▂▆█▆▆
Warm (First Request) 3.362s 3.350s ▄▅█▆▅

⚡ Production Builds

Metric Canary PR Change Trend
Fresh Build 6.280s 5.648s 🟢 632ms (-10%) ▂▁▁▂▂
Cached Build 3.224s 2.895s 🟢 329ms (-10%) ▂▁▁▂▂
📦 Production Builds (Webpack) (Legacy)

📦 Production Builds (Webpack)

Metric Canary PR Change Trend
Fresh Build 24.000s 23.800s ▃▅█▆█
Cached Build 23.985s 23.906s ▃▄█▆▇
node_modules Size 553 MB 553 MB ▁▁▁▁▁
📦 Bundle Sizes

Bundle Sizes

⚡ Turbopack

Client

Main Bundles
Canary PR Change
00gjhzer5qnd4.js gzip 154 B N/A -
058w61dpm3cqw.js gzip 65.6 kB N/A -
07jdby0ue616s.js gzip 450 B N/A -
0afsxfu2qvoii.js gzip 155 B N/A -
0awb0vu674hnk.js gzip 158 B N/A -
0b7f6-wm5_fgx.js gzip 10.3 kB N/A -
0cz1d0mv5g_q7.js gzip 39.4 kB 39.4 kB
0hnzlx68v8wq8.js gzip 8.7 kB N/A -
0ky-xprxap4kv.js gzip 158 B N/A -
0r0fd4d11wyn4.js gzip 71.5 kB N/A -
0r7ql6wdiv73c.js gzip 10 kB N/A -
0zzo4lng-xek6.js gzip 9.44 kB N/A -
10k2k258ymu43.js gzip 8.78 kB N/A -
1a3hvn719yvjk.js gzip 8.75 kB N/A -
1b0_t57fceog6.js gzip 170 B N/A -
1cgyoj65s6d9x.js gzip 156 B N/A -
1elt1qium-r2m.css gzip 115 B 115 B
1enuk65o0iyxc.js gzip 8.7 kB N/A -
1j8mwpx902qpn.js gzip 164 B N/A -
1myuaxlmtuan4.js gzip 8.75 kB N/A -
1tf1phijqlx9j.js gzip 220 B 220 B
1tgz0p9msc4tz.js gzip 155 B N/A -
29cpgg4v96x1a.js gzip 46.3 kB N/A -
2emlnosu2nbyp.js gzip 10.6 kB N/A -
2m3bh1binl60-.js gzip 13.2 kB N/A -
2n72evcce9v1r.js gzip 8.77 kB N/A -
2y5oyfvi8x17-.js gzip 157 B N/A -
31m0nox7lipas.js gzip 7.53 kB N/A -
34bfz28htmaat.js gzip 156 B N/A -
38-q43pzktqhs.js gzip 1.46 kB N/A -
38uv8-7_7l5av.js gzip 8.78 kB N/A -
3ac6edgvuj2vd.js gzip 3.66 kB N/A -
3ceky-0yt3r4e.js gzip 8.8 kB N/A -
3jb_acjto1dn1.js gzip 13.6 kB N/A -
3kfiy2dr7a5m3.js gzip 5.72 kB N/A -
3mrwxzed0ylgv.js gzip 13.1 kB N/A -
3vucd_i-9s02n.js gzip 154 B N/A -
407ltsw2jd-3v.js gzip 158 B N/A -
41u5s3oe2-erp.js gzip 2.29 kB N/A -
428qlfq3v26qp.js gzip 161 B N/A -
turbopack-08..64w9.js gzip 3.84 kB N/A -
turbopack-08..byc8.js gzip 3.86 kB N/A -
turbopack-0b..-c8g.js gzip 3.86 kB N/A -
turbopack-0m..dzni.js gzip 3.86 kB N/A -
turbopack-0r..y6wd.js gzip 3.86 kB N/A -
turbopack-0v..5wac.js gzip 3.86 kB N/A -
turbopack-1_..mi45.js gzip 3.86 kB N/A -
turbopack-1e..9fou.js gzip 3.86 kB N/A -
turbopack-1n..darg.js gzip 3.86 kB N/A -
turbopack-1t..zf9u.js gzip 3.86 kB N/A -
turbopack-1y..rqab.js gzip 3.88 kB N/A -
turbopack-21..vkoq.js gzip 3.86 kB N/A -
turbopack-3t..zm7h.js gzip 3.86 kB N/A -
turbopack-3t..xu-0.js gzip 3.86 kB N/A -
00kbbwl1whzq5.js gzip N/A 155 B -
070i4ykf3d62z.js gzip N/A 8.7 kB -
093rf1t2n-ajl.js gzip N/A 46.3 kB -
0agv3-x0j9qy3.js gzip N/A 10.3 kB -
0fp4xgd37m5-d.js gzip N/A 3.65 kB -
0j7ubrcd_njd7.js gzip N/A 162 B -
0jwho9fkrb_t3.js gzip N/A 2.29 kB -
0uqaj5-kagorf.js gzip N/A 160 B -
0wg1cc2wbyjd6.js gzip N/A 8.77 kB -
0z2rm17uxhhx5.js gzip N/A 158 B -
1_syfo9rv8a2q.js gzip N/A 7.54 kB -
10n4t641ac4-m.js gzip N/A 158 B -
12-pii8wla4es.js gzip N/A 71.6 kB -
14j95hw2e7a3z.js gzip N/A 8.74 kB -
166ci2e3b45t7.js gzip N/A 154 B -
17oe55cu76cd7.js gzip N/A 450 B -
1frm88k70_rxf.js gzip N/A 8.78 kB -
1rvsu5wwb2kn-.js gzip N/A 158 B -
1ynlcmklb8_j3.js gzip N/A 9.45 kB -
24yke4g7c6tl3.js gzip N/A 157 B -
2a_p4b_g75xdo.js gzip N/A 8.8 kB -
2ce6apx02oexo.js gzip N/A 156 B -
2dn0ae5-nmkio.js gzip N/A 13.6 kB -
2g2f64lmg3m7w.js gzip N/A 5.73 kB -
2kolt5lwxt8o0.js gzip N/A 10 kB -
2lpk5_hknut8q.js gzip N/A 13.1 kB -
2p0a0fr4ydefu.js gzip N/A 8.7 kB -
2p9-5diwx5gt6.js gzip N/A 10.6 kB -
2tlidw2e-lxb8.js gzip N/A 65.6 kB -
2ugc69z0t0ypz.js gzip N/A 1.46 kB -
3552-t9hisyhx.js gzip N/A 155 B -
39rn5wd6qslc0.js gzip N/A 156 B -
3crtg4i2per05.js gzip N/A 8.74 kB -
3ghtbhrse9p63.js gzip N/A 8.78 kB -
3hk4sk0tzj4o2.js gzip N/A 169 B -
3q0h3yzsw1g9v.js gzip N/A 13.2 kB -
3wp6cj5wrp0d8.js gzip N/A 156 B -
turbopack-07..60mu.js gzip N/A 3.86 kB -
turbopack-09..viho.js gzip N/A 3.86 kB -
turbopack-0a..-k6d.js gzip N/A 3.84 kB -
turbopack-14..fm0u.js gzip N/A 3.86 kB -
turbopack-16..-t6e.js gzip N/A 3.86 kB -
turbopack-19..0j_1.js gzip N/A 3.86 kB -
turbopack-1e..6nfx.js gzip N/A 3.86 kB -
turbopack-1n..goqe.js gzip N/A 3.86 kB -
turbopack-1p..m_om.js gzip N/A 3.86 kB -
turbopack-1r..a-g8.js gzip N/A 3.86 kB -
turbopack-1z..hpil.js gzip N/A 3.86 kB -
turbopack-3p..c8lx.js gzip N/A 3.86 kB -
turbopack-3p..ut2y.js gzip N/A 3.88 kB -
turbopack-42..4cr1.js gzip N/A 3.86 kB -
Total 451 kB 451 kB ✅ -5 B

Server

Middleware
Canary PR Change
middleware-b..fest.js gzip 775 B 771 B
Total 775 B 771 B ✅ -4 B
Build Details
Build Manifests
Canary PR Change
_buildManifest.js gzip 437 B 434 B
Total 437 B 434 B ✅ -3 B
Build Cache
Canary PR Change
00000001.sst gzip 13.8 MB 14.1 MB 🔴 +352 kB (+3%)
00000002.sst gzip 12.7 MB 14.9 MB 🔴 +2.24 MB (+18%)
00000003.sst gzip 15.3 MB 12.7 MB 🟢 2.64 MB (-17%)
00000004.sst gzip 13.3 MB 13.4 MB 🔴 +64.9 kB (+0%)
00000005.sst gzip 2.81 MB 2.81 MB
00000006.sst gzip 10.2 MB 10.2 MB 🔴 +11.5 kB (+0%)
00000007.sst gzip 59 B 59 B
00000008.meta gzip 89 B 89 B
00000009.meta gzip 298 kB 298 kB
00000010.meta gzip 298 kB 298 kB
00000011.meta gzip 298 kB 298 kB
00000012.sst gzip 51.2 kB 50.9 kB
00000013.sst gzip 1.88 MB 1.85 MB 🟢 29 kB (-2%)
00000014.sst gzip 59 B 59 B
00000015.meta gzip 116 B 116 B
00000016.meta gzip 324 kB 324 kB
00000017.meta gzip 404 kB 404 kB
00000018.sst gzip 51.9 kB 51.8 kB
00000019.sst gzip 1.39 MB 1.39 MB
00000020.sst gzip 59 B 59 B
00000021.meta gzip 116 B 116 B
00000022.meta gzip 324 kB 324 kB
00000023.meta gzip 370 kB 370 kB
00000024.sst gzip 52.1 kB 51.8 kB
00000025.sst gzip 1.39 MB 1.39 MB 🔴 +3.7 kB (+0%)
00000026.sst gzip 59 B 59 B
00000027.meta gzip 116 B 116 B
00000028.meta gzip 324 kB 324 kB
00000029.meta gzip 370 kB 370 kB
00000030.sst gzip 51.2 kB 51 kB
00000031.sst gzip 1.39 MB 1.39 MB 🔴 +3.95 kB (+0%)
00000032.sst gzip 59 B 59 B
00000033.meta gzip 116 B 116 B
00000034.meta gzip 324 kB 324 kB
00000035.meta gzip 370 kB 370 kB
00000036.sst gzip 52 kB 51.6 kB
00000037.sst gzip 1.39 MB 1.39 MB 🔴 +3.68 kB (+0%)
00000038.sst gzip 59 B 59 B
00000039.meta gzip 116 B 116 B
00000040.meta gzip 324 kB 324 kB
00000041.meta gzip 370 kB 370 kB
CURRENT gzip 24 B 24 B
LOG gzip 679 B 662 B 🟢 17 B (-3%)
Total 80.2 MB 80.2 MB ⚠️ +17.6 kB

📦 Webpack

Client

Main Bundles
Canary PR Change
3322-HASH.js gzip 65.2 kB N/A -
4191.HASH.js gzip 169 B N/A -
7920-HASH.js gzip 4.67 kB N/A -
9784-HASH.js gzip 5.63 kB N/A -
b1ad9f4c-HASH.js gzip 63.2 kB N/A -
framework-HASH.js gzip 59.7 kB 59.7 kB
main-app-HASH.js gzip 253 B 254 B
main-HASH.js gzip 40 kB 40 kB
webpack-HASH.js gzip 1.68 kB 1.68 kB
3577.HASH.js gzip N/A 168 B -
578-HASH.js gzip N/A 65.9 kB -
8590-HASH.js gzip N/A 5.6 kB -
9750-HASH.js gzip N/A 4.68 kB -
a8984546-HASH.js gzip N/A 63.2 kB -
Total 241 kB 241 kB ⚠️ +674 B
Polyfills
Canary PR Change
polyfills-HASH.js gzip 39.4 kB 39.4 kB
Total 39.4 kB 39.4 kB
Pages
Canary PR Change
_app-HASH.js gzip 194 B 193 B
_error-HASH.js gzip 181 B 182 B
css-HASH.js gzip 334 B 331 B
dynamic-HASH.js gzip 1.81 kB 1.81 kB
edge-ssr-HASH.js gzip 255 B 253 B
head-HASH.js gzip 349 B 351 B
hooks-HASH.js gzip 382 B 384 B
image-HASH.js gzip 581 B 582 B
index-HASH.js gzip 260 B 259 B
link-HASH.js gzip 2.48 kB 2.48 kB
routerDirect..HASH.js gzip 317 B 318 B
script-HASH.js gzip 384 B 386 B
withRouter-HASH.js gzip 316 B 315 B
1afbb74e6ecf..834.css gzip 106 B 106 B
Total 7.95 kB 7.95 kB ⚠️ +1 B

Server

Edge SSR
Canary PR Change
edge-ssr.js gzip 129 kB 128 kB
page.js gzip 294 kB 294 kB
Total 422 kB 423 kB ⚠️ +319 B
Middleware
Canary PR Change
middleware-b..fest.js gzip 616 B 617 B
middleware-r..fest.js gzip 156 B 156 B
middleware.js gzip 45.7 kB 45.2 kB 🟢 545 B (-1%)
edge-runtime..pack.js gzip 842 B 842 B
Total 47.3 kB 46.8 kB ✅ -544 B
Build Details
Build Manifests
Canary PR Change
_buildManifest.js gzip 718 B 718 B
Total 718 B 718 B
Build Cache
Canary PR Change
0.pack gzip 4.87 MB 4.86 MB
index.pack gzip 122 kB 123 kB
index.pack.old gzip 122 kB 123 kB
Total 5.11 MB 5.11 MB ✅ -2.48 kB

🔄 Shared (bundler-independent)

Runtimes
Canary PR Change
app-page-exp...dev.js gzip 374 kB 374 kB
app-page-exp..prod.js gzip 207 kB 207 kB
app-page-tur...dev.js gzip 374 kB 374 kB
app-page-tur..prod.js gzip 206 kB 206 kB
app-page-tur...dev.js gzip 370 kB 370 kB
app-page-tur..prod.js gzip 204 kB 204 kB
app-page.run...dev.js gzip 370 kB 370 kB
app-page.run..prod.js gzip 204 kB 204 kB
app-route-ex...dev.js gzip 83.3 kB 83.3 kB
app-route-ex..prod.js gzip 56.4 kB 56.4 kB
app-route-tu...dev.js gzip 83.3 kB 83.3 kB
app-route-tu..prod.js gzip 56.4 kB 56.4 kB
app-route-tu...dev.js gzip 82.9 kB 82.9 kB
app-route-tu..prod.js gzip 56.2 kB 56.2 kB
app-route.ru...dev.js gzip 82.8 kB 82.8 kB
app-route.ru..prod.js gzip 56.2 kB 56.2 kB
dev-validati...dev.js gzip 133 kB 133 kB
dev-validati...dev.js gzip 133 kB 133 kB
dev-validati...dev.js gzip 131 kB 131 kB
dev-validati...dev.js gzip 131 kB 131 kB
dist_client_...dev.js gzip 324 B 324 B
dist_client_...dev.js gzip 326 B 326 B
dist_client_...dev.js gzip 318 B 318 B
dist_client_...dev.js gzip 317 B 317 B
pages-api-tu...dev.js gzip 46 kB 46 kB
pages-api-tu..prod.js gzip 34.3 kB 34.3 kB
pages-api.ru...dev.js gzip 46 kB 46 kB
pages-api.ru..prod.js gzip 34.3 kB 34.3 kB
pages-turbo....dev.js gzip 54.8 kB 54.8 kB
pages-turbo...prod.js gzip 39.9 kB 39.9 kB
pages.runtim...dev.js gzip 54.8 kB 54.8 kB
pages.runtim..prod.js gzip 39.9 kB 39.9 kB
server.runti..prod.js gzip 67 kB 67 kB
use-cache-pr...dev.js gzip 72.3 kB 72.3 kB
use-cache-pr...dev.js gzip 72.4 kB 72.4 kB
use-cache-pr...dev.js gzip 70.6 kB 70.6 kB
use-cache-pr...dev.js gzip 70.6 kB 70.6 kB
Total 4.1 MB 4.1 MB ✅ -1 B
📎 Tarball URL
https://vercel-packages.vercel.app/next/commits/4df5d7cd1b562b77675caa87f5b8d636f17c0a5c/next

Commit: 4df5d7c

@marcoshernanz
marcoshernanz force-pushed the marcos/memleak-child-entry branch from a35623d to c4d2f10 Compare August 6, 2026 08:33
Comment thread packages/next/src/server/dev/on-demand-entry-handler.test.ts Outdated
@marcoshernanz
marcoshernanz force-pushed the marcos/memleak-child-entry branch 2 times, most recently from 4df5d7c to dca9aaa Compare August 6, 2026 18:11
The webpack dev server never disposed CHILD_ENTRY records in
on-demand-entry-handler (a TODO-APP): disposeInactiveEntries skipped them
entirely, so every visited app page left its client-side flight entry in
the entries map and in the client webpack compilation for the lifetime of
the dev server, accumulating compilation memory during long dev sessions
(#54708).

A child entry is injected by its parent entries (e.g. the client flight
entry of an app page) and is needed exactly as long as at least one of
them. This changes the disposal lifecycle in three places:

- disposeInactiveEntries recomputes each child entry's disposal state
  once per pass from the bundle paths of the active ENTRY records:
  scheduled once no active parent remains, and cleared again if a parent
  is revived. Recomputing instead of permanently pruning the parent set
  matters because the disposal flag is only pending until the disposal
  machinery runs.
- hot-reloader-webpack's entry computation, which deletes entries whose
  disposal flag is set, now consults live parent state for child entries:
  a child whose parent was revived after the last disposal pass is kept
  even while it still carries a stale disposal flag.
- hot-reloader-rspack rehydrates child parentEntries back into Sets when
  reading built-entries.json: JSON.stringify serializes Sets as plain
  objects, which would otherwise crash disposal iteration on restart.

Only ENTRY records count as parents: a child entry shares its parent
bundle path, so considering every record would keep the parent set alive
through the child itself.

The pre-existing open PR #89587 attempted this fix but compared parent
names against the bundle paths of all undisposed records including the
child itself, which always matches and therefore never disposes anything.

Adds unit tests covering: stale-parent disposal across passes, removal of
gone parents, empty parent sets, shared children with mixed parent
states, revival of a parent scheduled for disposal, active-parent
computation, and middleware/instrumentation exemptions.

Verified end to end in a webpack dev server instrumented to dump the
entries map per pass: after maxInactiveAge the parent entries of three
visited app pages were scheduled, their child entries were scheduled in
the following passes with the parent links kept intact, and the next
compilation removed all of them from the map; re-visiting a disposed page
revived its entries and rendered fine.

Fixes #54708

Co-Authored-By: Marcos Hernanz <96699542+marcoshernanz@users.noreply.github.com>
@marcoshernanz
marcoshernanz force-pushed the marcos/memleak-child-entry branch from dca9aaa to 166c4f6 Compare August 6, 2026 18:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Next.js development high memory usage

1 participant