opencode icon indicating copy to clipboard operation
opencode copied to clipboard

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

Open sauerdaniel opened this issue 17 hours ago • 1 comments

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

sauerdaniel avatar Jan 17 '26 23:01 sauerdaniel