Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-lazy-resource-leak.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"solid-js": patch
---

Fix memory leak with nested `lazy()` components. `lazy()` cached its `createResource` accessor in a module-scoped variable and `createResource` never released its Suspense contexts on disposal, so a disposed component tree (and its detached DOM) stayed reachable across navigations when `lazy()` boundaries were nested. The module-pinned accessor is now released on cleanup, and the resource clears its Suspense contexts and pending promise on disposal.
8 changes: 8 additions & 0 deletions packages/solid/src/reactive/signal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,14 @@ export function createResource<T, S, R>(
resolved ? "ready" : "unresolved"
);

if (Owner)
onCleanup(() => {
for (const c of contexts.keys()) c.decrement!();
contexts.clear();
if (Transition && pr) Transition.promises.delete(pr);
pr = null;
});

if (sharedConfig.context) {
id = sharedConfig.getNextContextId();
if (options.ssrLoadFrom === "initial") initP = options.initialValue as T;
Expand Down
6 changes: 4 additions & 2 deletions packages/solid/src/render/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
createSignal,
createResource,
createMemo,
onCleanup,
devComponent,
$PROXY,
SUPPORTS_PROXY,
Expand Down Expand Up @@ -356,7 +357,7 @@ export function splitProps<
export function lazy<T extends Component<any>>(
fn: () => Promise<{ default: T }>
): T & { preload: () => Promise<{ default: T }> } {
let comp: () => T | undefined;
let comp: (() => T | undefined) | undefined;
let p: Promise<{ default: T }> | undefined;
const wrap: T & { preload?: () => void } = ((props: any) => {
const ctx = sharedConfig.context;
Expand All @@ -374,10 +375,11 @@ export function lazy<T extends Component<any>>(
} else if (!comp) {
const [s] = createResource<T>(() => (p || (p = fn())).then(mod => mod.default));
comp = s;
onCleanup(() => (comp = undefined));
}
let Comp: T | undefined;
return createMemo(() =>
(Comp = comp())
(Comp = comp?.())
? untrack(() => {
if (IS_DEV) Object.assign(Comp!, { [$DEVCOMP]: true });
if (!ctx || sharedConfig.done) return Comp!(props);
Expand Down