From 080b3687d3206ae9611f7c198ee717379bbcdc88 Mon Sep 17 00:00:00 2001 From: arturovt Date: Thu, 22 May 2025 00:54:56 +0300 Subject: [PATCH] fix(core): unregister `onDestroy` in `ResourceImpl` when `destroy()` is called (#61870) This commit unregisters the `onDestroy` listener when `destroy()` is called on the `ResourceImpl`. This prevents memory leaks and ensures that the resource reference is not captured in the destroy callback after it has already been destroyed. PR Close #61870 --- packages/core/src/resource/resource.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/core/src/resource/resource.ts b/packages/core/src/resource/resource.ts index 6ed4b3f14e6..9d5e1776ea1 100644 --- a/packages/core/src/resource/resource.ts +++ b/packages/core/src/resource/resource.ts @@ -167,6 +167,7 @@ export class ResourceImpl extends BaseWritableResource implements Resou private pendingController: AbortController | undefined; private resolvePendingTask: (() => void) | undefined = undefined; private destroyed = false; + private unregisterOnDestroy: () => void; constructor( request: () => R, @@ -250,7 +251,7 @@ export class ResourceImpl extends BaseWritableResource implements Resou this.pendingTasks = injector.get(PendingTasks); // Cancel any pending request when the resource itself is destroyed. - injector.get(DestroyRef).onDestroy(() => this.destroy()); + this.unregisterOnDestroy = injector.get(DestroyRef).onDestroy(() => this.destroy()); } override readonly status = computed(() => projectStatusOfState(this.state())); @@ -302,6 +303,7 @@ export class ResourceImpl extends BaseWritableResource implements Resou destroy(): void { this.destroyed = true; + this.unregisterOnDestroy(); this.effectRef.destroy(); this.abortInProgressLoad();