Skip to content

Conversation

@sisyphus-dev-ai
Copy link
Collaborator

@sisyphus-dev-ai sisyphus-dev-ai commented Jan 24, 2026

Summary

Fixes memory leak in BackgroundManager where completion timers (5-minute delayed cleanup) were not tracked or cancelled on shutdown, causing the manager instance to be held in memory by timer callbacks.

Investigation Summary

Investigated the 6 memory leak patterns mentioned in #1043:

Issue Status
setTimeout timers (manager.ts:1011-1018) REAL BUG - FIXED
setInterval polling (manager.ts:790-804) False positive - already stops when no tasks
session-notification.ts Maps/Sets False positive - has proper cleanup via maxTrackedSessions
tool-input-cache.ts cache False positive - has TTL cleanup every 60s
atlas/index.ts pendingFilePaths Minor - small strings, not worth fixing
context-injector/collector.ts sessions Minor - consumed during injection

Changes

  • Add completionTimers: Map<string, ReturnType<typeof setTimeout>> to track timer IDs
  • Store timer reference in notifyParentSession() when scheduling 5-minute cleanup
  • Cancel and remove timer when task is deleted via session.deleted event
  • Clear all timers in shutdown() method

Testing

  • TDD approach: wrote failing tests first, then implemented fix
  • 4 new tests covering timer tracking, shutdown cleanup, and session.deleted cleanup
  • All 83 background-agent tests pass
  • Typecheck passes

Closes #1043


Summary by cubic

Fixed a memory leak in BackgroundManager by tracking and cancelling 5-minute completion timers. Timers are now canceled on shutdown and when tasks are deleted, preventing the manager from being kept in memory (fixes #1043).

  • Bug Fixes
    • Added a completionTimers Map to track setTimeout handles.
    • Stored timers in notifyParentSession; cancel and remove on session.deleted.
    • Cleared all timers in shutdown to avoid leaks.

Written for commit 9e260ed. Summary will update on new commits.

Copy link

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 2 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

…memory leaks

Track setTimeout timers in notifyParentSession using a completionTimers Map.
Clear all timers on shutdown() and when tasks are deleted via session.deleted.
This prevents the BackgroundManager instance from being held in memory by
uncancelled timer callbacks.

Fixes #1043
@justsisyphus justsisyphus force-pushed the fix/memory-leak-completion-timers branch from 8815e7a to 9e260ed Compare January 24, 2026 07:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Question]: memory spikes

2 participants