From 1b53e53dba5c3caae9ae00db1f98a4e5a5d4fd5b Mon Sep 17 00:00:00 2001 From: TiM Date: Mon, 20 Apr 2026 12:33:09 +1200 Subject: [PATCH] fix(lambda): destroy container handle on interrupt and generic exception (#530) Closes #525. Extends #529's timeout-path fix to the two remaining paths in LambdaExecutorService.executeSync that were still returning a potentially-dirty container to the warm pool. - InterruptedException: the container may still be processing the previous request, so the response from the old invocation could be returned to a new one. Destroy the handle instead of releasing it. - Exception (e.g. runtime API communication failure): container state is unknown. Destroy rather than trust it for reuse. Matches AWS Lambda behavior where a container is discarded after any internal error, not reused. Tests mirror the existing timeoutInvocation_destroysHandle_doesNotRelease pattern, one for each path. --- .../lambda/LambdaExecutorService.java | 4 +- .../lambda/LambdaExecutorServiceTest.java | 56 +++++++++++++++++++ 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaExecutorService.java b/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaExecutorService.java index d1966141dc..aa6a5aab40 100644 --- a/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaExecutorService.java +++ b/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaExecutorService.java @@ -113,11 +113,11 @@ public class LambdaExecutorService { null, requestId); } catch (InterruptedException e) { Thread.currentThread().interrupt(); - warmPool.release(handle); + warmPool.destroyHandle(handle); return new InvokeResult(200, "Unhandled", buildErrorPayload("Invocation interrupted", "Interrupted"), null, requestId); } catch (Exception e) { LOG.warnv("Invocation error for function {0}: {1}", fn.getFunctionName(), e.getMessage()); - warmPool.release(handle); + warmPool.destroyHandle(handle); return new InvokeResult(200, "Unhandled", buildErrorPayload(e.getMessage(), "InvocationError"), null, requestId); } } diff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaExecutorServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaExecutorServiceTest.java index 57481947c6..4483aa1a1d 100644 --- a/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaExecutorServiceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaExecutorServiceTest.java @@ -17,6 +17,9 @@ import org.mockito.junit.jupiter.MockitoSettings; import org.mockito.quality.Strictness; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicReference; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -118,4 +121,57 @@ class LambdaExecutorServiceTest { verify(warmPool, never()).destroyHandle(any()); assertEquals(204, result.getStatusCode()); } + + @Test + void interruptedInvocation_destroysHandle_doesNotRelease() throws Exception { + fn.setTimeout(30); + RuntimeApiServer rtas = mock(RuntimeApiServer.class); + ContainerHandle handle = new ContainerHandle("cid-int", "test-fn", rtas, ContainerState.WARM); + + when(warmPool.acquire(any())).thenReturn(handle); + CountDownLatch enqueued = new CountDownLatch(1); + doAnswer(inv -> { + PendingInvocation pi = inv.getArgument(0); + enqueued.countDown(); + return pi.getResultFuture(); + }).when(rtas).enqueue(any(PendingInvocation.class)); + + AtomicReference resultRef = new AtomicReference<>(); + Thread worker = new Thread(() -> resultRef.set( + executor.invoke(fn, "{}".getBytes(), InvocationType.RequestResponse))); + worker.start(); + + assertTrue(enqueued.await(5, TimeUnit.SECONDS), "enqueue never called"); + worker.interrupt(); + worker.join(5_000); + + InvokeResult result = resultRef.get(); + assertNotNull(result, "invoke did not return"); + verify(warmPool).destroyHandle(handle); + verify(warmPool, never()).release(handle); + assertEquals(200, result.getStatusCode()); + assertEquals("Unhandled", result.getFunctionError()); + assertTrue(new String(result.getPayload()).contains("Interrupted")); + } + + @Test + void exceptionDuringInvocation_destroysHandle_doesNotRelease() { + RuntimeApiServer rtas = mock(RuntimeApiServer.class); + ContainerHandle handle = new ContainerHandle("cid-exc", "test-fn", rtas, ContainerState.WARM); + + when(warmPool.acquire(any())).thenReturn(handle); + doAnswer(inv -> { + PendingInvocation pi = inv.getArgument(0); + pi.getResultFuture().completeExceptionally(new RuntimeException("runtime crash")); + return pi.getResultFuture(); + }).when(rtas).enqueue(any(PendingInvocation.class)); + + InvokeResult result = executor.invoke(fn, "{}".getBytes(), InvocationType.RequestResponse); + + verify(warmPool).destroyHandle(handle); + verify(warmPool, never()).release(handle); + assertEquals(200, result.getStatusCode()); + assertEquals("Unhandled", result.getFunctionError()); + assertTrue(new String(result.getPayload()).contains("InvocationError")); + } }