mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
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)
This commit is contained in:
2 files changed
+115
-6
No files matched your search
+63
@@ -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();
|
||||
|
||||
Reference in new issue
Block a user