Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(lambda): await HttpServer close in RuntimeApiServer.stop() (#926)

Steve9e7cbe012f43
diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncher.java b/src/main/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncher.java
index e19b5d5ee7..220f5b5479 100644
--- a/src/main/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncher.java
+++ b/src/main/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncher.java
@@ -30,6 +30,9 @@ import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
/**
* Starts and stops Docker containers for Lambda function execution.
@@ -276,7 +279,14 @@ public class ContainerLauncher {
LOG.infov("Stopping container {0}", handle.getContainerId());
handle.setState(ContainerState.STOPPED);
- handle.getRuntimeApiServer().stop();
+ try {
+ handle.getRuntimeApiServer().stop().get(5, TimeUnit.SECONDS);
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ } catch (ExecutionException | TimeoutException e) {
+ LOG.warnv(e, "RuntimeApiServer did not close cleanly for container {0}",
+ handle.getContainerId());
+ }
lifecycleManager.stopAndRemove(handle.getContainerId(), handle.getLogStream());
}
diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/runtime/RuntimeApiServer.java b/src/main/java/io/github/hectorvent/floci/services/lambda/runtime/RuntimeApiServer.java
index 26925e940a..a240dddc9f 100644
--- a/src/main/java/io/github/hectorvent/floci/services/lambda/runtime/RuntimeApiServer.java
+++ b/src/main/java/io/github/hectorvent/floci/services/lambda/runtime/RuntimeApiServer.java
@@ -49,6 +49,7 @@ public class RuntimeApiServer {
private volatile HttpServer httpServer;
private volatile boolean stopped;
+ private volatile CompletableFuture<Void> closeFuture;
RuntimeApiServer(Vertx vertx, int port) {
this.vertx = vertx;
@@ -136,9 +137,10 @@ public class RuntimeApiServer {
.setMaxFormAttributeSize(-1));
httpServer.requestHandler(router).listen(port, "0.0.0.0", result -> {
if (result.succeeded()) {
- LOG.debugv("RuntimeApiServer started on port {0}", port);
+ LOG.infov("RuntimeApiServer started on port {0}", port);
started.complete(null);
} else {
+ LOG.errorv(result.cause(), "RuntimeApiServer failed to bind on port {0}", port);
started.completeExceptionally(result.cause());
}
});
@@ -146,10 +148,25 @@ public class RuntimeApiServer {
return started;
}
- public void stop() {
+ public synchronized CompletableFuture<Void> stop() {
+ if (closeFuture != null) {
+ return closeFuture;
+ }
stopped = true;
+ CompletableFuture<Void> closed = new CompletableFuture<>();
+ closeFuture = closed;
if (httpServer != null) {
- httpServer.close();
+ httpServer.close(ar -> {
+ if (ar.succeeded()) {
+ LOG.debugv("RuntimeApiServer on port {0} closed", port);
+ closed.complete(null);
+ } else {
+ LOG.warnv(ar.cause(), "RuntimeApiServer on port {0} failed to close cleanly", port);
+ closed.completeExceptionally(ar.cause());
+ }
+ });
+ } else {
+ closed.complete(null);
}
// Wake any parked /next pollers with 204 (container shutting down — runtime will exit).
@@ -175,6 +192,8 @@ public class RuntimeApiServer {
inv.getResultFuture().complete(
new InvokeResult(200, "Unhandled", CONTAINER_STOPPED_PAYLOAD, null, inv.getRequestId())));
inFlight.clear();
+
+ return closed;
}
public CompletableFuture<InvokeResult> enqueue(PendingInvocation invocation) {
diff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/runtime/RuntimeApiServerTest.java b/src/test/java/io/github/hectorvent/floci/services/lambda/runtime/RuntimeApiServerTest.java
index 5cddc80006..70d7a3b9b5 100644
--- a/src/test/java/io/github/hectorvent/floci/services/lambda/runtime/RuntimeApiServerTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/lambda/runtime/RuntimeApiServerTest.java
@@ -45,8 +45,8 @@ class RuntimeApiServerTest {
}
@AfterEach
- void tearDown() {
- server.stop();
+ void tearDown() throws Exception {
+ server.stop().get(5, TimeUnit.SECONDS);
scheduler.shutdownNow();
vertx.close();
}
@@ -223,6 +223,27 @@ class RuntimeApiServerTest {
assertTrue(new String(result.getPayload()).contains("ContainerStopped"));
}
+ @Test
+ @Timeout(10)
+ void stopReleasesPortSynchronously() throws Exception {
+ server.stop().get(5, TimeUnit.SECONDS);
+ new ServerSocket(port).close();
+ }
+
+ @Test
+ @Timeout(10)
+ void newServerOnSamePortAcceptsTrafficAfterStop() throws Exception {
+ server.stop().get(5, TimeUnit.SECONDS);
+
+ server = new RuntimeApiServer(vertx, port);
+ server.start().get(5, TimeUnit.SECONDS);
+
+ HttpResponse<String> resp = httpClient.send(
+ HttpRequest.newBuilder(URI.create("http://localhost:" + port + "/x")).GET().build(),
+ HttpResponse.BodyHandlers.ofString());
+ assertEquals(404, resp.statusCode());
+ }
+
private static int findFreePort() throws IOException {
try (ServerSocket socket = new ServerSocket(0)) {
return socket.getLocalPort();