From 8616bf577575c4626b48f6619a9625c202a85a52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Champeau?= Date: Thu, 26 Sep 2024 17:52:07 +0200 Subject: [PATCH] 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. --- .../server/TestResourcesController.java | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/test-resources-server/src/main/java/io/micronaut/testresources/server/TestResourcesController.java b/test-resources-server/src/main/java/io/micronaut/testresources/server/TestResourcesController.java index f849cf451..7d3418b40 100644 --- a/test-resources-server/src/main/java/io/micronaut/testresources/server/TestResourcesController.java +++ b/test-resources-server/src/main/java/io/micronaut/testresources/server/TestResourcesController.java @@ -22,6 +22,7 @@ import io.micronaut.http.annotation.Post; import io.micronaut.runtime.server.EmbeddedServer; import io.micronaut.scheduling.TaskExecutors; +import io.micronaut.scheduling.TaskScheduler; import io.micronaut.scheduling.annotation.ExecuteOn; import io.micronaut.testresources.core.ResolverLoader; import io.micronaut.testresources.core.TestResourcesResolutionException; @@ -33,6 +34,7 @@ import java.io.Closeable; import java.io.IOException; +import java.time.Duration; import java.util.Collection; import java.util.Collections; import java.util.List; @@ -57,15 +59,18 @@ public class TestResourcesController implements TestResourcesResolver { private final List propertyResolutionListeners; private final EmbeddedServer embeddedServer; private final ApplicationContext applicationContext; + private final TaskScheduler taskScheduler; public TestResourcesController(List propertyResolutionListeners, EmbeddedServer embeddedServer, ApplicationContext applicationContext, - ResolverLoader loader) { + ResolverLoader loader, + TaskScheduler taskScheduler) { this.propertyResolutionListeners = propertyResolutionListeners; this.embeddedServer = embeddedServer; this.applicationContext = applicationContext; this.loader = loader; + this.taskScheduler = taskScheduler; } /** @@ -235,8 +240,8 @@ public List listContainersByScope(@Nullable String scope) { * Requests a test resources service shutdown. */ @Post("/stop") - public void stopService() { - var makeSureServerIsStopped = new Thread(() -> { + public boolean stopService() { + taskScheduler.schedule(Duration.ofMillis(200), () -> { try { try { embeddedServer.stop(); @@ -252,8 +257,7 @@ public void stopService() { System.exit(0); } }); - makeSureServerIsStopped.setDaemon(true); - makeSureServerIsStopped.start(); + return true; } private void closeResolvers() {