fix(mcp): Fix memory leaks in OAuth transport and process cleanup #9145
+174
−110
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Add proper cleanup for MCP (Model Context Protocol) OAuth transports and process lifecycle management to prevent resource leaks.
Fixes #9153
Relates to #5363
Problem
When MCP OAuth flows are initiated, transports stored in
pendingOAuthTransportsMap are never closed when:removeAuth()deletes the map entry but doesn't close the transportSolution
Add proper transport lifecycle management:
closeTransport()helper function to properly close transportssetPendingOAuthTransport()that closes old transports before setting new onesremoveAuth()before deletionChanges
packages/opencode/src/mcp/index.ts- Add transport cleanup helpers and update OAuth flowpackages/opencode/src/index.ts- Add signal handlers and main() wrapper with cleanupTesting
bun turbo typecheck)Note: Manual MCP OAuth flow and process cleanup testing was not performed.