getService: try getting service before waiting
am: 3ae9bf951b

Change-Id: I680f3b28fdd989e613ae8ed97465bfac6a5c4ffe
diff --git a/transport/ServiceManagement.cpp b/transport/ServiceManagement.cpp
index edd2af3..f11acab 100644
--- a/transport/ServiceManagement.cpp
+++ b/transport/ServiceManagement.cpp
@@ -677,11 +677,13 @@
     const bool vintfLegacy = (transport == Transport::EMPTY);
 #endif  // ENFORCE_VINTF_MANIFEST
 
-    while (!getStub && (vintfHwbinder || vintfLegacy)) {
-        if (waiter == nullptr) {
+    for (int tries = 0; !getStub && (vintfHwbinder || vintfLegacy); tries++) {
+        if (waiter == nullptr && tries > 0) {
             waiter = new Waiter(descriptor, instance, sm);
         }
-        waiter->reset(); // don't reorder this -- see comments on reset()
+        if (waiter != nullptr) {
+            waiter->reset();  // don't reorder this -- see comments on reset()
+        }
         Return<sp<IBase>> ret = sm->get(descriptor, instance);
         if (!ret.isOk()) {
             ALOGE("getService: defaultServiceManager()->get returns %s for %s/%s.",
@@ -694,7 +696,9 @@
                 details::canCastInterface(base.get(), descriptor.c_str(), true /* emitError */);
 
             if (canCastRet.isOk() && canCastRet) {
-                waiter->done();
+                if (waiter != nullptr) {
+                    waiter->done();
+                }
                 return base; // still needs to be wrapped by Bp class.
             }
 
@@ -704,8 +708,10 @@
         // In case of legacy or we were not asked to retry, don't.
         if (vintfLegacy || !retry) break;
 
-        ALOGI("getService: Trying again for %s/%s...", descriptor.c_str(), instance.c_str());
-        waiter->wait();
+        if (waiter != nullptr) {
+            ALOGI("getService: Trying again for %s/%s...", descriptor.c_str(), instance.c_str());
+            waiter->wait();
+        }
     }
 
     if (waiter != nullptr) {