fix(background-agent): prevent memory leak - completed tasks now removed from Map (#302)
- 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)
This commit is contained in:
@@ -325,6 +325,7 @@ export class BackgroundManager {
|
|||||||
|
|
||||||
log("[background-agent] Sending notification to parent session:", { parentSessionID: task.parentSessionID })
|
log("[background-agent] Sending notification to parent session:", { parentSessionID: task.parentSessionID })
|
||||||
|
|
||||||
|
const taskId = task.id
|
||||||
setTimeout(async () => {
|
setTimeout(async () => {
|
||||||
try {
|
try {
|
||||||
const messageDir = getMessageDir(task.parentSessionID)
|
const messageDir = getMessageDir(task.parentSessionID)
|
||||||
@@ -344,10 +345,13 @@ export class BackgroundManager {
|
|||||||
},
|
},
|
||||||
query: { directory: this.directory },
|
query: { directory: this.directory },
|
||||||
})
|
})
|
||||||
this.clearNotificationsForTask(task.id)
|
this.clearNotificationsForTask(taskId)
|
||||||
log("[background-agent] Successfully sent prompt to parent session:", { parentSessionID: task.parentSessionID })
|
log("[background-agent] Successfully sent prompt to parent session:", { parentSessionID: task.parentSessionID })
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
log("[background-agent] prompt failed:", String(error))
|
log("[background-agent] prompt failed:", String(error))
|
||||||
|
} finally {
|
||||||
|
this.tasks.delete(taskId)
|
||||||
|
log("[background-agent] Removed completed task from memory:", taskId)
|
||||||
}
|
}
|
||||||
}, 200)
|
}, 200)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user