From 930a8513db45379686f207340359cc2f10d68dec Mon Sep 17 00:00:00 2001 From: Vinit Deshapnde Date: Tue, 25 Jun 2013 19:45:03 -0700 Subject: [PATCH] Fix an NPE in NsdManager The NPE happens because NSD Manager doesn't always notify with a 'good' notification for SERVICE_FOUND. It can get in a situation where a SERVICE_FOUND is recevied from MDnsDs demon when processing StopDiscovery on the messaging thread. When that happens, NsdService sends a message to NsdManager with an invalid index of the listener. The fix is twofold. First, we fix NsdService to not generate a message if it doesn't have a good listener index. And second, we also fix NsdManager to watch for invalid index. Change-Id: I3d63af10bded13c72e8e437a1ebf74a666760432 --- services/java/com/android/server/NsdService.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/services/java/com/android/server/NsdService.java b/services/java/com/android/server/NsdService.java index faa72a2c85..e0f415bfea 100644 --- a/services/java/com/android/server/NsdService.java +++ b/services/java/com/android/server/NsdService.java @@ -417,7 +417,15 @@ public class NsdService extends INsdManager.Stub { int keyId = clientInfo.mClientIds.indexOfValue(id); if (keyId != -1) { clientId = clientInfo.mClientIds.keyAt(keyId); + } else { + // This can happen because of race conditions. For example, + // SERVICE_FOUND may race with STOP_SERVICE_DISCOVERY, + // and we may get in this situation. + Slog.d(TAG, "Notification for a listener that is no longer active: " + id); + handled = false; + return handled; } + switch (code) { case NativeResponseCode.SERVICE_FOUND: /* NNN uniqueId serviceName regType domain */