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.
This commit is contained in:
TiM authored and GitHub committed 2026-04-19 19:33:09 -05:00
1 parent 571d6d7a0d
commit 1b53e53dba
2 files changed
+58 -2

No files matched your search

@@ -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);
}
}
@@ -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"));
}
}