From b7d0ac1ac533932990e134beab40b1219a7dccbe Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 11 Feb 2026 02:05:27 +0000 Subject: [PATCH] fix: prevent memory leak by clearing initial timeout in session cleanup Fixes potential memory leak where the initial setTimeout for scheduling the first cleanup was not being stored or cleared when stop() is called. Changes: - Add initialTimeout variable to store the initial setTimeout reference - Clear initialTimeout in stop() to prevent orphaned timeout - Set initialTimeout to null after execution for cleanup Impact: - Prevents memory leak if stop() is called before first cleanup executes - Prevents unexpected cleanup execution after stop() is called - Allows multiple start/stop cycles without side effects https://claude.ai/code/session_01SoNUGBEWbJwWWws3F2fuzh --- src/Signal/session-cleanup.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/Signal/session-cleanup.ts b/src/Signal/session-cleanup.ts index 0e399f1e..0e836024 100644 --- a/src/Signal/session-cleanup.ts +++ b/src/Signal/session-cleanup.ts @@ -71,6 +71,7 @@ export const makeSessionCleanup = ( config: SessionCleanupConfig = DEFAULT_SESSION_CLEANUP_CONFIG ) => { let cleanupInterval: ReturnType | null = null + let initialTimeout: ReturnType | null = null let lastCleanupAt: number = 0 let cleanupRunning: boolean = false @@ -398,7 +399,9 @@ export const makeSessionCleanup = ( '⏰ First cleanup scheduled' ) - setTimeout(async () => { + initialTimeout = setTimeout(async () => { + initialTimeout = null // Clear reference after execution + // Run first cleanup await runCleanup() @@ -413,6 +416,11 @@ export const makeSessionCleanup = ( * Stop periodic session cleanup */ const stop = () => { + if (initialTimeout) { + clearTimeout(initialTimeout) + initialTimeout = null + } + if (cleanupInterval) { clearInterval(cleanupInterval) cleanupInterval = null