Repository navigation
fix(apple): don't apply unchanged network settings - #11141
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
There was a problem hiding this comment.
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
AppliedSettingsstruct 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 { |
There was a problem hiding this comment.
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.
| if currentSettings == lastAppliedSettings { | |
| if currentSettings == lastAppliedSettings { | |
| Log.log("Skipping network settings apply - no changes detected") |
| lastAppliedSettings = currentSettings | ||
|
|
||
| packetTunnelProvider?.setTunnelNetworkSettings(tunnelNetworkSettings) { error in | ||
| if let error = error { | ||
| Log.error(error) |
There was a problem hiding this comment.
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.
| 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 |
| 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] |
There was a problem hiding this comment.
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.
| if currentSettings == lastAppliedSettings { | ||
| completionHandler?() | ||
| return | ||
| } |
There was a problem hiding this comment.
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.
thomaseizinger
left a comment
There was a problem hiding this comment.
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? | |||
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Are you suggesting we instantiate a new instance of
NetworkSettingson 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.
There was a problem hiding this comment.
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.
thomaseizinger
left a comment
There was a problem hiding this comment.
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
50b09be to
61900df
Compare
| @@ -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] { | |||
There was a problem hiding this comment.
I cannot wait for this hackjob to be removed in #10979
|
I've retested this on both macOS and iOS and correctly receive both UI updates and networkSettings apply updates properly. |
Connlib sends various updates via the
onResourcesUpdatedevent 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_failedevent. 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_CHANGEDerror.With this change in place, these should happen much less frequently in day to day operation.
Fixes #11120