Fix race condition where a VPN is still active while detecting connetion type #543
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I was experiencing a bug where the change from metered to un-metered did not take effect. After investigating, this was caused by the VPN service continuing to be active while attempting to detect the after network. This resulted in the a VPN network being set as the active network for the VPN. This prevented other calls that determine the metered status from accurately determining the metered status and it seems it defaulted to true if the underlying network for the VPN was a VPN.
Here's an example from Logcat of the issue:
2024-08-16 20:46:55.843 9252-9296 NetGuard.Service eu.faircode.netguard I Setting underlying network=1073 [type: VPN[], state: DISCONNECTED/DISCONNECTED, reason: (unspecified), extra: (none), failover: false, available: true, roaming: false]
There's a twofold fix here:
I'm not happy with the change in part 2 since it uses busy-waiting. The API documentation for the network status API says that the information should be tracked by using the network status callbacks and memorizing the information. This may make waiting for the VPN to go down before setting the underlying network unnecessary.
https://developer.android.com/reference/android/net/NetworkInfo#isConnected()