fix(server): prevent RequestEvent retention in AsyncLocalStorage async resources after SSR#15770
Open
Zelys-DFKH wants to merge 4 commits into
Open
fix(server): prevent RequestEvent retention in AsyncLocalStorage async resources after SSR#15770Zelys-DFKH wants to merge 4 commits into
Zelys-DFKH wants to merge 4 commits into
Conversation
…er SSR Wrap the store passed to `als.run()` in a single-property container so that async resources created inside the render callback (Promise continuations, Svelte 4 component_subscribe callbacks, etc.) only retain a reference to the container rather than the full RequestStore. After the render promise settles we null out `container.current`, allowing the RequestStore and RequestEvent to be garbage-collected even when stale async resources from the render still linger in memory. Without this fix, every `als.run(store, render)` call causes Node.js to associate `kResourceStore = store` with every async resource created during the render (nodejs/node#53408). Under load, hundreds of RequestEvent objects accumulate as those resources are not GC'd, leading to linear heap growth and eventual OOM. Fixes sveltejs#15764 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
🦋 Changeset detectedLatest commit: a1192cb The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
… in event.spec Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Narrows the AsyncLocalStorage container-nulling fix to the SSR render call in render.js, which is the only context where async resources (Svelte 4 component_subscribe callbacks) can outlive the request and cause a memory leak. Other with_request_store callers (command handlers, query batch, etc.) now receive gc_barrier=false (the default), so their async continuations (get_response await-0 deferral, batch setTimeout callbacks) can still read the store for the duration of the request. Fixes the query.batch refresh-in-command single-flight regression by restoring store access for those async resources without reintroducing the original memory leak. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
closes #15764
Hey! I tracked down what's causing the heap to grow under SSR load with Svelte 4 apps.
What's going on
with_request_storewraps the SSR render inals.run(store, renderFn). Node.js has a quirk in async context propagation (nodejs/node#53408): every async resource created insiderenderFn(Promise continuations, callbacks, the works) getskResourceStore = storestamped onto it internally. In Svelte 4 apps,component_subscribecreates subscription callbacks inside this context that can outlive the render. Those callbacks hold a strong reference tostore, so theRequestStoreand itsRequestEventnever get garbage-collected. Under any real load, this accumulates fast.Svelte 5 isn't affected because subscriptions are released per-component immediately. Svelte 4 batches them until the full component tree renders.
The fix
Instead of passing
storedirectly toals.run(), I wrap it in a single-property container ({ current: store }) and pass that instead. Async resources created during rendering hold a reference to the lightweight container, not the store itself.with_request_storegets agc_barrierparameter (defaultfalse). Only the SSR render call inrender.jspassesgc_barrier = true; after the render promise settles,container.currentis nulled. That severs the only path from lingering Svelte 4 subscription callbacks back to theRequestStore, and normal GC takes over.Other callers (command handlers, query batches, form handlers) keep
gc_barrier = false. Their async continuations (theawait 0inget_response, thesetTimeout(0)in batch processing) need store access for the duration of the request, so nulling would break them. Scoping the barrier to the render path leaves those flows untouched.Changes
packages/kit/src/exports/internal/event.js— wrap ALS value in a container; addgc_barrierparameter (defaultfalse) to null the container only when the caller opts inpackages/kit/src/runtime/server/page/render.js— passgc_barrier = trueto thewith_request_storecall that wraps the component render (the only source of this leak)packages/kit/src/exports/internal/event.spec.js— new unit tests including a regression test that spawns a long-lived async resource insideals.run()withgc_barrier = trueand asserts it cannot reach the store after render completes (fails on pre-fix code)All existing tests pass. Happy to adjust anything — thanks for all the work you put into this.
Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
Tests
pnpm testand lint the project withpnpm lintandpnpm checkChangesets
pnpm changesetand following the prompts. Changesets that add features should beminorand those that fix bugs should bepatch. Please prefix changeset messages withfeat:,fix:, orchore:.Edits