Memory leak: MCP OAuth transports not closed on retry or cancellation #6633

Open
opened 2026-02-16 18:04:49 -05:00 by yindo · 1 comment
Owner

Originally created by @sauerdaniel on GitHub (Jan 17, 2026).

Originally assigned to: @rekram1-node on GitHub.

Problem

When MCP OAuth flows are initiated, transports are stored in pendingOAuthTransports Map. These transports are never closed when:

  1. OAuth retry - A new transport is created for the same server, orphaning the previous one
  2. OAuth cancellation - removeAuth() deletes the map entry but doesn't close the transport
  3. OAuth timeout - Incomplete flows leave open transports

Additionally, when the main OpenCode process terminates, spawned MCP server processes may continue running as orphans.

Code Location

packages/opencode/src/mcp/index.ts

Impact

  • Open connections accumulate over OAuth retries
  • Orphaned MCP server processes after OpenCode exits
  • Memory growth in long-running sessions

Relates to #5363

Expected Behavior

  • Old transports should be closed before creating new ones
  • removeAuth() should close transport before deletion
  • Process exit should clean up all MCP clients
Originally created by @sauerdaniel on GitHub (Jan 17, 2026). Originally assigned to: @rekram1-node on GitHub. ## Problem When MCP OAuth flows are initiated, transports are stored in `pendingOAuthTransports` Map. These transports are never closed when: 1. **OAuth retry** - A new transport is created for the same server, orphaning the previous one 2. **OAuth cancellation** - `removeAuth()` deletes the map entry but doesn't close the transport 3. **OAuth timeout** - Incomplete flows leave open transports Additionally, when the main OpenCode process terminates, spawned MCP server processes may continue running as orphans. ### Code Location `packages/opencode/src/mcp/index.ts` ### Impact - Open connections accumulate over OAuth retries - Orphaned MCP server processes after OpenCode exits - Memory growth in long-running sessions Relates to #5363 ## Expected Behavior - Old transports should be closed before creating new ones - `removeAuth()` should close transport before deletion - Process exit should clean up all MCP clients
yindo added the perf label 2026-02-16 18:04:49 -05:00
Author
Owner

@github-actions[bot] commented on GitHub (Jan 17, 2026):

This issue might be a duplicate of existing issues. Please check:

  • #6633: [Bug] MCP processes not terminated after session ends
  • #7261: v1.1.6 still has memory bloat - heap not released + MCP orphan processes

Both of these issues describe similar symptoms of orphaned MCP server processes and memory leaks that appear directly related to the transport cleanup problems described here.

Feel free to ignore if none of these address your specific case.

@github-actions[bot] commented on GitHub (Jan 17, 2026): This issue might be a duplicate of existing issues. Please check: - #6633: [Bug] MCP processes not terminated after session ends - #7261: v1.1.6 still has memory bloat - heap not released + MCP orphan processes Both of these issues describe similar symptoms of orphaned MCP server processes and memory leaks that appear directly related to the transport cleanup problems described here. Feel free to ignore if none of these address your specific case.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#6633