fix(lambda): fix container lifecycle on timeout (rebase of #504) (#529)

* fix(lambda): release container handle on timeout instead of draining warm pool

* fix: fix container lifecycle on timeout

* fix(lambda): correct /next poll loop condition and restore response handling

Flip while loop to `invocation == null && !stopped` so the poll actually
runs (previous condition was unreachable because invocation starts null).
Restore the 204 response on null return and the 500 response on
InterruptedException so the handler never leaves the response open.

Without this fix, every RIC call to /next hangs, which also broke the
existing LambdaReactiveSyncIntegrationTest with SocketTimeout.

---------

Co-authored-by: Ricardo Lima <ricardo.hrlima@gmail.com>
This commit is contained in:
TiMandRicardo Lima authored and GitHub committed 2026-04-19 18:34:48 -05:00
1 parent 4955537de2
commit f837be0051
8 files changed
+379 -5

No files matched your search

@@ -107,7 +107,7 @@ public class LambdaExecutorService {
} catch (TimeoutException e) {
LOG.warnv("Function {0} timed out after {1}s", fn.getFunctionName(), fn.getTimeout());
warmPool.drainFunction(fn.getFunctionName());
warmPool.destroyHandle(handle);
return new InvokeResult(200, "Unhandled",
buildErrorPayload("Task timed out after " + fn.getTimeout() + " seconds", "Function.TimedOut"),
null, requestId);
@@ -160,6 +160,17 @@ public class WarmPool {
drainFunction(fn.getFunctionName());
}
/**
* Stops and removes a single container that is no longer usable (e.g. after a timeout).
* The container must have already been acquired (removed from the pool) so only a
* stop is needed — no pool bookkeeping required.
*/
public void destroyHandle(ContainerHandle handle) {
LOG.debugv("Destroying timed-out container {0} for function {1}",
handle.getContainerId(), handle.getFunctionName());
stopQuietly(handle);
}
/**
* Stops and removes all warm containers for the given function.
* Called on function delete or code update.
@@ -211,8 +211,8 @@ public class ContainerLauncher {
LOG.infov("Stopping container {0}", handle.getContainerId());
handle.setState(ContainerState.STOPPED);
lifecycleManager.stopAndRemove(handle.getContainerId(), handle.getLogStream());
handle.getRuntimeApiServer().stop();
lifecycleManager.stopAndRemove(handle.getContainerId(), handle.getLogStream());
}
private void copyDirToContainer(DockerClient dockerClient, String containerId,
@@ -38,6 +38,7 @@ public class RuntimeApiServer {
private final ConcurrentHashMap<String, PendingInvocation> inFlight = new ConcurrentHashMap<>();
private volatile HttpServer httpServer;
private volatile boolean stopped;
RuntimeApiServer(Vertx vertx, int port) {
this.vertx = vertx;
@@ -57,7 +58,10 @@ public class RuntimeApiServer {
// GET /runtime/invocation/next — long-poll for next invocation
router.get(NEXT_PATH).blockingHandler(ctx -> {
try {
PendingInvocation invocation = pendingQueue.poll(30, TimeUnit.SECONDS);
PendingInvocation invocation = null;
while (invocation == null && !stopped) {
invocation = pendingQueue.poll(30, TimeUnit.SECONDS);
}
if (invocation == null) {
ctx.response().setStatusCode(204).end();
return;
@@ -124,6 +128,7 @@ public class RuntimeApiServer {
}
public void stop() {
stopped = true;
if (httpServer != null) {
httpServer.close();
}