Skip to content

Commit 2b79160

Browse files
gsmetdanielsoro
authored andcommitted
Make sure the TestResourceManager is closed before the CL is closed
- The TestResourceManager is instantiated using the runtime class loader so we need to make sure we close it before we close the runtime class loader. Otherwise we can get into trouble if we need to load some additional classes. - Also make sure that it is only closed once. It used to be closed once when the application is stopped and then again when the QuarkusTestExtensionState was closed. Related to quarkusio#41233
1 parent c42c6ff commit 2b79160

File tree

3 files changed

+37
-18
lines changed

3 files changed

+37
-18
lines changed

core/deployment/src/main/java/io/quarkus/runner/bootstrap/StartupActionImpl.java

+28
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.nio.charset.StandardCharsets;
1212
import java.nio.file.Files;
1313
import java.nio.file.StandardOpenOption;
14+
import java.util.ArrayList;
1415
import java.util.HashMap;
1516
import java.util.List;
1617
import java.util.Map;
@@ -52,6 +53,7 @@ public class StartupActionImpl implements StartupAction {
5253
private final String applicationClassName;
5354
private final Map<String, String> devServicesProperties;
5455
private final List<RuntimeApplicationShutdownBuildItem> runtimeApplicationShutdownBuildItems;
56+
private final List<Closeable> runtimeCloseTasks = new ArrayList<>();
5557

5658
public StartupActionImpl(CuratedApplication curatedApplication, BuildResult buildResult) {
5759
this.curatedApplication = curatedApplication;
@@ -125,6 +127,13 @@ public void run() {
125127
log.error("Failed to run close task", t);
126128
}
127129
}
130+
for (var closeTask : runtimeCloseTasks) {
131+
try {
132+
closeTask.close();
133+
} catch (Throwable t) {
134+
log.error("Failed to run close task", t);
135+
}
136+
}
128137
}
129138
}
130139
}, "Quarkus Main Thread");
@@ -168,6 +177,11 @@ public void run() {
168177
}
169178
}
170179

180+
@Override
181+
public void addRuntimeCloseTask(Closeable closeTask) {
182+
this.runtimeCloseTasks.add(closeTask);
183+
}
184+
171185
private void doClose() {
172186
try {
173187
runtimeClassLoader.loadClass(Quarkus.class.getName()).getMethod("blockingExit").invoke(null);
@@ -234,6 +248,13 @@ public void run() {
234248
}
235249
return result.get();
236250
} finally {
251+
for (var closeTask : runtimeCloseTasks) {
252+
try {
253+
closeTask.close();
254+
} catch (Throwable t) {
255+
log.error("Failed to run close task", t);
256+
}
257+
}
237258
runtimeClassLoader.close();
238259
Thread.currentThread().setContextClassLoader(old);
239260
for (var i : runtimeApplicationShutdownBuildItems) {
@@ -294,6 +315,13 @@ public void close() throws IOException {
294315
// (e.g. ServiceLoader calls)
295316
Thread.currentThread().setContextClassLoader(runtimeClassLoader);
296317
closeTask.close();
318+
for (var closeTask : runtimeCloseTasks) {
319+
try {
320+
closeTask.close();
321+
} catch (Throwable t) {
322+
log.error("Failed to run close task", t);
323+
}
324+
}
297325
} finally {
298326
Thread.currentThread().setContextClassLoader(original);
299327
runtimeClassLoader.close();

independent-projects/bootstrap/core/src/main/java/io/quarkus/bootstrap/app/StartupAction.java

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.quarkus.bootstrap.app;
22

3+
import java.io.Closeable;
34
import java.util.Map;
45
import java.util.function.Consumer;
56

@@ -31,4 +32,6 @@ public interface StartupAction {
3132
*/
3233
int runMainClassBlocking(String... args) throws Exception;
3334

35+
void addRuntimeCloseTask(Closeable closeTask);
36+
3437
}

test-framework/junit5/src/main/java/io/quarkus/test/junit/QuarkusTestExtension.java

+6-18
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ public Thread newThread(Runnable r) {
237237
Map<String, String> properties = (Map<String, String>) testResourceManager.getClass().getMethod("start")
238238
.invoke(testResourceManager);
239239
startupAction.overrideConfig(properties);
240+
startupAction.addRuntimeCloseTask(testResourceManager);
240241
hasPerTestResources = (boolean) testResourceManager.getClass().getMethod("hasPerTestResources")
241242
.invoke(testResourceManager);
242243

@@ -276,7 +277,6 @@ public Thread newThread(Runnable r) {
276277
RestorableSystemProperties restorableSystemProperties = RestorableSystemProperties.setProperties(
277278
Collections.singletonMap("test.url", TestHTTPResourceManager.getUri(runningQuarkusApplication)));
278279

279-
Closeable tm = testResourceManager;
280280
Closeable shutdownTask = new Closeable() {
281281
@Override
282282
public void close() throws IOException {
@@ -292,12 +292,8 @@ public void close() throws IOException {
292292
shutdownTasks.pop().run();
293293
}
294294
} finally {
295-
try {
296-
tm.close();
297-
} finally {
298-
restorableSystemProperties.close();
299-
shutdownHangDetection();
300-
}
295+
restorableSystemProperties.close();
296+
shutdownHangDetection();
301297
}
302298
try {
303299
TestClassIndexer.removeIndex(requiredTestClass);
@@ -1233,23 +1229,15 @@ protected void doClose() throws IOException {
12331229
Thread.currentThread().setContextClassLoader(runningQuarkusApplication.getClassLoader());
12341230
}
12351231
try {
1232+
// this will close the application, the test resources, the class loader...
12361233
resource.close();
12371234
} catch (Throwable e) {
12381235
log.error("Failed to shutdown Quarkus", e);
12391236
} finally {
12401237
runningQuarkusApplication = null;
12411238
clonePattern = null;
1242-
try {
1243-
if (QuarkusTestExtension.this.originalCl != null) {
1244-
setCCL(QuarkusTestExtension.this.originalCl);
1245-
}
1246-
testResourceManager.close();
1247-
} catch (Exception e) {
1248-
log.error("Failed to shutdown Quarkus test resources", e);
1249-
} finally {
1250-
Thread.currentThread().setContextClassLoader(old);
1251-
ConfigProviderResolver.setInstance(null);
1252-
}
1239+
Thread.currentThread().setContextClassLoader(old);
1240+
ConfigProviderResolver.setInstance(null);
12531241
}
12541242
}
12551243
}

0 commit comments

Comments
 (0)