Skip to content

Does iMCP possibly leak an NWBrowser on each discovery timeout, and therefore cause mDNSResponder to run out of file descriptors? #174

Closed

Description

@jerzygangi

Hey @mattt,

Looking at CLI/main.swift around lines 494-505, there's this block:

// Set up a timeout task to ensure we don't wait forever
let timeoutTask = Task {
    // Allow 30 seconds to find the service
    try await Task.sleep(for: .seconds(30))

    // If we haven't found a service by now, resume with an error
    if await connectionState.checkAndSetResumed() {
        await log.error("Bonjour service discovery timed out after 30 seconds")
        continuation.resume(
            throwing: MCPError.internalError("Service discovery timeout")
        )
    }
}

Wouldn't this mean that when NWBrowser doesn't find a service within 30 secs, that instance is never cancelled?

My understanding is that each started NWBrowser keeps a socket open to mDNSResponder. So, basically if you never cancel them, eventually they'll add up and cause mDNSResponder to hit whatever its internal file descriptor limit is.

-JJ

Activity

  1. ApptivistsKafe commented on Jun 26, 2026

    @ApptivistsKafe

    It sure does. Days of troubleshooting why my DNS keeps getting screwed up machine-wide resulted in this:

    Title: imcp-server leaks DNS-SD socket connections, exhausting mDNSResponder and breaking system-wide DNS

    Summary

    imcp-server steadily leaks unix-domain socket connections to macOS's mDNSResponder (the system DNS/Bonjour daemon) — apparently one per resolution/network operation, never released. Over ~18h of uptime it accumulated ~10,200 open connections, exhausting mDNSResponder's file-descriptor limit (~10,240). Once exhausted, all system DNS resolution fails machine-wide until mDNSResponder is force-restarted.

    Environment

    iMCP version: (defaults read /Applications/iMCP.app/Contents/Info CFBundleShortVersionString)
    macOS Darwin 25.4.0; iMCP running as an MCP server
    Impact: High. A single leaking helper silently breaks DNS for the whole machine, recoverable only via sudo killall mDNSResponder. It's hard to diagnose because apps with their own resolver (Chrome's DoH, dig) keep working while everything using the system resolver fails.

    Evidence

    With DNS dead, mDNSResponder had 10,254 FDs, 10,227 of them unix sockets (DNS-SD client connections). Tracing those to their owner points straight at imcp-server — a near-exact match:

    $ sudo lsof -U +c0 | awk 'NR>1{print $1}' | sort | uniq -c | sort -rn | head
    10227 mDNSResponder
    10224 imcp-server ← peer ends of the leaked connections
    236 node
    Concurrent: mDNSResponder logging open() failed: Too many open files continuously; dns-sd -G → -65563 (ServiceNotRunning). Leak rate is steady (~9/min) regardless of activity — consistent with a per-operation handle that's created but never deallocated.

    Suspected cause: A DNSServiceRef (DNSServiceGetAddrInfo/Browse) or NWConnection/NWBrowser/NWPathMonitor created per operation but never DNSServiceRefDeallocate'd / cancel()ed. Each opens a unix socket to mDNSResponder; not tearing it down leaks one per call.

    Confirm: Let it run a few hours and watch the count climb without bound:

    sudo lsof -U +c0 | awk '$1=="imcp-server"' | wc -l
    Workaround: Restart iMCP to release the sockets, then sudo killall mDNSResponder.

  2. jerzygangi commented on Jun 27, 2026

    @jerzygangi
    Author

    Thx for sharing that @ApptivistsKafe. It looks like @grlee is attempting to fix this bug in grlee#1. I don't feel qualified to review that pr, but hopefully @mattt will take a look at it soon.

  3. added a commit that references this issue on Sep 2, 2026
    6f90737
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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