Skip to content

Commit

Permalink
Workaround for shutdown error (#722)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
melix authored Sep 26, 2024
1 parent 570a634 commit 8616bf5
Showing 1 changed file with 9 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -57,15 +59,18 @@ public class TestResourcesController implements TestResourcesResolver {
private final List<PropertyResolutionListener> propertyResolutionListeners;
private final EmbeddedServer embeddedServer;
private final ApplicationContext applicationContext;
private final TaskScheduler taskScheduler;

public TestResourcesController(List<PropertyResolutionListener> 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;
}

/**
Expand Down Expand Up @@ -235,8 +240,8 @@ public List<TestContainer> 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();
Expand All @@ -252,8 +257,7 @@ public void stopService() {
System.exit(0);
}
});
makeSureServerIsStopped.setDaemon(true);
makeSureServerIsStopped.start();
return true;
}

private void closeResolvers() {
Expand Down

0 comments on commit 8616bf5

Please sign in to comment.