Memory leak: MCP clients not closed before reassignment #5386

Closed
opened 2026-02-16 17:52:05 -05:00 by yindo · 0 comments
Owner

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

Originally assigned to: @rekram1-node on GitHub.

Description

When MCP clients are reassigned in MCP.add() or during OAuth finishAuth(), the existing client at that key is silently overwritten without being closed first.

Impact

  • Open connections/sockets are orphaned
  • Active event listeners accumulate
  • Resources are never released
  • Contributes to memory growth in long-running sessions

Location

packages/opencode/src/mcp/index.ts - two locations where s.clients[name] = result.mcpClient overwrites existing clients

Reproduction

  1. Connect to an MCP server
  2. Reconnect or complete OAuth flow
  3. Previous client is leaked

Related

Part of #3013 (general memory issues)
Related to #7261 (MCP orphan processes)

Originally created by @sauerdaniel on GitHub (Jan 13, 2026). Originally assigned to: @rekram1-node on GitHub. ## Description When MCP clients are reassigned in `MCP.add()` or during OAuth `finishAuth()`, the existing client at that key is silently overwritten without being closed first. ## Impact - Open connections/sockets are orphaned - Active event listeners accumulate - Resources are never released - Contributes to memory growth in long-running sessions ## Location `packages/opencode/src/mcp/index.ts` - two locations where `s.clients[name] = result.mcpClient` overwrites existing clients ## Reproduction 1. Connect to an MCP server 2. Reconnect or complete OAuth flow 3. Previous client is leaked ## Related Part of #3013 (general memory issues) Related to #7261 (MCP orphan processes)
yindo added the perf label 2026-02-16 17:52:05 -05:00
yindo closed this issue 2026-02-16 17:52:05 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#5386