Skip to content

fix(apple): prevent utun increments from IPC calls - #10855

Merged
jamilbk merged 6 commits into
mainfrom
fix/never-increment-utun
Nov 13, 2025
Merged

jamilbk merged 6 commits into
mainfrom
fix/never-increment-utun

Conversation

@klochowicz

@klochowicz klochowicz commented Nov 11, 2025 •

Copy link
Copy Markdown
Contributor

On macOS, IPC calls to the network extension can wake it whilst not
connected, causing the system to create a utun device.
If startTunnel() is not subsequently called, these devices
persist and accumulate over time.

The existing dryStartStopCycle() mechanism was introduced to wake the
extension after upgrades, but other IPC operations (log management
functions) could also wake the extension without proper cleanup.

Solution

Add wrapper functions in IPCClient that automatically handle wake-up
and cleanup lifecycle for IPC calls made whilst disconnected:

  • Check VPN connection status
  • If connected: execute IPC operation directly (utun already exists)
  • If disconnected: wake extension → wait 500ms → execute IPC → cleanup

Implementation

For async IPC operations (clearLogs, getLogFolderSize):
Created free functions in IPCClient that wrap low-level IPC calls
with wrapIPCCallIfNeeded():

  • clearLogsWithCleanup(store:session:)
  • getLogFolderSizeWithCleanup(store:session:)

For callback-based exportLogs:
We cannot use wrapper because exportLogs returns immediately and uses
callbacks for streaming chunks. Wrapper would call stop() before
export finishes, killing the extension mid-stream.

Solution: Manual wake-up/cleanup in LogExporter where we already have
continuation that waits for chunk.done signal:

  1. Check if extension needs waking (vpnStatus != .connected)
  2. If yes: wake extension, wait 500ms
  3. Start export with callbacks
  4. When chunk.done=true: cleanup utun device, then resume continuation
  5. On error: cleanup utun device, then resume with error

Fixes #10580

@vercel

vercel Bot commented Nov 11, 2025 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
firezone Ready Ready Preview Comment Nov 13, 2025 1:37pm

@klochowicz
klochowicz requested review from jamilbk and thomaseizinger and removed request for thomaseizinger November 11, 2025 07:21
@klochowicz klochowicz self-assigned this Nov 11, 2025
@klochowicz klochowicz added the area/apple_client Issues related to the Apple client label Nov 11, 2025
@klochowicz

Copy link
Copy Markdown
Contributor Author

open question:

shall we allow setConfiguration() to wake up the extension or keep the previous fix?

@thomaseizinger thomaseizinger left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nice work! Some minor, mostly stylistic feedback.

@jamilbk jamilbk left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

In the right direction! I think we need to clean up a couple things.

On macOS, IPC calls to the network extension can wake it whilst not
connected, causing the system to create a utun device (utun0, utun1,
utun2...). If startTunnel() is not subsequently called, these devices
persist and accumulate over time.

The existing dryStartStopCycle() mechanism was introduced to wake the
extension after upgrades, but other IPC operations (log management
functions) could also wake the extension without proper cleanup.

Solution
--------

Add wrapper functions in IPCClient that automatically handle wake-up
and cleanup lifecycle for IPC calls made whilst disconnected:

- Check VPN connection status
- If connected: execute IPC operation directly (utun already exists)
- If disconnected: wake extension → wait 500ms → execute IPC → cleanup

@thomaseizinger thomaseizinger left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

One more non-blocking comment, otherwise LGTM

@jamilbk jamilbk removed the area/apple_client Issues related to the Apple client label Nov 12, 2025

@jamilbk jamilbk left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@klochowicz I pushed several cleanup changes and a few bug fixes. Please review the most recent commits and let me know if you have any questions.

@thomaseizinger Another review would be much appreciated.

Tested

  • Log count / clear / export works on iOS and macOS
  • utun does not increment in either connected or disconnected states on macOS and iOS
  • Configuration is persisted and latest is used on macOS / iOS
  • Configuration observer does not send IPC calls unless TunnelConfiguration instance has meaningfully changed.


if session.status != .connected {
Log.trace("Not setting configuration whilst not connected")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Our private version of sendProviderMessage handles this case for us now.

@@ -52,33 +52,10 @@ import System
.appendingPathComponent("tunnel.zip")
fileManager.createFile(atPath: tunnelLogURL.path, contents: nil)
let fileHandle = try FileHandle(forWritingTo: tunnelLogURL)
defer { try? fileHandle.close() }

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ensures we close the file handle regardless of outcome.

Comment on lines -365 to -382
// Get data from the provider - if hash matches, provider returns nil
let data = try await withCheckedThrowingContinuation {
(continuation: CheckedContinuation<Data?, Error>) in
do {
guard session.status == .connected else {
throw IPCClient.Error.invalidStatus(session.status)
}

try session.sendProviderMessage(
IPCClient.encoder.encode(ProviderMessage.getResourceList(currentHash))
) { data in
continuation.resume(returning: data)
}
} catch {
continuation.resume(throwing: error)
}
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This business logic was moved back into IPCClient, where we can leverage the new sendProviderMessage private func to have consistent IPC call semantics.

Not sure how it escaped, but it would probably be a tad cleaner to keep IPC calls in that module instead of calling them directly from other areas of the codebase.

@jamilbk
jamilbk force-pushed the fix/never-increment-utun branch from 24d9be4 to ef9258d Compare November 12, 2025 21:15

@thomaseizinger thomaseizinger left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

I left a suggestion for a more type-safe interface although I am not sure if this can be expressed in Swift.

@klochowicz klochowicz left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

LGTM!

thank you @jamilbk for cleaning it up.

can't approve on github, as I created the PR

@jamilbk

jamilbk commented Nov 13, 2025

Copy link
Copy Markdown
Member

Merging to get into next release

@jamilbk
jamilbk added this pull request to the merge queue Nov 13, 2025
Merged via the queue into main with commit bdffa3a Nov 13, 2025
27 checks passed
@jamilbk
jamilbk deleted the fix/never-increment-utun branch November 13, 2025 22:38
klochowicz added a commit that referenced this pull request Nov 26, 2025
github-merge-queue Bot pushed a commit that referenced this pull request Nov 27, 2025
A regression was introduced in #10855 that introduced a `Task {}`
closure upon each iteration of the log export loop that was never
awaited. This meant that the log export IPC call returned after the
first chunk was written, which triggered the `defer` block which stopped
the tunnel and caused the next iteration of the task to return `nil`
data, corrupting the log archive.

To fix this, we ensure the ipc calls happen within the loop, passing
control flow logic back to the caller to ultimately determine if further
IPC calls to grab logs should be made.

No changelog since not released yet.
jamilbk added a commit that referenced this pull request Jan 27, 2026
In #10855, we removed the passing of the configuration through
startTunnel based on the assumption that a prior `setConfiguration` call
would have occurred in order to hydrate the configuration cache.

Unfortunately, on iOS, this is not the case, resulting in a bug on fresh
installs of iOS that prevents the tunnel from starting because it has no
configuration.

Related:
https://app.hubspot.com/live-messages/23723443/inbox/10333087281

This branch was successfully deployed

1 active deployment
Preview — 76b1288d Deployed Nov 13, 2025 by vercel[bot]
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.

utun pool increments can push interface order past others, resulting in broken DNS interception

3 participants