Skip to content

Cancel the Bonjour browser on every discovery exit path - #199

Merged
mattt merged 2 commits into
mainfrom
fix/cli-browser-leak
Sep 2, 2026
Merged

mattt merged 2 commits into
mainfrom
fix/cli-browser-leak

Conversation

@mattt

@mattt mattt commented Sep 2, 2026

Copy link
Copy Markdown
Owner

Fixes #192. Fixes #174.

imcp-server retries discovery in a loop, creating a new NWBrowser each time. The 30-second timeout path never cancelled the previous browser (only the found and failed paths did), so every timed-out attempt left a DNSServiceBrowse connection to mDNSResponder open. With the app not running, that leaked one connection every ~35 seconds, which matches the cadence in #192, until the daemon ran out of descriptors and DNS failed machine-wide.

Discovery now lives in BonjourDiscovery.discoverEndpoint, which cancels the browser in a defer on every exit. The file is compiled into the CLI test target too, so there's a regression test against a real NWBrowser that fails without the cancel. Also removes a module-level browser that was never started and a write-only property.

imcp-server retries discovery in a loop, creating a new NWBrowser each
time. The 30-second timeout path never cancelled the previous browser,
so every timed-out attempt left a DNSServiceBrowse connection to
mDNSResponder open. On a machine where the app wasn't running, that
leaked one connection every ~35 seconds until the daemon ran out of
descriptors and DNS failed for every process.

Move discovery into BonjourDiscovery.discoverEndpoint, which cancels the
browser in a defer, and share the file with the CLI test target so the
behavior is covered by a test against a real NWBrowser. Also drop a
module-level browser that was never started and a write-only property.

Fixes #192. Fixes #174.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The regression test uses an invalid Bonjour type and accepts unrelated errors, so it may pass without exercising the timeout path.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

Moves Bonjour discovery into a helper that reliably cancels each browser, preventing DNS-SD handle leaks.

Changes:

  • Centralizes discovery and cleanup.
  • Adds timeout cancellation regression coverage.
  • Removes unused browser state.
File summaries
File Description
CLI/BonjourDiscovery.swift Adds cancellation-safe discovery helper.
CLI/main.swift Uses the helper during retries.
CLITests/BonjourDiscoveryTests.swift Tests timeout cleanup.
iMCP.xcodeproj/project.pbxproj Adds helper and test to the test target.
Review details

Suppressed comments (1)

CLITests/BonjourDiscoveryTests.swift:30

  • This accepts every browser error as the expected timeout, so an immediate setup/network failure makes the regression test pass without exercising the leaked timeout path. Catch only BonjourDiscovery.Error.timeout and allow other errors to fail the throwing test.
        } catch {
            // Expected.
        }
  • Files reviewed: 4/4 changed files
  • Comments generated: 1
  • Review effort level: Balanced

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread CLITests/BonjourDiscoveryTests.swift Resolved

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The cleanup reliably covers timeout, success, and browser-failure paths with focused regression coverage.

Review details
  • Files reviewed: 4/4 changed files
  • Comments generated: 0 new
  • Review effort level: Balanced

@mattt
mattt merged commit 6f90737 into main Sep 2, 2026
2 checks passed
@mattt
mattt deleted the fix/cli-browser-leak branch September 2, 2026 19:25
toonvl added a commit to toonvl/iMCP that referenced this pull request Sep 2, 2026
Brings in the six upstream commits behind #2: the SIGTRAP
continuation fix (mattt#151), the Bonjour browser cleanup (mattt#199), the approval
setup-timeout exemption (mattt#200), the events_fetch date-only fix (mattt#175),
Madrid 0.5.0 with FetchRequest<Message> (mattt#201), and message read status
(mattt#195).

Three files conflicted, all because upstream shipped its own version of a
fix this fork already carried:

- CLI/main.swift: took upstream. Its mattt#199 supersedes our inline
  browser.cancel() (our closed upstream PR mattt#196) by extracting
  BonjourDiscovery.discoverEndpoint, which cancels on every exit path and
  comes with tests.
- App/Extensions/Foundation+Extensions.swift: took upstream. Its
  `count > 10` guard on the timezone-suffix regex is equivalent to our
  `!isDateOnlyISO8601String(...)`; staying identical to upstream is worth
  more than the marginal precision.
- App/Services/Calendar.swift: kept ours. Upstream only annotates
  `identifier` on the events_fetch and events_create returns, which our
  private encode(_:) helper already does alongside organizedByMe,
  organizer, isRecurring and coordinates.

Co-Authored-By: Claude Opus 5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants