From 18d134fa57f3f29188e833f68300bea0e7044377 Mon Sep 17 00:00:00 2001 From: YeonGyu-Kim Date: Sun, 28 Dec 2025 14:59:06 +0900 Subject: [PATCH] fix(background-agent): prevent memory leak - completed tasks now removed from Map (#302) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add finally block in notifyParentSession() to ensure task cleanup - Call tasks.delete(taskId) after notification sent or on error - Prevents memory accumulation when tasks complete or fail - taskId captured before setTimeout to ensure proper cleanup in async context 🤖 Generated with assistance of OhMyOpenCode (https://github.com/code-yeongyu/oh-my-opencode) --- src/features/background-agent/manager.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/features/background-agent/manager.ts b/src/features/background-agent/manager.ts index cb2f03e..1b3dbea 100644 --- a/src/features/background-agent/manager.ts +++ b/src/features/background-agent/manager.ts @@ -325,6 +325,7 @@ export class BackgroundManager { log("[background-agent] Sending notification to parent session:", { parentSessionID: task.parentSessionID }) + const taskId = task.id setTimeout(async () => { try { const messageDir = getMessageDir(task.parentSessionID) @@ -344,10 +345,13 @@ export class BackgroundManager { }, query: { directory: this.directory }, }) - this.clearNotificationsForTask(task.id) + this.clearNotificationsForTask(taskId) log("[background-agent] Successfully sent prompt to parent session:", { parentSessionID: task.parentSessionID }) } catch (error) { log("[background-agent] prompt failed:", String(error)) + } finally { + this.tasks.delete(taskId) + log("[background-agent] Removed completed task from memory:", taskId) } }, 200) }