Skip to content

fix(apple): don't apply unchanged network settings - #11141

Merged
jamilbk merged 4 commits into
mainfrom
fix/avoid-network-settings-apply
Dec 13, 2025
Merged

jamilbk merged 4 commits into
mainfrom
fix/avoid-network-settings-apply

Conversation

@jamilbk

@jamilbk jamilbk commented Dec 13, 2025

Copy link
Copy Markdown
Member

Connlib sends various updates via the onResourcesUpdated event to the app in order to both update network settings (for route changes) and the UI (for site online / offline status).

We were blindly applying these updates without first determining if they had any network-related changes. Combined with a bug in connlib that emitted this event even when nothing changed, it had the potential to cause very noticeable network disruptions during something like a flow_creation_failed event. This is because updates like those are emitted at the rate we retry resource connections, every 2s.

When network settings are applied, apps like Google Chrome detect this and immediately cut all in-flight browser requests with an ERR_NETWORK_CHANGED error.

With this change in place, these should happen much less frequently in day to day operation.

Fixes #11120

@vercel

vercel Bot commented Dec 13, 2025 •

Copy link
Copy Markdown

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

Project Deployment Review Updated (UTC)
firezone Ready Ready Preview, Comment Dec 13, 2025 4:26pm

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.

Pull request overview

This PR addresses network disruption issues caused by unnecessary reapplication of network settings. The problem occurred when connlib emitted onResourcesUpdated events without actual network changes, triggering network setting reapplications every 2 seconds during connection retries. This caused browsers like Chrome to abort in-flight requests with ERR_NETWORK_CHANGED errors.

Key changes:

  • Adds an AppliedSettings struct to track the last successfully applied network configuration
  • Implements equality checking to skip redundant network setting applications when nothing has changed
  • Stores network configuration including tunnel addresses, DNS settings, routes, and domain settings for comparison

searchDomains: searchDomains
)

