Skip to content

macOS: avoid using stale tunnel interfaces on reconnect - #9080

Open
atavism wants to merge 1 commit into
mainfrom
atavism/issue-3781
Open

atavism wants to merge 1 commit into
mainfrom
atavism/issue-3781

Conversation

@atavism

@atavism atavism commented Sep 18, 2026

Copy link
Copy Markdown
Contributor

A failed tunnel start can leave an old utun descriptor in the extension process. A later connection could select that descriptor and appear connected without passing traffic.

Match fallback descriptors to the IPv4/IPv6 addresses configured for the tunnel, instead of relying on descriptor order. If no interface matches, or multiple interfaces match, fail startup rather than risk using the wrong tunnel. Descriptor ownership remains with NetworkExtension.

Adds regression coverage for stale and reused descriptors, ambiguous matches, duplicate handles, and equivalent IPv6 addresses.

Refs getlantern/engineering#3781

Summary by CodeRabbit

  • Bug Fixes

    • Improved macOS tunnel descriptor selection to reliably match the active network interface and configured IPv4/IPv6 addresses.
    • Prevents stale or incorrect tunnel interfaces from being selected during tunnel startup.
    • Added handling for missing, invalid, unmatched, or ambiguous interface address configurations.
  • Tests

    • Added coverage for descriptor reuse, interface matching, IPv4/IPv6 comparisons, and error conditions.

Copilot AI lite review requested due to automatic review settings September 18, 2026 17:11
@coderabbitai

coderabbitai Bot commented Sep 18, 2026

Copy link
Copy Markdown

Review Change StackReview Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Advanced

Run ID: fca6db13-5770-4a36-8cd5-277952ee9d1d

📥 Commits

Reviewing files that changed from the base of the PR and between c55d9ae and 56e6e5c.

📒 Files selected for processing (5)
  • macos/PacketTunnel/SingBox/ExtensionPlatformInterface.swift
  • macos/PacketTunnel/SingBox/ExtensionProvider.swift
  • macos/PacketTunnel/SingBox/TunnelFileDescriptor.swift
  • macos/Runner.xcodeproj/project.pbxproj
  • macos/RunnerTests/TunnelFileDescriptorTests.swift

Included review availability: Your plan provides up to 10 included reviews per hour; 8 remain after this review.


📝 Walkthrough

Walkthrough

The macOS packet tunnel now resolves fallback utun descriptors by matching configured IPv4 and IPv6 addresses. The change adds descriptor resolution, error handling, target wiring, and tests for matching, ambiguity, duplicates, and invalid addresses.

Changes

Tunnel descriptor resolution

Layer / File(s) Summary
Address-based descriptor resolver
macos/PacketTunnel/SingBox/TunnelFileDescriptor.swift
Adds utun descriptor discovery, interface address collection, address normalization, candidate selection, and resolution errors.
Tunnel startup integration
macos/PacketTunnel/SingBox/ExtensionPlatformInterface.swift, macos/PacketTunnel/SingBox/ExtensionProvider.swift, macos/Runner.xcodeproj/project.pbxproj
openTunSync resolves the descriptor from configured addresses. The new source files are added to the packet tunnel and test targets. The startup comment is shortened without changing behavior.
Resolver test coverage
macos/RunnerTests/TunnelFileDescriptorTests.swift
Tests validate interface matching, descriptor reuse, duplicate candidates, missing addresses, ambiguity, IPv6 comparison, and invalid input errors.

Priority: ➖ Normal

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix · Severity of issue fixed: Medium

Sequence Diagram(s)

sequenceDiagram
  participant openTunSync
  participant TunnelFileDescriptor
  participant NetworkInterfaces
  openTunSync->>TunnelFileDescriptor: resolve configured addresses
  TunnelFileDescriptor->>NetworkInterfaces: inspect utun descriptors and active addresses
  NetworkInterfaces-->>TunnelFileDescriptor: matching candidates
  TunnelFileDescriptor-->>openTunSync: selected descriptor and interface name
Loading

Suggested reviewers: myleshorton

Merge Risk: ⚪ Minimal · up to 56e6e

The fallback selects the tunnel descriptor using the configured tunnel addresses and fails safely when resolution cannot identify one.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 5.88% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 17 functions across 4 files. (1 skipped: 1… Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: preventing stale tunnel interface selection during macOS reconnects.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Full details: Docstring Coverage

Explanation

Docstring coverage is 5.88% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 17 functions across 4 files. (1 skipped: 1 unsupported.)

  • Fix all pre-merge checks with AI
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR

Comment @coderabbitai help to get the list of available commands.

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

Critical issues currently prevent compilation and break the non-auto-route startup path.

Get a fresh assessment by requesting another Copilot review.

Pull request overview

Updates macOS tunnel descriptor selection to match configured IPv4/IPv6 addresses, preventing stale or ambiguous interfaces from being used.

Changes:

  • Added address-based descriptor resolution.
  • Added regression tests for stale, duplicate, and ambiguous descriptors.
  • Integrated the resolver and tests into the Xcode project.
  • Updated tunnel replacement documentation.
File summaries
File Description
macos/RunnerTests/TunnelFileDescriptorTests.swift Adds descriptor-selection regression tests.
macos/Runner.xcodeproj/project.pbxproj Registers implementation and test files.
macos/PacketTunnel/SingBox/TunnelFileDescriptor.swift Implements address-based interface matching.
macos/PacketTunnel/SingBox/ExtensionProvider.swift Updates tunnel replacement documentation.
macos/PacketTunnel/SingBox/ExtensionPlatformInterface.swift Uses the new descriptor resolver.
Review details
  • Files reviewed: 5/5 changed files
  • Comments generated: 3
  • Review effort level: Lite

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

Comment on lines +265 to +267
let addresses =
(settings.ipv4Settings?.addresses ?? []) + (settings.ipv6Settings?.addresses ?? [])
let candidate = try TunnelFileDescriptor.resolve(addresses: addresses)
while let current = next {
next = current.pointee.ifa_next
guard let address = current.pointee.ifa_addr,
address.pointee.sa_family == AF_INET || address.pointee.sa_family == AF_INET6,
]
XCTAssertEqual(try TunnelFileDescriptor.select(candidates, addresses: [address]), live)
XCTAssertEqual(
try TunnelFileDescriptor.select(candidates.reversed(), addresses: [address]), live)
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.

2 participants