fix(disposal): Complete Instance disposal chain for plugins and caches#9149
fix(disposal): Complete Instance disposal chain for plugins and caches#9149sauerdaniel wants to merge 4 commits intoanomalyco:devfrom
Conversation
|
Thanks for your contribution! This PR doesn't have a linked issue. All PRs must reference an existing issue. Please:
See CONTRIBUTING.md for details. |
|
The following comment was made by an LLM, it may be inaccurate: Based on my search, I found the following related PRs: Related/Dependent PRs:
Similar Previous Work:
Note: PR #9149 is the current PR being checked, so it's not a duplicate of itself. The main dependency to be aware of is #9141 (LRU cache utility), which the PR explicitly mentions must be merged first. |
00637c0 to
71e0ba2
Compare
f1ae801 to
08fa7f7
Compare
fd45d0b to
cf1eff3
Compare
|
Rebased onto latest dev and resolved merge conflicts.\n\nChanges:\n- bootstrap.ts: Combined Snapshot/Truncate init with commandSubscription cleanup\n- plugin/index.ts, provider/provider.ts, project/instance.ts: Auto-merged successfully\n\nDepends on #9141 (LRU cache utility) - please merge that first.\n\nAll typechecks pass. Ready for review. |
6d2ca57 to
6cea219
Compare
6cea219 to
330190a
Compare
Add bounded LRU cache implementation to prevent unbounded memory growth in long-running sessions. Changes: - Create new createLruCache utility in src/util/cache.ts with: - maxEntries limit for bounded cache size - LRU eviction policy when limit is reached - Optional onEvict callback for cleanup - SDK cache: limited to 50 entries with LRU eviction - Languages cache: limited to 100 entries with LRU eviction - Instance cache: limited to 20 entries with LRU eviction These caches previously grew unbounded, contributing to 70GB+ memory usage in long-running sessions.
Add optional dispose callback to plugin Hooks interface to allow plugins to clean up resources when the Instance is disposed.
When LRU cache evicts an Instance, call State.dispose() to clean up associated resources. Prevents memory leaks from evicted instances.
…gins Ensure proper cleanup when Instance is disposed: - Call plugin dispose() hooks - Clean up bootstrap subscriptions - Properly chain disposal through all components This completes the Instance lifecycle management, preventing memory leaks from orphaned subscriptions and plugin resources.
330190a to
5a7deb5
Compare
… CPU optimization, MCP OAuth client profile workaround, Session Mind fixes, and memory leak prevention Session Mind improvements: - Publish Session.Event.Finished in prompt.ts for short session capture - Add 60s timeout + 2 retries with exponential backoff for memory extraction LLM - Add async indexing functions for immediate memory searchability - Add exponential backoff retry queue for transient indexing failures - Enable Session Mind features by default (saveMemories, enrich_compaction) - Increase context limits (max_tokens: 2000, max_memories: 5, max_sessions: 5) - Enhanced degraded mode logging (info level) - Updated OpenAPI schema defaults and regenerated SDK types Memory leak prevention (from upstream PRs anomalyco#14650, anomalyco#13186, anomalyco#12259, anomalyco#9149): - ACP session TTL (1h) and LRU eviction (max 50 sessions) with 5min cleanup - Signal handlers (SIGTERM/SIGINT/SIGHUP) for graceful shutdown in serve/worker/CLI - LSP client limit (max 20) with oldest-client eviction - Plugin dispose hook for cleanup on Instance.disposeAll() - O(1) string building: textParts[] in copilot message conversion - O(1) string building: argumentChunks[] in copilot tool call streaming - O(1) buffer management: bufferChunks[] in PTY with 2MB limit - 50ms throttled flush in session processor to batch streaming deltas - LRU cache utility for reusable memory-bounded caching Tests added (44 new tests, 2435 total pass): - ACP session manager tests (TTL expiry, LRU eviction, basic ops) - LSP client limit eviction test - Plugin dispose hook tests - PTY buffer chunk tests - Session processor flush throttling tests - LRU cache utility tests
… CPU optimization, MCP OAuth client profile workaround, Session Mind fixes, and memory leak prevention Session Mind improvements: - Publish Session.Event.Finished in prompt.ts for short session capture - Add 60s timeout + 2 retries with exponential backoff for memory extraction LLM - Add async indexing functions for immediate memory searchability - Add exponential backoff retry queue for transient indexing failures - Enable Session Mind features by default (saveMemories, enrich_compaction) - Increase context limits (max_tokens: 2000, max_memories: 5, max_sessions: 5) - Enhanced degraded mode logging (info level) - Updated OpenAPI schema defaults and regenerated SDK types Memory leak prevention (from upstream PRs anomalyco#14650, anomalyco#13186, anomalyco#12259, anomalyco#9149): - ACP session TTL (1h) and LRU eviction (max 50 sessions) with 5min cleanup - Signal handlers (SIGTERM/SIGINT/SIGHUP) for graceful shutdown in serve/worker/CLI - LSP client limit (max 20) with oldest-client eviction - Plugin dispose hook for cleanup on Instance.disposeAll() - O(1) string building: textParts[] in copilot message conversion - O(1) string building: argumentChunks[] in copilot tool call streaming - O(1) buffer management: bufferChunks[] in PTY with 2MB limit - 50ms throttled flush in session processor to batch streaming deltas - LRU cache utility for reusable memory-bounded caching Tests added (44 new tests, 2435 total pass): - ACP session manager tests (TTL expiry, LRU eviction, basic ops) - LSP client limit eviction test - Plugin dispose hook tests - PTY buffer chunk tests - Session processor flush throttling tests - LRU cache utility tests
… CPU optimization, MCP OAuth client profile workaround, Session Mind fixes, and memory leak prevention Session Mind improvements: - Publish Session.Event.Finished in prompt.ts for short session capture - Add 60s timeout + 2 retries with exponential backoff for memory extraction LLM - Add async indexing functions for immediate memory searchability - Add exponential backoff retry queue for transient indexing failures - Enable Session Mind features by default (saveMemories, enrich_compaction) - Increase context limits (max_tokens: 2000, max_memories: 5, max_sessions: 5) - Enhanced degraded mode logging (info level) - Updated OpenAPI schema defaults and regenerated SDK types Memory leak prevention (from upstream PRs anomalyco#14650, anomalyco#13186, anomalyco#12259, anomalyco#9149): - ACP session TTL (1h) and LRU eviction (max 50 sessions) with 5min cleanup - Signal handlers (SIGTERM/SIGINT/SIGHUP) for graceful shutdown in serve/worker/CLI - LSP client limit (max 20) with oldest-client eviction - Plugin dispose hook for cleanup on Instance.disposeAll() - O(1) string building: textParts[] in copilot message conversion - O(1) string building: argumentChunks[] in copilot tool call streaming - O(1) buffer management: bufferChunks[] in PTY with 2MB limit - 50ms throttled flush in session processor to batch streaming deltas - LRU cache utility for reusable memory-bounded caching Tests added (44 new tests, 2435 total pass): - ACP session manager tests (TTL expiry, LRU eviction, basic ops) - LSP client limit eviction test - Plugin dispose hook tests - PTY buffer chunk tests - Session processor flush throttling tests - LRU cache utility tests
… CPU optimization, MCP OAuth client profile workaround, Session Mind fixes, and memory leak prevention Session Mind improvements: - Publish Session.Event.Finished in prompt.ts for short session capture - Add 60s timeout + 2 retries with exponential backoff for memory extraction LLM - Add async indexing functions for immediate memory searchability - Add exponential backoff retry queue for transient indexing failures - Enable Session Mind features by default (saveMemories, enrich_compaction) - Increase context limits (max_tokens: 2000, max_memories: 5, max_sessions: 5) - Enhanced degraded mode logging (info level) - Updated OpenAPI schema defaults and regenerated SDK types Memory leak prevention (from upstream PRs anomalyco#14650, anomalyco#13186, anomalyco#12259, anomalyco#9149): - ACP session TTL (1h) and LRU eviction (max 50 sessions) with 5min cleanup - Signal handlers (SIGTERM/SIGINT/SIGHUP) for graceful shutdown in serve/worker/CLI - LSP client limit (max 20) with oldest-client eviction - Plugin dispose hook for cleanup on Instance.disposeAll() - O(1) string building: textParts[] in copilot message conversion - O(1) string building: argumentChunks[] in copilot tool call streaming - O(1) buffer management: bufferChunks[] in PTY with 2MB limit - 50ms throttled flush in session processor to batch streaming deltas - LRU cache utility for reusable memory-bounded caching Tests added (44 new tests, 2435 total pass): - ACP session manager tests (TTL expiry, LRU eviction, basic ops) - LSP client limit eviction test - Plugin dispose hook tests - PTY buffer chunk tests - Session processor flush throttling tests - LRU cache utility tests
… CPU optimization, MCP OAuth client profile workaround, Session Mind fixes, and memory leak prevention Session Mind improvements: - Publish Session.Event.Finished in prompt.ts for short session capture - Add 60s timeout + 2 retries with exponential backoff for memory extraction LLM - Add async indexing functions for immediate memory searchability - Add exponential backoff retry queue for transient indexing failures - Enable Session Mind features by default (saveMemories, enrich_compaction) - Increase context limits (max_tokens: 2000, max_memories: 5, max_sessions: 5) - Enhanced degraded mode logging (info level) - Updated OpenAPI schema defaults and regenerated SDK types Memory leak prevention (from upstream PRs anomalyco#14650, anomalyco#13186, anomalyco#12259, anomalyco#9149): - ACP session TTL (1h) and LRU eviction (max 50 sessions) with 5min cleanup - Signal handlers (SIGTERM/SIGINT/SIGHUP) for graceful shutdown in serve/worker/CLI - LSP client limit (max 20) with oldest-client eviction - Plugin dispose hook for cleanup on Instance.disposeAll() - O(1) string building: textParts[] in copilot message conversion - O(1) string building: argumentChunks[] in copilot tool call streaming - O(1) buffer management: bufferChunks[] in PTY with 2MB limit - 50ms throttled flush in session processor to batch streaming deltas - LRU cache utility for reusable memory-bounded caching Tests added (44 new tests, 2435 total pass): - ACP session manager tests (TTL expiry, LRU eviction, basic ops) - LSP client limit eviction test - Plugin dispose hook tests - PTY buffer chunk tests - Session processor flush throttling tests - LRU cache utility tests
… CPU optimization, MCP OAuth client profile workaround, Session Mind fixes, and memory leak prevention Session Mind improvements: - Publish Session.Event.Finished in prompt.ts for short session capture - Add 60s timeout + 2 retries with exponential backoff for memory extraction LLM - Add async indexing functions for immediate memory searchability - Add exponential backoff retry queue for transient indexing failures - Enable Session Mind features by default (saveMemories, enrich_compaction) - Increase context limits (max_tokens: 2000, max_memories: 5, max_sessions: 5) - Enhanced degraded mode logging (info level) - Updated OpenAPI schema defaults and regenerated SDK types Memory leak prevention (from upstream PRs anomalyco#14650, anomalyco#13186, anomalyco#12259, anomalyco#9149): - ACP session TTL (1h) and LRU eviction (max 50 sessions) with 5min cleanup - Signal handlers (SIGTERM/SIGINT/SIGHUP) for graceful shutdown in serve/worker/CLI - LSP client limit (max 20) with oldest-client eviction - Plugin dispose hook for cleanup on Instance.disposeAll() - O(1) string building: textParts[] in copilot message conversion - O(1) string building: argumentChunks[] in copilot tool call streaming - O(1) buffer management: bufferChunks[] in PTY with 2MB limit - 50ms throttled flush in session processor to batch streaming deltas - LRU cache utility for reusable memory-bounded caching Tests added (44 new tests, 2435 total pass): - ACP session manager tests (TTL expiry, LRU eviction, basic ops) - LSP client limit eviction test - Plugin dispose hook tests - PTY buffer chunk tests - Session processor flush throttling tests - LRU cache utility tests
… CPU optimization, MCP OAuth client profile workaround, Session Mind fixes, and memory leak prevention Session Mind improvements: - Publish Session.Event.Finished in prompt.ts for short session capture - Add 60s timeout + 2 retries with exponential backoff for memory extraction LLM - Add async indexing functions for immediate memory searchability - Add exponential backoff retry queue for transient indexing failures - Enable Session Mind features by default (saveMemories, enrich_compaction) - Increase context limits (max_tokens: 2000, max_memories: 5, max_sessions: 5) - Enhanced degraded mode logging (info level) - Updated OpenAPI schema defaults and regenerated SDK types Memory leak prevention (from upstream PRs anomalyco#14650, anomalyco#13186, anomalyco#12259, anomalyco#9149): - ACP session TTL (1h) and LRU eviction (max 50 sessions) with 5min cleanup - Signal handlers (SIGTERM/SIGINT/SIGHUP) for graceful shutdown in serve/worker/CLI - LSP client limit (max 20) with oldest-client eviction - Plugin dispose hook for cleanup on Instance.disposeAll() - O(1) string building: textParts[] in copilot message conversion - O(1) string building: argumentChunks[] in copilot tool call streaming - O(1) buffer management: bufferChunks[] in PTY with 2MB limit - 50ms throttled flush in session processor to batch streaming deltas - LRU cache utility for reusable memory-bounded caching Tests added (44 new tests, 2435 total pass): - ACP session manager tests (TTL expiry, LRU eviction, basic ops) - LSP client limit eviction test - Plugin dispose hook tests - PTY buffer chunk tests - Session processor flush throttling tests - LRU cache utility tests
… CPU optimization, MCP OAuth client profile workaround, Session Mind fixes, and memory leak prevention Session Mind improvements: - Publish Session.Event.Finished in prompt.ts for short session capture - Add 60s timeout + 2 retries with exponential backoff for memory extraction LLM - Add async indexing functions for immediate memory searchability - Add exponential backoff retry queue for transient indexing failures - Enable Session Mind features by default (saveMemories, enrich_compaction) - Increase context limits (max_tokens: 2000, max_memories: 5, max_sessions: 5) - Enhanced degraded mode logging (info level) - Updated OpenAPI schema defaults and regenerated SDK types Memory leak prevention (from upstream PRs anomalyco#14650, anomalyco#13186, anomalyco#12259, anomalyco#9149): - ACP session TTL (1h) and LRU eviction (max 50 sessions) with 5min cleanup - Signal handlers (SIGTERM/SIGINT/SIGHUP) for graceful shutdown in serve/worker/CLI - LSP client limit (max 20) with oldest-client eviction - Plugin dispose hook for cleanup on Instance.disposeAll() - O(1) string building: textParts[] in copilot message conversion - O(1) string building: argumentChunks[] in copilot tool call streaming - O(1) buffer management: bufferChunks[] in PTY with 2MB limit - 50ms throttled flush in session processor to batch streaming deltas - LRU cache utility for reusable memory-bounded caching Tests added (44 new tests, 2435 total pass): - ACP session manager tests (TTL expiry, LRU eviction, basic ops) - LSP client limit eviction test - Plugin dispose hook tests - PTY buffer chunk tests - Session processor flush throttling tests - LRU cache utility tests
… CPU optimization, MCP OAuth client profile workaround, Session Mind fixes, and memory leak prevention Session Mind improvements: - Publish Session.Event.Finished in prompt.ts for short session capture - Add 60s timeout + 2 retries with exponential backoff for memory extraction LLM - Add async indexing functions for immediate memory searchability - Add exponential backoff retry queue for transient indexing failures - Enable Session Mind features by default (saveMemories, enrich_compaction) - Increase context limits (max_tokens: 2000, max_memories: 5, max_sessions: 5) - Enhanced degraded mode logging (info level) - Updated OpenAPI schema defaults and regenerated SDK types Memory leak prevention (from upstream PRs anomalyco#14650, anomalyco#13186, anomalyco#12259, anomalyco#9149): - ACP session TTL (1h) and LRU eviction (max 50 sessions) with 5min cleanup - Signal handlers (SIGTERM/SIGINT/SIGHUP) for graceful shutdown in serve/worker/CLI - LSP client limit (max 20) with oldest-client eviction - Plugin dispose hook for cleanup on Instance.disposeAll() - O(1) string building: textParts[] in copilot message conversion - O(1) string building: argumentChunks[] in copilot tool call streaming - O(1) buffer management: bufferChunks[] in PTY with 2MB limit - 50ms throttled flush in session processor to batch streaming deltas - LRU cache utility for reusable memory-bounded caching Tests added (44 new tests, 2435 total pass): - ACP session manager tests (TTL expiry, LRU eviction, basic ops) - LSP client limit eviction test - Plugin dispose hook tests - PTY buffer chunk tests - Session processor flush throttling tests - LRU cache utility tests
Summary
Complete the Instance disposal chain to ensure plugins and cached resources are properly cleaned up when instances are disposed.
Fixes #9157
Depends on #9141 (LRU cache utility)
Problem
When
Instance.dispose()is called:Solution
1. Plugin Dispose Hook
Add
dispose()method to plugin Hooks interface.2. LRU Cache onEvict Callback
Use the LRU cache's
onEvictcallback to dispose instances when evicted.3. Bootstrap Cleanup
Add cleanup in bootstrap to dispose plugins before process exit.
Changes
packages/plugin/src/index.ts- Adddispose()to Hooks interfacepackages/opencode/src/plugin/index.ts- Call plugin dispose() on instance disposalpackages/opencode/src/project/instance.ts- Add onEvict callback for cache evictionpackages/opencode/src/project/bootstrap.ts- Add cleanup for plugins on exitpackages/opencode/src/provider/provider.ts- Use LRU cache for SDK instancespackages/opencode/src/util/cache.ts- Add iterator support (from feat(util): Add bounded LRU cache utility #9141)Testing
bun turbo typecheck)Note: Manual plugin disposal testing was not performed. This requires creating a test plugin with cleanup logic and verifying it's called.
Stacking
This PR includes commits from #9141 (LRU cache utility). After #9141 is merged, this PR can be rebased to only include the disposal-specific changes.