opencode icon indicating copy to clipboard operation
opencode copied to clipboard

Memory leak: Instance disposal doesn't clean up plugins or cached resources

Open sauerdaniel opened this issue 19 hours ago • 1 comments

Problem

When Instance.dispose() is called, several resources are not properly cleaned up:

1. Plugins Not Disposed

Plugins may hold resources (connections, file handles, caches) that need cleanup, but there's no disposal hook in the Hooks interface:

interface Hooks {
  // No dispose() method exists
}

2. Cached Instances Not Disposed

The instance cache uses a standard Map without disposal hooks. When instances are evicted (due to memory pressure or explicit removal), their state is not properly cleaned up.

3. No Bootstrap Cleanup

When the process exits, plugins are not given a chance to clean up their resources.

Code Locations

  • packages/plugin/src/index.ts - Hooks interface
  • packages/opencode/src/plugin/index.ts - Plugin loading
  • packages/opencode/src/project/instance.ts - Instance cache
  • packages/opencode/src/project/bootstrap.ts - Bootstrap process

Impact

  • Plugin resources leak on instance disposal
  • Cache eviction doesn't trigger cleanup
  • Process exit leaves orphaned resources

Relates to #5363 Depends on #9140 (LRU cache utility needed for onEvict callback)

Expected Behavior

  • Plugins should have a dispose() hook
  • Cache eviction should trigger disposal
  • Bootstrap should clean up plugins on exit

sauerdaniel avatar Jan 17 '26 23:01 sauerdaniel