Skip to content

Conversation

@sauerdaniel
Copy link
Contributor

@sauerdaniel sauerdaniel commented Jan 17, 2026

Summary

Add proper cleanup for ACP (Agent Communication Protocol) sessions to prevent resource leaks from orphaned sessions.

Fixes #9154
Relates to #5363

Problem

When ACP sessions are created:

  1. Sessions may not be properly cleaned up when the parent process ends
  2. Event listeners may not be removed
  3. Orphaned sessions can accumulate

Solution

Add proper session lifecycle management:

  • Track active sessions
  • Clean up sessions on process exit
  • Remove event listeners properly

Changes

  • packages/opencode/src/acp/session.ts - Add session cleanup logic

Testing

  • TypeScript compilation passes (bun turbo typecheck)
  • Unit tests pass (725 tests, 0 failures)

Note: Manual ACP session cleanup testing was not performed.

@github-actions
Copy link
Contributor

Thanks for your contribution!

This PR doesn't have a linked issue. All PRs must reference an existing issue.

Please:

  1. Open an issue describing the bug/feature (if one doesn't exist)
  2. Add Fixes #<number> or Closes #<number> to this PR description

See CONTRIBUTING.md for details.

@github-actions
Copy link
Contributor

The following comment was made by an LLM, it may be inaccurate:

No duplicate PRs found

Add cleanup mechanism for ACP session map and event streams to prevent
unbounded growth. Implements proper session lifecycle management.
@sauerdaniel
Copy link
Contributor Author

Rebased onto latest dev and resolved merge conflicts.\n\nChanges adapted to the current architecture:\n- Added delete() and clear() methods to ACPSessionManager\n- Added dispose() method to Agent (aborts global event stream, clears sessions)\n- Added closeSession() method for per-session cleanup\n- Added handler for session.deleted event to auto-cleanup sessions\n- Removed per-session abortController from types (using global eventAbort pattern)\n\nAll typechecks pass. Ready for review.

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.

Memory leak: ACP session event streams never aborted

1 participant