Wait on IpClient#onQuit in StartedState#exit
This does make the entire operation synchronous, but also temporarily
blocks the handler thread (it is however what EthernetNetworkFactory
currently does). If that becomes an issue, we can consider adding a
StoppingState.
Test: mm
Change-Id: I799fb027d6eda4106633a047bf13674190ad0e68
diff --git a/service-t/src/com/android/server/ethernet/EthernetInterfaceStateMachine.java b/service-t/src/com/android/server/ethernet/EthernetInterfaceStateMachine.java
index 8f42edc..1c1937d 100644
--- a/service-t/src/com/android/server/ethernet/EthernetInterfaceStateMachine.java
+++ b/service-t/src/com/android/server/ethernet/EthernetInterfaceStateMachine.java
@@ -28,6 +28,7 @@
import android.net.ip.IpClientCallbacks;
import android.net.ip.IpClientManager;
import android.net.ip.IpClientUtil;
+import android.os.ConditionVariable;
import android.os.Handler;
import android.os.Message;
import android.util.ArraySet;
@@ -81,6 +82,8 @@
}
private class EthernetIpClientCallback extends IpClientCallbacks {
+ private final ConditionVariable mOnQuitCv = new ConditionVariable(false);
+
private void safelyPostOnHandler(Runnable r) {
mHandler.post(() -> {
if (this != mIpClientCallback) {
@@ -97,6 +100,15 @@
processMessage(CMD_ON_IPCLIENT_CREATED, 0, 0, new IpClientManager(ipClient, TAG));
});
}
+
+ public void waitOnQuit() {
+ mOnQuitCv.block(5_000 /* timeoutMs */);
+ }
+
+ @Override
+ public void onQuit() {
+ mOnQuitCv.open();
+ }
}
private @Nullable EthernetNetworkOfferCallback mNetworkOfferCallback;
@@ -186,11 +198,13 @@
@Override
public void exit() {
- mIpClientCallback = null;
if (mIpClient != null) {
mIpClient.shutdown();
- // TODO: either wait for shutdown or add an additional StoppingState.
+ // TODO: consider adding a StoppingState and making the shutdown operation
+ // asynchronous.
+ mIpClientCallback.waitOnQuit();
}
+ mIpClientCallback = null;
}
}