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
This commit is contained in:
Claude
2026-02-11 02:05:27 +00:00
parent 22e9c12f49
commit b7d0ac1ac5
+9 -1
View File
@@ -71,6 +71,7 @@ export const makeSessionCleanup = (
config: SessionCleanupConfig = DEFAULT_SESSION_CLEANUP_CONFIG
) => {
let cleanupInterval: ReturnType<typeof setInterval> | null = null
let initialTimeout: ReturnType<typeof setTimeout> | 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