Ensure the dump service is completed
If dumping MdnsDiscoveryManager is not executed on the handler
thread, it may not complete in time because the dump task is
performed on the handler thread. Therefore, use
HandlerUtils#runWithScissorsForDump to ensure the dump service
is completed.
Test: dumpsys servicediscovery
Change-Id: Ifbb972fe7a83677b7cdcc97bb6c5c099ce56bb8f
diff --git a/service-t/src/com/android/server/connectivity/mdns/MdnsDiscoveryManager.java b/service-t/src/com/android/server/connectivity/mdns/MdnsDiscoveryManager.java
index b16d8bd..f540f10 100644
--- a/service-t/src/com/android/server/connectivity/mdns/MdnsDiscoveryManager.java
+++ b/service-t/src/com/android/server/connectivity/mdns/MdnsDiscoveryManager.java
@@ -33,6 +33,7 @@
import com.android.internal.annotations.VisibleForTesting;
import com.android.net.module.util.DnsUtils;
+import com.android.net.module.util.HandlerUtils;
import com.android.net.module.util.SharedLog;
import com.android.server.connectivity.mdns.util.MdnsUtils;
@@ -213,6 +214,19 @@
MdnsUtils.ensureRunningOnHandlerThread(handler);
}
}
+
+ public void runWithScissorsForDumpIfReady(@NonNull Runnable function) {
+ final Handler handler;
+ synchronized (pendingTasks) {
+ if (this.handler == null) {
+ Log.d(TAG, "The handler is not ready. Ignore the DiscoveryManager dump");
+ return;
+ } else {
+ handler = this.handler;
+ }
+ }
+ HandlerUtils.runWithScissorsForDump(handler, function, 10_000);
+ }
}
/**
@@ -469,7 +483,7 @@
* Dump DiscoveryManager state.
*/
public void dump(PrintWriter pw) {
- discoveryExecutor.checkAndRunOnHandlerThread(() -> {
+ discoveryExecutor.runWithScissorsForDumpIfReady(() -> {
pw.println("Clients:");
// Dump ServiceTypeClients
for (MdnsServiceTypeClient serviceTypeClient
diff --git a/staticlibs/Android.bp b/staticlibs/Android.bp
index 85258f8..9d27608 100644
--- a/staticlibs/Android.bp
+++ b/staticlibs/Android.bp
@@ -435,6 +435,7 @@
sdk_version: "core_platform",
srcs: [
"device/com/android/net/module/util/FdEventsReader.java",
+ "device/com/android/net/module/util/HandlerUtils.java",
"device/com/android/net/module/util/SharedLog.java",
"framework/com/android/net/module/util/ByteUtils.java",
"framework/com/android/net/module/util/CollectionUtils.java",