Skip to content

Race condition: 'PORT IS IN USE' error caused by Inspector's own process #1090

@btbowman

Description

@btbowman

Bug Description

When running mcp dev server.py, the Inspector reports PORT IS IN USE at http://localhost:6274 even when the port is confirmed free before launch. The port check appears to run after the Inspector has already bound the port, causing it to detect its own process.

Steps to Reproduce

  1. Confirm port 6274 is free: lsof -i :6274 returns nothing
  2. Run mcp dev server.py
  3. Inspector starts the proxy on port 6277 successfully
  4. Inspector then reports port 6274 is in use and fails

Actual Output

⚙️ Proxy server listening on localhost:6277
🔑 Session token: <token>
   Use this token to authenticate requests or set DANGEROUSLY_OMIT_AUTH=true to disable auth
❌  MCP Inspector PORT IS IN USE at http://localhost:6274 ❌

Expected Behavior

The port availability check should run before binding/starting the server on that port, not after. Currently the startup sequence appears to be:

  1. Start proxy server (port 6277) ✅
  2. Start Inspector server (binds port 6274)
  3. Check if port 6274 is available ← detects its own process, fails

The correct sequence should be:

  1. Check if port 6274 is available ✅
  2. Check if port 6277 is available ✅
  3. Start proxy server (port 6277)
  4. Start Inspector server (port 6274)

Root Cause Analysis

This is a race condition. The port availability check (net.createServer or equivalent) runs concurrently with or after the actual server bind. When the check fires, the port is already occupied by the Inspector's own Node.js process.

Additionally, after killing the process with Ctrl+C, the port sometimes remains in CLOSE_WAIT state, requiring kill -9 of the stale Node process to reclaim it. This suggests the graceful shutdown handler may not be properly closing all sockets.

Environment

  • MCP SDK: 1.26.0 (Python)
  • OS: macOS (Sequoia)
  • Python: 3.12
  • Node: (used internally by Inspector)

Workarounds

  • Use --port flag to specify a different port: mcp dev server.py --port 6280
  • Kill stale processes before launch: lsof -ti:6274 | xargs kill -9 2>/dev/null

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions