[FELIX-4190] The framework should not hold any lock while calling ServiceFactory#unget
Make sure all references are cleaned

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1576875 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/framework/src/main/java/org/apache/felix/framework/ServiceRegistry.java b/framework/src/main/java/org/apache/felix/framework/ServiceRegistry.java
index 32494e2..46c880b 100644
--- a/framework/src/main/java/org/apache/felix/framework/ServiceRegistry.java
+++ b/framework/src/main/java/org/apache/felix/framework/ServiceRegistry.java
@@ -155,13 +155,21 @@
         }
 
         // Now forcibly unget the service object for all stubborn clients.
-        Bundle[] clients = getUsingBundles(reg.getReference());
+        ServiceReference ref = reg.getReference();
+        Bundle[] clients = getUsingBundles(ref);
         for (int i = 0; (clients != null) && (i < clients.length); i++)
         {
             while (ungetService(clients[i], reg.getReference()))
                 ; // Keep removing until it is no longer possible
         }
+        // Invalidate registration
         ((ServiceRegistrationImpl) reg).invalidate();
+        // Bundles are allowed to get a reference while unregistering
+        for (int i = 0; (clients != null) && (i < clients.length); i++)
+        {
+            while (ungetService(clients[i], ref))
+                ; // Keep removing until it is no longer possible
+        }
     }
 
     /**