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

* test(lambda): assert RuntimeApiServer.stop() releases its port synchronously

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

stop() returned before httpServer.close() completed. Under warm-pool
churn the PortAllocator could hand the just-released port to a new
server while the kernel still held the old socket in LISTEN, producing
a ghost listener that bound but never accepted - Lambda bootstraps
then timed out polling AWS_LAMBDA_RUNTIME_API.

stop() now returns CompletableFuture<Void>; ContainerLauncher awaits
it. Idempotent via a cached future.
This commit is contained in:
Steve authored and GitHub committed 2026-05-19 00:43:33 -05:00
1 parent b8d1b817c9
commit 9e7cbe012f
3 files changed
+56 -6

No files matched your search

@@ -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());
}
@@ -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) {