mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 22:11:36 -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
+52
-6
@@ -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();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user