Light, fluffy, and always free - The AWS Local Emulator alternative
fix(lambda): wake blocked /next poller and drain queue on RuntimeApiServer.stop() (#531)
TiM79a7a04287cd
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.javaindex 58d7ccf72e..b997585ac5 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@@ -32,6 +32,14 @@ public class RuntimeApiServer {private static final String ERROR_PATH = "/" + RUNTIME_API_VERSION + "/runtime/invocation/:requestId/error";private static final String INIT_ERROR_PATH = "/" + RUNTIME_API_VERSION + "/runtime/init/error";+ private static final byte[] CONTAINER_STOPPED_PAYLOAD =+ "{\"errorMessage\":\"Container stopped\",\"errorType\":\"ContainerStopped\"}".getBytes();++ // Sentinel used to wake a /next handler blocked in pendingQueue.poll() when stop() is called.+ // Compared by identity (==), not equals.+ private static final PendingInvocation SHUTDOWN_SENTINEL =+ new PendingInvocation("<shutdown>", new byte[0], 0L, "", new CompletableFuture<>());+private final Vertx vertx;private final int port;private final LinkedBlockingQueue<PendingInvocation> pendingQueue = new LinkedBlockingQueue<>();@@ -61,6 +69,17 @@ public class RuntimeApiServer {PendingInvocation invocation = null;while (invocation == null && !stopped) {invocation = pendingQueue.poll(30, TimeUnit.SECONDS);+ if (invocation == SHUTDOWN_SENTINEL) {+ invocation = null;+ break;+ }+ }+ // If stop() ran after poll() returned a real invocation (narrow race before+ // inFlight.put), complete it here so the caller doesn't hang.+ if (invocation != null && stopped) {+ invocation.getResultFuture().complete(+ new InvokeResult(200, "Unhandled", CONTAINER_STOPPED_PAYLOAD, null, invocation.getRequestId()));+ invocation = null;}if (invocation == null) {ctx.response().setStatusCode(204).end();@@ -132,17 +151,44 @@ public class RuntimeApiServer {if (httpServer != null) {httpServer.close();}- // Complete any in-flight invocations with error- inFlight.values().forEach(inv -> {- byte[] errorPayload = "{\"errorMessage\":\"Container stopped\",\"errorType\":\"ContainerStopped\"}".getBytes();- inv.getResultFuture().complete(new InvokeResult(200, "Unhandled", errorPayload, null, inv.getRequestId()));- });++ // Drain queued invocations that were never consumed by /next, completing each future+ // with ContainerStopped so callers (LambdaExecutorService) don't hang waiting for a result.+ PendingInvocation pending;+ while ((pending = pendingQueue.poll()) != null) {+ if (pending == SHUTDOWN_SENTINEL) {+ continue;+ }+ pending.getResultFuture().complete(+ new InvokeResult(200, "Unhandled", CONTAINER_STOPPED_PAYLOAD, null, pending.getRequestId()));+ }++ // Offer sentinel AFTER drain so it cannot be discarded. Wakes any /next handler+ // currently blocked in poll(). N=1 today: a single RuntimeApiServer serves one container,+ // so one sentinel suffices.+ pendingQueue.offer(SHUTDOWN_SENTINEL);++ // Complete any in-flight invocations with error.+ inFlight.values().forEach(inv ->+ inv.getResultFuture().complete(+ new InvokeResult(200, "Unhandled", CONTAINER_STOPPED_PAYLOAD, null, inv.getRequestId())));inFlight.clear();- pendingQueue.clear();}public CompletableFuture<InvokeResult> enqueue(PendingInvocation invocation) {+ if (stopped) {+ // stop() already ran; don't queue. Complete immediately so caller doesn't hang.+ invocation.getResultFuture().complete(+ new InvokeResult(200, "Unhandled", CONTAINER_STOPPED_PAYLOAD, null, invocation.getRequestId()));+ return invocation.getResultFuture();+ }pendingQueue.offer(invocation);+ // Close the check-then-offer race: if stop() ran between the guard and offer(),+ // the drain is done and our invocation would sit forever. Remove and complete.+ if (stopped && pendingQueue.remove(invocation)) {+ invocation.getResultFuture().complete(+ new InvokeResult(200, "Unhandled", CONTAINER_STOPPED_PAYLOAD, null, invocation.getRequestId()));+ }return invocation.getResultFuture();}}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.javaindex b7c362b8b4..cfebaec8f6 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@@ -134,6 +134,69 @@ class RuntimeApiServerTest {assertTrue(payload.contains("ContainerStopped"));}+ @Test+ @Timeout(15)+ void stopWakesBlockedPollerImmediately() throws Exception {+ // GET /next on a background thread — blocks in pendingQueue.poll(30s).+ HttpRequest request = HttpRequest.newBuilder()+ .uri(URI.create("http://localhost:" + port + "/2018-06-01/runtime/invocation/next"))+ .GET()+ .build();+ CompletableFuture<HttpResponse<String>> asyncResponse =+ httpClient.sendAsync(request, HttpResponse.BodyHandlers.ofString());++ // Give the handler time to enter poll()+ Thread.sleep(500);+ assertFalse(asyncResponse.isDone(), "handler should be blocked in poll");++ long start = System.currentTimeMillis();+ server.stop();+ HttpResponse<String> response = asyncResponse.get(2, TimeUnit.SECONDS);+ long elapsed = System.currentTimeMillis() - start;++ // 204 requires the handler to have: woken from poll, observed sentinel, exited loop,+ // written the response. Proves the worker thread is released (not just the socket closed).+ assertEquals(204, response.statusCode());+ assertTrue(elapsed < 1000, "stop() should wake poller in <1s, took " + elapsed + "ms");+ }++ @Test+ @Timeout(15)+ void stopCompletesQueuedInvocationsWithContainerStopped() throws Exception {+ // Enqueue an invocation, but never call /next — it sits in pendingQueue.+ PendingInvocation invocation = new PendingInvocation(+ "req-queued", "{}".getBytes(), System.currentTimeMillis() + 60_000,+ "arn:aws:lambda:us-east-1:000000000000:function:test",+ new CompletableFuture<>());+ server.enqueue(invocation);++ // stop() must drain the queue and complete the future — not discard it silently.+ server.stop();++ InvokeResult result = invocation.getResultFuture().get(2, TimeUnit.SECONDS);+ assertNotNull(result);+ assertEquals("Unhandled", result.getFunctionError());+ assertTrue(new String(result.getPayload()).contains("ContainerStopped"));+ }++ @Test+ @Timeout(15)+ void enqueueAfterStopCompletesImmediately() throws Exception {+ server.stop();++ PendingInvocation invocation = new PendingInvocation(+ "req-late", "{}".getBytes(), System.currentTimeMillis() + 60_000,+ "arn:aws:lambda:us-east-1:000000000000:function:test",+ new CompletableFuture<>());+ server.enqueue(invocation);++ // Future is completed synchronously by enqueue() when stopped, so no /next is needed.+ assertTrue(invocation.getResultFuture().isDone(), "future should be already done");+ InvokeResult result = invocation.getResultFuture().get(0, TimeUnit.SECONDS);+ assertEquals("Unhandled", result.getFunctionError());+ assertTrue(new String(result.getPayload()).contains("ContainerStopped"));+ }+private static int findFreePort() throws IOException {try (ServerSocket socket = new ServerSocket(0)) {return socket.getLocalPort();