[PR #7032] fix(core): add dispose functions to prevent subscription memory leaks #12216

Open
opened 2026-02-16 18:17:08 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/anomalyco/opencode/pull/7032

State: open
Merged: No


Fixes #3013

Summary

  • Add dispose() functions to Share, ShareNext, Plugin, and Format namespaces to clean up Bus subscriptions
  • Add cleanupSession() and dispose() to ACP Agent for event subscription cleanup using AbortControllers
  • Add Bus._getSubscriptionCount() and Bus._getTotalSubscriptionCount() test helpers
  • Add memory tests to verify subscription cleanup works correctly

Problem

Bus subscriptions were accumulating during extended use because the return values from Bus.subscribe() were being ignored. Over time, this caused memory growth that could contribute to Bun running out of memory.

Solution

Each module that subscribes to Bus events now:

  1. Stores the unsubscribe function returned by Bus.subscribe()
  2. Provides a dispose() function that calls all stored unsubscribe functions

For ACP Agent specifically, session-scoped subscriptions use AbortControllers so they can be cleaned up when a session ends.

Testing

Added 10 unit tests in test/memory/ that verify:

  • Each module's dispose() properly unsubscribes
  • Multiple init/dispose cycles don't leak subscriptions
  • ACP session cleanup works correctly

All tests pass: bun test test/memory/

**Original Pull Request:** https://github.com/anomalyco/opencode/pull/7032 **State:** open **Merged:** No --- Fixes #3013 ## Summary - Add `dispose()` functions to Share, ShareNext, Plugin, and Format namespaces to clean up Bus subscriptions - Add `cleanupSession()` and `dispose()` to ACP Agent for event subscription cleanup using AbortControllers - Add `Bus._getSubscriptionCount()` and `Bus._getTotalSubscriptionCount()` test helpers - Add memory tests to verify subscription cleanup works correctly ## Problem Bus subscriptions were accumulating during extended use because the return values from `Bus.subscribe()` were being ignored. Over time, this caused memory growth that could contribute to Bun running out of memory. ## Solution Each module that subscribes to Bus events now: 1. Stores the unsubscribe function returned by `Bus.subscribe()` 2. Provides a `dispose()` function that calls all stored unsubscribe functions For ACP Agent specifically, session-scoped subscriptions use AbortControllers so they can be cleaned up when a session ends. ## Testing Added 10 unit tests in `test/memory/` that verify: - Each module's `dispose()` properly unsubscribes - Multiple init/dispose cycles don't leak subscriptions - ACP session cleanup works correctly All tests pass: `bun test test/memory/`
yindo added the pull-request label 2026-02-16 18:17:08 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#12216