Skip to content

Commit 8616bf5

Browse files
authored
Workaround for shutdown error (#722)
This commit is a workaround for an error which seems to happen since the upgrade to Micronaut 4.6. In a nutshell, when the `/stop` endpoint is called, the thread which was started to monitor if the service is properly shutdown after a timeout was started and the application context was closed, _before_ the `stop` method would return something to the client. As a consequence, there was an error message saying that the application context wasn't open and that a bean (the message writers) weren't found. This was not quite true, since the application context _used to be_ open but wasn't. The workaround, which isn't great, is to use the task scheduler to delay the shutdown by a few hundreds of milliseconds. This gives the opportunity to send the response back to the client _before_ the application context is shutdown. Note that in any case, this wasn't a big issue, since the service would be shutdown anyway, but the error message for the user wasn't great.
1 parent 570a634 commit 8616bf5

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

test-resources-server/src/main/java/io/micronaut/testresources/server/TestResourcesController.java

+9-5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import io.micronaut.http.annotation.Post;
2323
import io.micronaut.runtime.server.EmbeddedServer;
2424
import io.micronaut.scheduling.TaskExecutors;
25+
import io.micronaut.scheduling.TaskScheduler;
2526
import io.micronaut.scheduling.annotation.ExecuteOn;
2627
import io.micronaut.testresources.core.ResolverLoader;
2728
import io.micronaut.testresources.core.TestResourcesResolutionException;
@@ -33,6 +34,7 @@
3334

3435
import java.io.Closeable;
3536
import java.io.IOException;
37+
import java.time.Duration;
3638
import java.util.Collection;
3739
import java.util.Collections;
3840
import java.util.List;
@@ -57,15 +59,18 @@ public class TestResourcesController implements TestResourcesResolver {
5759
private final List<PropertyResolutionListener> propertyResolutionListeners;
5860
private final EmbeddedServer embeddedServer;
5961
private final ApplicationContext applicationContext;
62+
private final TaskScheduler taskScheduler;
6063

6164
public TestResourcesController(List<PropertyResolutionListener> propertyResolutionListeners,
6265
EmbeddedServer embeddedServer,
6366
ApplicationContext applicationContext,
64-
ResolverLoader loader) {
67+
ResolverLoader loader,
68+
TaskScheduler taskScheduler) {
6569
this.propertyResolutionListeners = propertyResolutionListeners;
6670
this.embeddedServer = embeddedServer;
6771
this.applicationContext = applicationContext;
6872
this.loader = loader;
73+
this.taskScheduler = taskScheduler;
6974
}
7075

7176
/**
@@ -235,8 +240,8 @@ public List<TestContainer> listContainersByScope(@Nullable String scope) {
235240
* Requests a test resources service shutdown.
236241
*/
237242
@Post("/stop")
238-
public void stopService() {
239-
var makeSureServerIsStopped = new Thread(() -> {
243+
public boolean stopService() {
244+
taskScheduler.schedule(Duration.ofMillis(200), () -> {
240245
try {
241246
try {
242247
embeddedServer.stop();
@@ -252,8 +257,7 @@ public void stopService() {
252257
System.exit(0);
253258
}
254259
});
255-
makeSureServerIsStopped.setDaemon(true);
256-
makeSureServerIsStopped.start();
260+
return true;
257261
}
258262

259263
private void closeResolvers() {

0 commit comments

Comments
 (0)