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

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