Skip to content

Race condition in McpClientManager creates duplicate MCP processes #3817

@thedoctormes-hue

Description

@thedoctormes-hue

Problem

When Qwen Code restarts or reinitializes MCP servers, a race condition occurs that creates duplicate MCP processes instead of replacing them.

Root Cause Analysis

In McpClientManager.discoverMcpToolsForServer():

async discoverMcpToolsForServer(serverName, cliConfig) {
  const existingClient = this.clients.get(serverName);
  if (existingClient) {
    await existingClient.disconnect();
    this.clients.delete(serverName);
  }
  const client = new McpClient(...);
  this.clients.set(serverName, client);
  await client.connect();
}

Race condition window: Between disconnect() and connect(), the old MCP process is still running.

Evidence

kotolizator (PID 30400) → ACP (30452) → 2 MCP processes (30477, 30513) created 21 seconds apart

Environment

Qwen Code v0.15.6, Node.js v22.22.1, Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions