From 79a7a04287cdedfe448e606069138be7233ba8c1 Mon Sep 17 00:00:00 2001 From: TiM Date: Mon, 20 Apr 2026 16:44:24 +1200 Subject: [PATCH] fix(lambda): wake blocked /next poller and drain queue on RuntimeApiServer.stop() (#531) * fix(lambda): wake blocked /next poller and drain queue on RuntimeApiServer.stop() RuntimeApiServer.stop() had two independent shutdown bugs: 1. Blocked /next handler not woken. The long-poll loop `while (invocation == null && !stopped)` only checks `stopped` after `pendingQueue.poll(30s)` returns. stop() sets stopped=true but never nudges the queue, so a blocked worker holds a Vert.x thread for up to 30s per container tear-down. 2. Queued invocations dropped without completing futures. stop() ended with `pendingQueue.clear()`, silently discarding any invocation that was enqueued but not yet consumed by /next. LambdaExecutorService was already awaiting that resultFuture; the caller hung until its own timeout fired. Fix: - Add SHUTDOWN_SENTINEL, offered AFTER the drain so it cannot be discarded by clear(). /next recognises it by identity, exits the loop, returns 204. - Replace `pendingQueue.clear()` with a drain loop that completes each queued invocation's future with ContainerStopped. - Harden enqueue() to short-circuit when stopped, with a post-offer recheck closing the check-then-offer race. - Add a post-loop `stopped` check in the /next handler so an invocation polled just before stop() gets completed locally rather than parked in inFlight after cleanup. Keeps `poll(30s)` rather than `take()` as a belt-and-braces timeout if the sentinel is ever missed. Tests: three new RuntimeApiServerTest cases cover sentinel wake, queue-drain completion, and enqueue-after-stop short-circuit. Related: #485 (worker-pool RCA), #504 (original, closed), #529 (rebase), #530 (interrupt follow-up). * ci: retrigger (SecretsManagerServiceTest flake on main, not related to this PR) --- .../lambda/runtime/RuntimeApiServer.java | 58 +++++++++++++++-- .../lambda/runtime/RuntimeApiServerTest.java | 63 +++++++++++++++++++ 2 files changed, 115 insertions(+), 6 deletions(-) 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 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("", new byte[0], 0L, "", new CompletableFuture<>()); + private final Vertx vertx; private final int port; private final LinkedBlockingQueue 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 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.java index 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> 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 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();