-
Notifications
You must be signed in to change notification settings - Fork 63
Fix UI stuck on “Disconnected” during network-change engine restart #167
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
6eb07a3
f0df3f5
b52ce5d
212cf42
5a96d64
bd31953
b2d0f6d
ff71758
5c6391e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,15 +9,22 @@ | |
| import androidx.annotation.NonNull; | ||
| import androidx.core.util.Consumer; | ||
|
|
||
| import java.util.concurrent.atomic.AtomicBoolean; | ||
| import java.util.concurrent.atomic.AtomicReference; | ||
|
|
||
| public class NetworkChangeDetector { | ||
| private static final String LOGTAG = NetworkChangeDetector.class.getSimpleName(); | ||
| private final ConnectivityManager connectivityManager; | ||
| private ConnectivityManager.NetworkCallback networkCallback; | ||
| private ConnectivityManager.NetworkCallback defaultNetworkCallback; | ||
| private volatile NetworkAvailabilityListener listener; | ||
| private final AtomicBoolean defaultNetworkCallbackActive = new AtomicBoolean(false); | ||
| private final AtomicReference<Network> currentlyBoundDefaultNetwork = new AtomicReference<>(null); | ||
|
|
||
| public NetworkChangeDetector(ConnectivityManager connectivityManager) { | ||
| this.connectivityManager = connectivityManager; | ||
| initNetworkCallback(); | ||
| initDefaultNetworkCallback(); | ||
| } | ||
|
|
||
| private void checkNetworkCapabilities(Network network, Consumer<Integer> operation) { | ||
|
|
@@ -58,18 +65,74 @@ public void onCapabilitiesChanged(@NonNull Network network, @NonNull NetworkCapa | |
| }; | ||
| } | ||
|
|
||
| private void initDefaultNetworkCallback() { | ||
| defaultNetworkCallback = new ConnectivityManager.NetworkCallback() { | ||
| @Override | ||
| public void onAvailable(@NonNull Network network) { | ||
| if (!defaultNetworkCallbackActive.get()) { | ||
| Log.d(LOGTAG, "ignoring onAvailable for " + network + "; default callback inactive"); | ||
| return; | ||
| } | ||
| Log.d(LOGTAG, "default network became " + network + ", binding process to it"); | ||
| try { | ||
| if (connectivityManager.bindProcessToNetwork(network)) { | ||
| currentlyBoundDefaultNetwork.set(network); | ||
| } else { | ||
| Log.w(LOGTAG, "bindProcessToNetwork returned false for " + network); | ||
| } | ||
| } catch (Exception e) { | ||
| Log.e(LOGTAG, "bindProcessToNetwork failed", e); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void onLost(@NonNull Network network) { | ||
| if (!defaultNetworkCallbackActive.get()) { | ||
| Log.d(LOGTAG, "ignoring onLost for " + network + "; default callback inactive"); | ||
| return; | ||
| } | ||
| if (!network.equals(currentlyBoundDefaultNetwork.get())) { | ||
| Log.d(LOGTAG, "ignoring onLost for " + network + "; not the currently bound default network"); | ||
| return; | ||
| } | ||
| Log.d(LOGTAG, "default network " + network + " lost, clearing process binding"); | ||
| try { | ||
| if (connectivityManager.bindProcessToNetwork(null)) { | ||
| currentlyBoundDefaultNetwork.compareAndSet(network, null); | ||
| } | ||
| } catch (Exception e) { | ||
| Log.e(LOGTAG, "bindProcessToNetwork(null) failed", e); | ||
| } | ||
| } | ||
| }; | ||
| } | ||
|
Comment on lines
+68
to
+108
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Residual TOCTOU between The
The process ends up bound to a network after full shutdown, with A small 🔧 Proposed hardening- private final AtomicBoolean defaultNetworkCallbackActive = new AtomicBoolean(false);
- private final AtomicReference<Network> currentlyBoundDefaultNetwork = new AtomicReference<>(null);
+ private final Object defaultNetworkBindingLock = new Object();
+ private boolean defaultNetworkCallbackActive = false;
+ private Network currentlyBoundDefaultNetwork = null;
@@
public void onAvailable(`@NonNull` Network network) {
- if (!defaultNetworkCallbackActive.get()) {
- Log.d(LOGTAG, "ignoring onAvailable for " + network + "; default callback inactive");
- return;
- }
- Log.d(LOGTAG, "default network became " + network + ", binding process to it");
- try {
- if (connectivityManager.bindProcessToNetwork(network)) {
- currentlyBoundDefaultNetwork.set(network);
- } else {
- Log.w(LOGTAG, "bindProcessToNetwork returned false for " + network);
- }
- } catch (Exception e) {
- Log.e(LOGTAG, "bindProcessToNetwork failed", e);
- }
+ synchronized (defaultNetworkBindingLock) {
+ if (!defaultNetworkCallbackActive) {
+ Log.d(LOGTAG, "ignoring onAvailable for " + network + "; default callback inactive");
+ return;
+ }
+ Log.d(LOGTAG, "default network became " + network + ", binding process to it");
+ try {
+ if (connectivityManager.bindProcessToNetwork(network)) {
+ currentlyBoundDefaultNetwork = network;
+ } else {
+ Log.w(LOGTAG, "bindProcessToNetwork returned false for " + network);
+ }
+ } catch (Exception e) {
+ Log.e(LOGTAG, "bindProcessToNetwork failed", e);
+ }
+ }
}
@@
public void onLost(`@NonNull` Network network) {
- if (!defaultNetworkCallbackActive.get()) { ... }
- if (!network.equals(currentlyBoundDefaultNetwork.get())) { ... }
- ...
+ synchronized (defaultNetworkBindingLock) {
+ if (!defaultNetworkCallbackActive) { return; }
+ if (!network.equals(currentlyBoundDefaultNetwork)) { return; }
+ try {
+ if (connectivityManager.bindProcessToNetwork(null)) {
+ currentlyBoundDefaultNetwork = null;
+ }
+ } catch (Exception e) {
+ Log.e(LOGTAG, "bindProcessToNetwork(null) failed", e);
+ }
+ }
}
@@
public void unregisterNetworkCallback() {
- defaultNetworkCallbackActive.set(false);
+ synchronized (defaultNetworkBindingLock) {
+ defaultNetworkCallbackActive = false;
+ }
@@
- try {
- connectivityManager.bindProcessToNetwork(null);
- currentlyBoundDefaultNetwork.set(null);
- } catch (Exception e) {
+ synchronized (defaultNetworkBindingLock) {
+ try {
+ connectivityManager.bindProcessToNetwork(null);
+ currentlyBoundDefaultNetwork = null;
+ } catch (Exception e) {
Log.e(LOGTAG, "bindProcessToNetwork(null) on unregister failed", e);
+ }
}
}🤖 Prompt for AI Agents |
||
|
|
||
| public void registerNetworkCallback() { | ||
| NetworkRequest.Builder builder = new NetworkRequest.Builder(); | ||
| builder.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET); | ||
| connectivityManager.registerNetworkCallback(builder.build(), networkCallback); | ||
| defaultNetworkCallbackActive.set(true); | ||
| connectivityManager.registerDefaultNetworkCallback(defaultNetworkCallback); | ||
| } | ||
|
|
||
| public void unregisterNetworkCallback() { | ||
| defaultNetworkCallbackActive.set(false); | ||
| try { | ||
| connectivityManager.unregisterNetworkCallback(networkCallback); | ||
| } catch (Exception e) { | ||
| Log.e(LOGTAG, "failed to unregister network callback", e); | ||
| } | ||
| try { | ||
| connectivityManager.unregisterNetworkCallback(defaultNetworkCallback); | ||
| } catch (Exception e) { | ||
| Log.e(LOGTAG, "failed to unregister default network callback", e); | ||
| } | ||
| try { | ||
| connectivityManager.bindProcessToNetwork(null); | ||
| currentlyBoundDefaultNetwork.set(null); | ||
| } catch (Exception e) { | ||
| Log.e(LOGTAG, "bindProcessToNetwork(null) on unregister failed", e); | ||
| } | ||
| } | ||
|
|
||
| public void subscribe(NetworkAvailabilityListener listener) { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.