Commit 2d08f1a
fix: cache compiled modules to prevent per-request SourceTextModule leak
The buildLink() function had a cache for Node builtins but none for
regular imports. Every dynamic import() during SSR rendering compiled
a new vm.SourceTextModule, read the file from disk, and evaluated it.
These modules were never freed because they're linked to the vm.Context.
After ~30 SSR renders, accumulated SourceTextModule instances filled
the 384MB heap limit and the worker OOMed. The stack traces confirmed
this: OOM consistently occurred inside ModuleWrap::CompileSourceTextModule
and SourceTextModule::Evaluate.
The fix: cache compiled modules by resolved file path. A module compiled
once is reused for all subsequent imports of the same file. This is safe
because the file contents don't change at runtime, and all modules share
the same vm.Context.
Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>1 parent 6765c6b commit 2d08f1a
1 file changed
Lines changed: 8 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
243 | 243 | | |
244 | 244 | | |
245 | 245 | | |
246 | | - | |
| 246 | + | |
247 | 247 | | |
248 | 248 | | |
249 | | - | |
| 249 | + | |
250 | 250 | | |
251 | 251 | | |
252 | 252 | | |
| |||
262 | 262 | | |
263 | 263 | | |
264 | 264 | | |
265 | | - | |
| 265 | + | |
266 | 266 | | |
267 | 267 | | |
268 | 268 | | |
269 | 269 | | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
270 | 274 | | |
271 | 275 | | |
272 | 276 | | |
273 | 277 | | |
| 278 | + | |
274 | 279 | | |
275 | 280 | | |
276 | 281 | | |
| |||
0 commit comments