Skip to content

Fix CLI dropping connection instead of reconnecting mid-session - #1

Merged
grlee merged 2 commits into
mainfrom
fix/connection-reliability
Jun 30, 2026
Merged

grlee merged 2 commits into
mainfrom
fix/connection-reliability

Conversation

@grlee

@grlee grlee commented Jun 22, 2026

Copy link
Copy Markdown
Owner

Problem

imcp-server regularly drops out of Claude Code mid-session and only comes back after a manual restart. Root cause: MCPService.run() had its two stop paths inverted.

  • A dropped network connection (.connectionClosed, or NWError 54/57) called return — killing the relay loop, so the MCP client saw the server disconnect permanently.
  • stdin EOF (the client legitimately closing us) fell through and looped back to reconnect.

So the one case that should recover (transient network drop: app restart, sleep/wake, Bonjour blip) was the one that gave up.

Fix

Swap the behavior, routed through a pure, unit-tested policy:

  • Network drop → reconnect (1s backoff).
  • stdin EOF → terminate cleanly.

The decision lives in a new CLI/ReconnectPolicy.swift (ProxyOutcome → ReconnectDecision), extracted so it can be unit-tested. The test target can't import the executable's top-level code, so the file is dual-membered into both the CLI and test targets (the CLI target auto-includes it via its synchronized folder group).

Also in this PR

Tests

New CLITests/ReconnectPolicyTests.swift (TDD: watched it fail red against a stub, then green):

  • connectionDropped → reconnect
  • networkTimedOut → reconnect
  • stdinClosed → terminate

Verification: iMCP build ✅ · imcp-serverTests 6/6 ✅ · swift format lint --strict ✅

🤖 Generated with Claude Code

`MCPService.run()` had its two stop paths inverted: a dropped *network*
connection (`.connectionClosed` / NWError 54·57) called `return`, killing
the relay loop so the MCP client saw the server disconnect and had to be
restarted. Meanwhile stdin EOF — the client legitimately closing us —
fell through and looped back to reconnect.

Swap the behavior: a dropped network connection now reconnects (the app
may have restarted, the machine slept, or Bonjour blipped), and stdin EOF
now terminates cleanly. The decision is extracted into a pure
`reconnectDecision(for:)` policy in ReconnectPolicy.swift so the mapping
is unit-tested (the test target can't import the executable's top-level
code, so the file is dual-membered into both targets).

Also:
- Cancel the NWBrowser when discovery times out, releasing its socket/FD
  before the loop creates a fresh one on retry (upstream mattt#174).
- Shorten the discovery timeout 30s -> 10s so a missing app fails faster
  than the MCP client's own initialize timeout; the reconnect loop retries.
- Remove the dead top-level NWBrowser (created, never started or used;
  MCPService creates its own).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Claude-Session: https://claude.ai/code/session_01XsyoHa1VekPHDCoxnURFcV
Follow-up to the connection-reliability fix, resolving findings from the
multi-agent PR review:

- Log mid-session drops at .warning (the release log level) and include the
  underlying error, so a drop — and especially a persistent reconnect loop —
  is visible in shipped builds. Previously logged at .debug/.info, invisible
  in release.
- Extract the error→ProxyOutcome classification into a pure, unit-tested
  proxyOutcome(for:) in ReconnectPolicy.swift, and move StdioProxyError there
  so the test target can see it. Adds tests for the exact regression line
  (.connectionClosed → .connectionDropped) that the prior tests left uncovered.
- Replace the 54/57 magic numbers with NWError .posix(.ECONNRESET/.ENOTCONN),
  and model the reconnect delay as Duration instead of a bare Double seconds.

12/12 tests pass; strict lint and full iMCP build clean.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Claude-Session: https://claude.ai/code/session_01XsyoHa1VekPHDCoxnURFcV
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant