Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(lambda): destroy container handle on interrupt and generic exception (#530)

TiM1b53e53dba5c
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<InvokeResult> 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"));
+ }
}