feat(lambda): add init-timeout-seconds so a cold runtime gets its own init budget (#6442)

* feat(lambda): add init-timeout-seconds so a cold runtime gets its own init budget

* fix(lambda): count the init budget from container start, apply it only until the first /next poll, and cover it in SQS visibility

* test(lambda): check the dispatch wait directly instead of on the clock, and correct the init budget docs
This commit is contained in:
floverfelt authored and GitHub committed 2026-10-25 14:03:34 -04:00
1 parent 97de815256
commit 4d35617518
13 files changed
+205 -20

No files matched your search

@@ -217,6 +217,39 @@ class LambdaExecutorServiceTest {
assertNull(result.getFunctionError());
}
@Test
void dispatchWait_coldRuntimeGetsTheInitBudget() {
LambdaExecutorService withBudget = new LambdaExecutorService(warmPool, new ObjectMapper(), concurrencyLimiter, 10);
try {
assertEquals(10_000, withBudget.dispatchWaitMs(1, true, 50_000, 50_000));
} finally {
withBudget.shutdown();
}
}
@Test
void dispatchWait_warmRuntimeGetsTheTimeoutPlusGraceNotTheInitBudget() {
LambdaExecutorService withBudget = new LambdaExecutorService(warmPool, new ObjectMapper(), concurrencyLimiter, 10);
try {
assertEquals(3_000, withBudget.dispatchWaitMs(1, false, 50_000, 50_000));
} finally {
withBudget.shutdown();
}
}
@Test
void dispatchWait_lateStartedRuntimeGetsWhatIsLeftOfTheInitBudget() {
LambdaExecutorService withBudget = new LambdaExecutorService(warmPool, new ObjectMapper(), concurrencyLimiter, 10);
try {
// The launcher spent 4 of the 10 seconds after the start (extensions, log attach).
assertEquals(6_000, withBudget.dispatchWaitMs(1, true, 50_000, 54_000));
// Once less is left than Timeout plus grace, that wait applies instead.
assertEquals(3_000, withBudget.dispatchWaitMs(1, true, 50_000, 59_000));
} finally {
withBudget.shutdown();
}
}
@Test
void resultAfterConfiguredDeadline_timesOutAndDestroysHandle() {
RuntimeApiServer rtas = mock(RuntimeApiServer.class);
@@ -640,6 +640,37 @@ class SqsEventSourcePollerTest {
assertEquals(120 + 60 + 30, visibility.getValue());
}
@Test
void visibilityTimeoutAlsoCoversTheInitBudget() {
EmulatorConfig config = mock(EmulatorConfig.class);
EmulatorConfig.ServicesConfig services = mock(EmulatorConfig.ServicesConfig.class);
EmulatorConfig.LambdaServiceConfig lambdaConfig = mock(EmulatorConfig.LambdaServiceConfig.class);
when(config.services()).thenReturn(services);
when(services.lambda()).thenReturn(lambdaConfig);
when(lambdaConfig.pollIntervalMs()).thenReturn(1000L);
when(lambdaConfig.initTimeoutSeconds()).thenReturn(120);
when(config.effectiveBaseUrl()).thenReturn("http://localhost:4566");
SqsEventSourcePoller slowInitPoller = new SqsEventSourcePoller(mock(Vertx.class), sqsService,
executorService, new LambdaTargetResolver(functionStore, aliasStore), mock(EsmStore.class),
config, OBJECT_MAPPER, new PipesFilterMatcher(OBJECT_MAPPER));
EventSourceMapping esm = esm();
LambdaFunction fn = new LambdaFunction();
fn.setFunctionName("throwfn");
fn.setTimeout(3);
when(functionStore.getForAccount("000000000000", "us-east-1", "throwfn"))
.thenReturn(Optional.of(fn));
when(sqsService.receiveMessage(eq(esm.getQueueUrl()), anyInt(), anyInt(), anyInt(), eq("us-east-1")))
.thenReturn(List.of());
slowInitPoller.pollAndInvoke(esm);
ArgumentCaptor<Integer> visibility = ArgumentCaptor.forClass(Integer.class);
verify(sqsService, timeout(2000)).receiveMessage(
eq(esm.getQueueUrl()), anyInt(), visibility.capture(), anyInt(), eq("us-east-1"));
// up to 120s for a cold runtime to request the invocation, then 3s executing, plus the 30s margin
assertEquals(3 + 120 + 30, visibility.getValue());
}
@Test
void turningTheBatchingWindowOffDeliversAnAlreadyBufferedBatch() {
LambdaFunction fn = stubThrowFn();
@@ -86,6 +86,21 @@ class RuntimeApiServerTest {
+ server.waitingContextsSize());
}
@Test
@Timeout(10)
void nextEndpoint_firstPollEndsTheInitPhase() throws Exception {
assertFalse(server.hasRuntimePolled());
httpClient.sendAsync(HttpRequest.newBuilder()
.uri(URI.create("http://localhost:" + port + "/2018-06-01/runtime/invocation/next"))
.GET()
.build(),
HttpResponse.BodyHandlers.ofString());
awaitWaitingContexts(1);
assertTrue(server.hasRuntimePolled());
}
@Test
@Timeout(15)
void nextEndpoint_blocksUntilInvocationArrives() throws Exception {