if currentSettings == lastAppliedSettings {

Copilot AI Dec 13, 2025

Copy link

Choose a reason for hiding this comment

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

Consider adding a log statement when network settings are skipped due to no changes. This would help with debugging and understanding why expected network setting applications didn't occur. For example: Log.log("Skipping network settings apply - no changes detected"). This is especially useful given the PR's goal of reducing unnecessary applications.

Suggested change
if currentSettings == lastAppliedSettings {
if currentSettings == lastAppliedSettings {
Log.log("Skipping network settings apply - no changes detected")

Copilot uses AI. Check for mistakes.
Comment on lines 119 to 123
lastAppliedSettings = currentSettings

packetTunnelProvider?.setTunnelNetworkSettings(tunnelNetworkSettings) { error in
if let error = error {
Log.error(error)

Copilot AI Dec 13, 2025

Copy link

Choose a reason for hiding this comment

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

The lastAppliedSettings is being set before the async setTunnelNetworkSettings call completes. If the operation fails (as indicated by the error check on line 122-124), the cached settings will incorrectly reflect that the settings were successfully applied when they weren't. This could prevent future legitimate attempts to apply the same settings. Consider moving this assignment inside the completion handler after confirming no error occurred.

Suggested change
lastAppliedSettings = currentSettings
packetTunnelProvider?.setTunnelNetworkSettings(tunnelNetworkSettings) { error in
if let error = error {
Log.error(error)
packetTunnelProvider?.setTunnelNetworkSettings(tunnelNetworkSettings) { error in
if let error = error {
Log.error(error)
} else {
self.lastAppliedSettings = currentSettings

Copilot uses AI. Check for mistakes.
Comment on lines +34 to +41
private struct AppliedSettings: Equatable {
let tunnelAddressIPv4: String?
let tunnelAddressIPv6: String?
let dnsAddresses: [String]
let routes4: [RouteKey]
let routes6: [RouteKey]
let matchDomains: [String]
let searchDomains: [String]

Copilot AI Dec 13, 2025

Copy link

Choose a reason for hiding this comment

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

The mtu property is part of the network settings (applied on line 117) but is not included in the AppliedSettings struct. Since mtu is declared as a constant (line 15), this might be intentional. However, if mtu ever becomes modifiable in the future, this would cause the optimization to incorrectly skip applying settings when only mtu changes. Consider adding a comment explaining why mtu is excluded, or include it in AppliedSettings for completeness.

Copilot uses AI. Check for mistakes.
Comment on lines +90 to +93
if currentSettings == lastAppliedSettings {
completionHandler?()
return
}

Copilot AI Dec 13, 2025

Copy link

Choose a reason for hiding this comment

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

The apply method modifies and reads lastAppliedSettings without synchronization. If multiple threads call apply concurrently (e.g., when handling rapid resourcesUpdated events), there could be a race condition where two threads read the same lastAppliedSettings value, both proceed to apply settings, and both update lastAppliedSettings, potentially causing duplicate network setting applications. Consider adding synchronization (e.g., using a serial queue or lock) around the check and update of lastAppliedSettings.

Copilot uses AI. Check for mistakes.

@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.

Concept ACK but I'd like to see this refactored to remove some of the indirection.

@@ -28,6 +28,24 @@ class NetworkSettings {
private var matchDomains: [String] = [""]
private var searchDomains: [String] = [""]

// Track the last applied settings to avoid redundant applies
private var lastAppliedSettings: AppliedSettings?

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.

Can we not duplicate this information again?

We already have a custom class NetworkSettings here. That only exists because connlib used to emit all this information in different events. It is now all in one event so we should be able to actually get rid of NetworkSettings and construct the NEPacketTunnelNetworkSettings directly and save us the indirection. Or if that is not Equatable, can we at least refactor this by comparing NetworkSettings instead of creating yet another struct?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

So unfortunately, after testing, it appears that NEPacketTunnelNetworkSettings that are instantiated with the same values are not equal. So we will need to track these separately.

Are you suggesting we instantiate a new instance of NetworkSettings on each callback and check for equality from Adapter? I can try that.

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.

Are you suggesting we instantiate a new instance of NetworkSettings on each callback and check for equality from Adapter? I can try that.

Yeah, basically have NetworkSettings perform the role of what you are trying to do with AppliedSettings in here. Maybe the apply function needs to move out of the class such that NetworkSettings becomes a side-effect free data structure that makes sense to keep around and compare against itself.

It is kind of weird that we are passing the packet tunnel provider into the NetworkSettings and then delegate apply to it. I think it would be cleaner if the side-effect was outside of NetworkSettings. For example, what you could do is create an update function on NetworkSettings where you pass in all new values and only if they differ, you get back a NEPacketTunnelNetworkSettings that you can then pass to the packet tunnel provider.

That would also allow you to unit test that we are only creating the settings if there is an actual change.

@jamilbk jamilbk left a comment •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Tested:

  • macOS 15.7.2
  • iOS 26.1
  • Tested enabling / disabling a resource -> settings applied
  • Tested updating a resource name -> settings not applied
  • Tested pinging a resource that's connected to an offline Site -> settings not applied

This fixes a long-standing issue I've faced where opening a Resource in Chrome after initially connecting would always fail with ERR_NETWORK_CHANGED.

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.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

@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, that is a much cleaner diff. We can do the refactoring of NetworkSettings another time.

Connlib sends various updates via the `onResourcesUpdated` event to the
app in order to both update network settings (for route changes) and the
UI (for site online / offline status).

We were blindly applying these updates without first determining if they
had any network-related changes. Combined with a bug in connlib that
emitted this event even when nothing changed, it had the potential to
cause very noticeable network disruptions during something like a
`flow_creation_failed` event. This is because updates like those are
emitted at the rate we retry resource connections, every 2s.

When network settings are applied, apps like Google Chrome detect this
and immediately cut all in-flight browser requests with an
`ERR_NETWORK_CHANGED` error.

With this change in place, these should happen much less frequently in
day to day operation.

Fixes #11120
@@ -597,7 +618,8 @@ class Adapter: @unchecked Sendable {
// If matchDomains is an empty string, /etc/resolv.conf will contain connlib's
// sentinel, which isn't helpful to us.
private func resetToSystemDNSGettingBindResolvers() -> [String] {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I cannot wait for this hackjob to be removed in #10979

@jamilbk

jamilbk commented Dec 13, 2025

Copy link
Copy Markdown
Member Author

I've retested this on both macOS and iOS and correctly receive both UI updates and networkSettings apply updates properly.

@jamilbk
jamilbk added this pull request to the merge queue Dec 13, 2025
Merged via the queue into main with commit dac872b Dec 13, 2025
28 checks passed
@jamilbk
jamilbk deleted the fix/avoid-network-settings-apply branch December 13, 2025 17:00

This branch was successfully deployed

1 active deployment
Preview — 61900df5 Deployed Dec 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.

Network settings re-applied on each flow_creation_failed message

3 participants