Skip to content

Memory leak: MCP clients not closed before reassignment #8257

@sauerdaniel

Description

@sauerdaniel

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)

Metadata

Metadata

Assignees

Labels

perfIndicates a performance issue or need for optimization

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions