Fix child service locator shutdown (#472)
Signed-off-by: Jan Supol <jan.supol@oracle.com>
diff --git a/hk2-locator/src/main/java/org/jvnet/hk2/internal/ServiceLocatorImpl.java b/hk2-locator/src/main/java/org/jvnet/hk2/internal/ServiceLocatorImpl.java
index aff4e3b..4f872d5 100755
--- a/hk2-locator/src/main/java/org/jvnet/hk2/internal/ServiceLocatorImpl.java
+++ b/hk2-locator/src/main/java/org/jvnet/hk2/internal/ServiceLocatorImpl.java
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2012, 2018 Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2012, 2019 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -31,6 +31,7 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
+import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
@@ -871,14 +872,19 @@
wLock.lock();
try {
if (state.equals(ServiceLocatorState.SHUTDOWN)) return;
-
- for (ServiceLocatorImpl child : children.keySet()) {
- child.shutdown();
+
+ synchronized(children) {
+ for (Iterator<ServiceLocatorImpl> childIterator = children.keySet().iterator(); childIterator.hasNext();) {
+ ServiceLocatorImpl child = childIterator.next();
+ childIterator.remove();
+ child.shutdown();
+ }
}
if (parent != null) {
parent.removeChild(this);
}
+
}
finally {
wLock.unlock();
diff --git a/hk2-locator/src/test/java/org/glassfish/hk2/tests/locator/children/ChildServiceLocatorTest.java b/hk2-locator/src/test/java/org/glassfish/hk2/tests/locator/children/ChildServiceLocatorTest.java
index db1810e..398fd7b 100644
--- a/hk2-locator/src/test/java/org/glassfish/hk2/tests/locator/children/ChildServiceLocatorTest.java
+++ b/hk2-locator/src/test/java/org/glassfish/hk2/tests/locator/children/ChildServiceLocatorTest.java
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2019 Payara Services Ltd.
+ * Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -33,9 +34,10 @@
public void CreationTest() {
ServiceLocator parent = LocatorHelper.create();
ServiceLocator child = LocatorHelper.create(parent);
+ ServiceLocator child2 = LocatorHelper.create(parent);
parent.shutdown();
Assert.assertEquals(child.getState(), ServiceLocatorState.SHUTDOWN);
-
+ Assert.assertEquals(child2.getState(), ServiceLocatorState.SHUTDOWN);
}