From bc951bf4bfac3c4fa4934e95c0497582e50644ee Mon Sep 17 00:00:00 2001 From: Kenshin Okinaka Date: Sun, 19 Apr 2026 23:33:54 +0900 Subject: [PATCH] fix(lambda): restore create-copy-start ordering for provided runtimes (#524) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(lambda): restore create-copy-start ordering for provided runtimes The refactor in #466 changed ContainerLauncher from create → copy → start to createAndStart → copy, breaking provided.* runtimes whose entrypoint execs /var/runtime/bootstrap immediately on start — before the file has been copied in. Split ContainerLifecycleManager into create(spec) and startCreated(containerId, spec) so ContainerLauncher can copy code and the bootstrap file into the container filesystem before starting it. Other services (ECS, ECR, EKS, MSK, ElastiCache, RDS) continue using createAndStart since they have no pre-start file copy requirement. Fixes #494 * test(lambda): verify create-copy-start ordering for provided runtimes Update ContainerLauncherTest to use the new create/startCreated API and add two ordering tests: - launchFunction_createsBeforeCopyAndStartsAfter: verifies the create → copy → startCreated sequence for any runtime using Mockito InOrder. - launchProvidedRuntime_copiesBootstrapBeforeStart: verifies the same ordering specifically for provided.al2023 with a bootstrap file, and asserts createAndStart is never called. This is the exact regression from #466 that caused exit 127. * refactor: delegate createAndStart to create + startCreated Eliminate duplicated container-creation logic by making createAndStart a thin delegate to create(spec) + startCreated(containerId, spec). Future fixes to either step now apply to both call paths automatically. * test(lambda): add SDK compatibility test for provided runtime invoke Create and invoke a provided.al2023 function using the shell-based bootstrap from LambdaUtils.providedRuntimeZip(). Asserts the response contains the expected payload without timeout or FunctionError. This test fails on 1.5.4 (Read timed out after 120s due to the create-start ordering regression in #466) and passes on the fix. * docs: add @param to create() and rename misleading test - Add missing @param spec tag to ContainerLifecycleManager.create() for consistency with surrounding methods. - Rename launchFunctionCopiesCode to launchFunction_createsWithoutBindMounts to reflect what the test actually asserts. Remove the getDockerClient verify that didn't prove code copying. * test(lambda): stub Docker copy chain and add cleanup for provided runtime test - Stub dockerClient.copyArchiveToContainerCmd → withRemotePath → withTarInputStream so copy operations execute against the mock instead of throwing silently. Ordering tests now verify actual Docker copy invocations between create and startCreated. - Provided runtime test asserts both /var/task and /var/runtime remote paths were targeted. - Add sdk-test-provided-fn to @AfterAll cleanup in LambdaTest so assertion failures don't leak the function across test runs. * test(lambda): drain tar InputStream in copy mock to prevent thread leaks Each copy mock now returns a fresh CopyArchiveToContainerCmd that drains the PipedInputStream on exec(), so the background tar writer thread can complete instead of blocking when the pipe buffer fills. Remote paths are captured via the mock's withRemotePath answer and asserted from the shared list. Also adds sdk-test-provided-fn to @AfterAll cleanup in the compat test to prevent function leaks on assertion failure. * refactor(lambda): drop unused ContainerInfo variable from launch The return value of startCreated() was assigned but never read. Remove the variable and its now-unused import. * test(lambda): include getDockerClient in InOrder verification Add the getDockerClient() call between create() and copyArchiveToContainerCmd() in both ordering tests so the verified sequence matches the actual call flow in ContainerLauncher.launch(). * style: remove redundant doAnswer import The wildcard import already covers it. --- .../test/java/com/floci/test/LambdaTest.java | 38 ++++++ .../docker/ContainerLifecycleManager.java | 40 +++++-- .../lambda/launcher/ContainerLauncher.java | 10 +- .../launcher/ContainerLauncherTest.java | 113 +++++++++++++++--- 4 files changed, 168 insertions(+), 33 deletions(-) diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/LambdaTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/LambdaTest.java index a083ac2ca8..4e32bc5a94 100644 --- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/LambdaTest.java +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/LambdaTest.java @@ -33,6 +33,10 @@ class LambdaTest { lambda.deleteFunction(DeleteFunctionRequest.builder() .functionName("sdk-test-ruby-fn").build()); } catch (Exception ignored) {} + try { + lambda.deleteFunction(DeleteFunctionRequest.builder() + .functionName("sdk-test-provided-fn").build()); + } catch (Exception ignored) {} lambda.close(); } } @@ -306,6 +310,40 @@ class LambdaTest { @Test @Order(17) + void providedRuntimeInvoke() { + String providedFn = "sdk-test-provided-fn"; + + lambda.createFunction(CreateFunctionRequest.builder() + .functionName(providedFn) + .runtime(Runtime.PROVIDED_AL2023) + .role(ROLE) + .handler("bootstrap") + .timeout(30) + .code(FunctionCode.builder() + .zipFile(SdkBytes.fromByteArray(LambdaUtils.providedRuntimeZip())) + .build()) + .build()); + + // RequestResponse invoke — the bootstrap shell script POSTs a + // response via the Runtime API, so we should get a real payload + // back rather than a timeout. + InvokeResponse response = lambda.invoke(InvokeRequest.builder() + .functionName(providedFn) + .invocationType(InvocationType.REQUEST_RESPONSE) + .payload(SdkBytes.fromUtf8String("{\"test\":true}")) + .build()); + + assertThat(response.statusCode()).isEqualTo(200); + assertThat(response.functionError()).isNull(); + String payload = response.payload().asUtf8String(); + assertThat(payload).contains("hello from provided runtime"); + + lambda.deleteFunction(DeleteFunctionRequest.builder() + .functionName(providedFn).build()); + } + + @Test + @Order(18) void rubyRuntimeSupport() { String rubyFn = "sdk-test-ruby-fn"; diff --git a/src/main/java/io/github/hectorvent/floci/core/common/docker/ContainerLifecycleManager.java b/src/main/java/io/github/hectorvent/floci/core/common/docker/ContainerLifecycleManager.java index ebeb1eb1ef..1bc852827f 100644 --- a/src/main/java/io/github/hectorvent/floci/core/common/docker/ContainerLifecycleManager.java +++ b/src/main/java/io/github/hectorvent/floci/core/common/docker/ContainerLifecycleManager.java @@ -48,22 +48,33 @@ public class ContainerLifecycleManager { } /** - * Creates and starts a container from the given specification. - * Automatically pulls the image if not present locally. + * Creates and immediately starts a container. Delegates to + * {@link #create} and {@link #startCreated}. Suitable when no + * filesystem modifications are needed between creation and start. * * @param spec the container specification * @return information about the created container including resolved endpoints */ public ContainerInfo createAndStart(ContainerSpec spec) { + String containerId = create(spec); + return startCreated(containerId, spec); + } + + /** + * Creates a container without starting it. Use {@link #startCreated} to + * start it after any pre-start setup (e.g. copying files into the + * container filesystem). + * + * @param spec the container specification + * @return the container ID + */ + public String create(ContainerSpec spec) { LOG.debugv("Creating container from spec: image={0}, name={1}", spec.image(), spec.name()); - // Ensure image is available imageCacheService.ensureImageExists(spec.image()); - // Build HostConfig HostConfig hostConfig = buildHostConfig(spec); - // Create container command CreateContainerCmd createCmd = dockerClient.createContainerCmd(spec.image()) .withHostConfig(hostConfig); @@ -86,18 +97,23 @@ public class ContainerLifecycleManager { createCmd.withExposedPorts(exposed); } - // Create the container CreateContainerResponse response = createCmd.exec(); String containerId = response.getId(); - LOG.infov("Created container {0} (name={1})", containerId, spec.name()); + LOG.infov("Created container {0} (name={1}, not yet started)", containerId, spec.name()); + return containerId; + } - // Start the container + /** + * Starts a previously created container and resolves its endpoints. + * + * @param containerId the container ID returned by {@link #create} + * @param spec the original spec (needed for network and endpoint resolution) + * @return information about the running container including resolved endpoints + */ + public ContainerInfo startCreated(String containerId, ContainerSpec spec) { dockerClient.startContainerCmd(containerId).exec(); LOG.infov("Started container {0}", containerId); - // For containers with port bindings, withNetworkMode was skipped during creation - // (it suppresses port publishing on macOS Docker Desktop). Connect to the - // configured network now, after the host port bindings are established. if (spec.networkMode() != null && !spec.networkMode().isBlank() && spec.hasPortBindings()) { try { dockerClient.connectToNetworkCmd() @@ -111,9 +127,7 @@ public class ContainerLifecycleManager { } } - // Resolve endpoints Map endpoints = resolveEndpoints(containerId, spec); - return new ContainerInfo(containerId, endpoints); } diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncher.java b/src/main/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncher.java index f59f23fe38..e0597486cf 100644 --- a/src/main/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncher.java +++ b/src/main/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncher.java @@ -3,7 +3,6 @@ package io.github.hectorvent.floci.services.lambda.launcher; import io.github.hectorvent.floci.config.EmulatorConfig; import io.github.hectorvent.floci.core.common.docker.ContainerBuilder; import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager; -import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager.ContainerInfo; import io.github.hectorvent.floci.core.common.docker.ContainerLogStreamer; import io.github.hectorvent.floci.core.common.docker.ContainerSpec; import io.github.hectorvent.floci.core.common.docker.DockerHostResolver; @@ -165,9 +164,9 @@ public class ContainerLauncher { ContainerSpec spec = specBuilder.build(); - // Create container (but don't start yet - need to copy code first for Zip packages) - ContainerInfo info = lifecycleManager.createAndStart(spec); - String containerId = info.containerId(); + // Create container without starting — provided.* runtimes exec + // /var/runtime/bootstrap on start, so code must be copied first. + String containerId = lifecycleManager.create(spec); LOG.infov("Created container {0} for function {1}", containerId, fn.getFunctionName()); // Copy code into container via Docker API tar stream (works inside Docker too) @@ -190,6 +189,9 @@ public class ContainerLauncher { } } + // Now start the container with code in place + lifecycleManager.startCreated(containerId, spec); + ContainerHandle handle = new ContainerHandle(containerId, fn.getFunctionName(), runtimeApiServer, ContainerState.WARM); // Determine CloudWatch Logs destination for this container instance diff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncherTest.java b/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncherTest.java index cbb25289e3..90dafb9888 100644 --- a/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncherTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncherTest.java @@ -11,23 +11,24 @@ import io.github.hectorvent.floci.services.lambda.model.LambdaFunction; import io.github.hectorvent.floci.services.lambda.runtime.RuntimeApiServer; import io.github.hectorvent.floci.services.lambda.runtime.RuntimeApiServerFactory; import com.github.dockerjava.api.DockerClient; -import com.github.dockerjava.api.model.Bind; +import com.github.dockerjava.api.command.CopyArchiveToContainerCmd; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.api.io.TempDir; import org.mockito.ArgumentCaptor; +import org.mockito.InOrder; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; -import org.junit.jupiter.api.io.TempDir; import java.nio.file.Files; import java.nio.file.Path; import java.util.Map; import java.util.Optional; -import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.*; @ExtendWith(MockitoExtension.class) @@ -47,15 +48,15 @@ class ContainerLauncherTest { Path tempDir; ContainerLauncher launcher; - ContainerBuilder containerBuilder; + /** Collects remote paths passed to withRemotePath across all copy mocks. */ + final java.util.List capturedRemotePaths = new java.util.ArrayList<>(); @BeforeEach void setUp() { - // Setup config mocks EmulatorConfig.ServicesConfig services = mock(EmulatorConfig.ServicesConfig.class); EmulatorConfig.LambdaServiceConfig lambda = mock(EmulatorConfig.LambdaServiceConfig.class); EmulatorConfig.DockerConfig docker = mock(EmulatorConfig.DockerConfig.class); - + when(config.services()).thenReturn(services); when(services.lambda()).thenReturn(lambda); when(lambda.dockerNetwork()).thenReturn(Optional.empty()); @@ -63,23 +64,52 @@ class ContainerLauncherTest { when(docker.logMaxSize()).thenReturn("10m"); when(docker.logMaxFile()).thenReturn("3"); - containerBuilder = new ContainerBuilder(config, dockerHostResolver); + ContainerBuilder containerBuilder = new ContainerBuilder(config, dockerHostResolver); launcher = new ContainerLauncher(containerBuilder, lifecycleManager, logStreamer, imageResolver, runtimeApiServerFactory, dockerHostResolver, config, ecrRegistryManager); when(runtimeApiServerFactory.create()).thenReturn(runtimeApiServer); when(runtimeApiServer.getPort()).thenReturn(9000); when(dockerHostResolver.resolve()).thenReturn("127.0.0.1"); - - ContainerLifecycleManager.ContainerInfo info = new ContainerLifecycleManager.ContainerInfo("c1", Map.of()); - when(lifecycleManager.createAndStart(any())).thenReturn(info); + + when(lifecycleManager.create(any())).thenReturn("container-123"); + ContainerLifecycleManager.ContainerInfo info = + new ContainerLifecycleManager.ContainerInfo("container-123", Map.of()); + when(lifecycleManager.startCreated(eq("container-123"), any())).thenReturn(info); when(lifecycleManager.getDockerClient()).thenReturn(dockerClient); + + // Stub the Docker copy chain so copyDirToContainer / copyFileToContainer + // don't throw when the mock DockerClient is used. Each invocation + // returns a fresh mock that drains the tar InputStream on exec() to + // prevent the background PipedOutputStream writer thread from blocking + // when the pipe buffer fills. + capturedRemotePaths.clear(); + when(dockerClient.copyArchiveToContainerCmd(any())).thenAnswer(inv -> { + CopyArchiveToContainerCmd cmd = mock(CopyArchiveToContainerCmd.class); + final java.io.InputStream[] captured = {null}; + when(cmd.withRemotePath(any())).thenAnswer(pathInv -> { + capturedRemotePaths.add(pathInv.getArgument(0)); + return cmd; + }); + when(cmd.withTarInputStream(any())).thenAnswer(streamInv -> { + captured[0] = streamInv.getArgument(0); + return cmd; + }); + doAnswer(execInv -> { + if (captured[0] != null) { + try { captured[0].transferTo(java.io.OutputStream.nullOutputStream()); } + catch (Exception ignored) {} + } + return null; + }).when(cmd).exec(); + return cmd; + }); } @Test - void launchFunctionCopiesCode() throws Exception { + void launchFunction_createsWithoutBindMounts() throws Exception { Path codePath = Files.createDirectory(tempDir.resolve("code")); - + LambdaFunction fn = new LambdaFunction(); fn.setFunctionName("standard-fn"); fn.setRuntime("nodejs20.x"); @@ -89,12 +119,63 @@ class ContainerLauncherTest { launcher.launch(fn); ArgumentCaptor specCaptor = ArgumentCaptor.forClass(ContainerSpec.class); - verify(lifecycleManager).createAndStart(specCaptor.capture()); + verify(lifecycleManager).create(specCaptor.capture()); ContainerSpec spec = specCaptor.getValue(); assertTrue(spec.binds().isEmpty(), "Function should NOT have bind mounts"); - - // Function should attempt to copy code - verify(lifecycleManager, atLeastOnce()).getDockerClient(); + } + + @Test + void launchFunction_createsBeforeCopyAndStartsAfter() throws Exception { + Path codePath = Files.createDirectory(tempDir.resolve("code")); + + LambdaFunction fn = new LambdaFunction(); + fn.setFunctionName("order-fn"); + fn.setRuntime("nodejs20.x"); + fn.setHandler("index.handler"); + fn.setCodeLocalPath(codePath.toString()); + + launcher.launch(fn); + + // Verify ordering: create → getDockerClient → Docker copy (to /var/task) → startCreated + InOrder inOrder = inOrder(lifecycleManager, dockerClient); + inOrder.verify(lifecycleManager).create(any()); + inOrder.verify(lifecycleManager).getDockerClient(); + inOrder.verify(dockerClient).copyArchiveToContainerCmd("container-123"); + inOrder.verify(lifecycleManager).startCreated(eq("container-123"), any()); + + // createAndStart must NOT be called — Lambda uses the split path + verify(lifecycleManager, never()).createAndStart(any()); + } + + @Test + void launchProvidedRuntime_copiesBootstrapBeforeStart() throws Exception { + Path codePath = Files.createDirectory(tempDir.resolve("provided-code")); + Files.writeString(codePath.resolve("bootstrap"), "#!/bin/sh\necho hello"); + + LambdaFunction fn = new LambdaFunction(); + fn.setFunctionName("provided-fn"); + fn.setRuntime("provided.al2023"); + fn.setHandler("bootstrap"); + fn.setCodeLocalPath(codePath.toString()); + + launcher.launch(fn); + + // The critical invariant: create must happen before any Docker copy, + // and start must happen after. This is the exact regression from #466. + InOrder inOrder = inOrder(lifecycleManager, dockerClient); + inOrder.verify(lifecycleManager).create(any()); + inOrder.verify(lifecycleManager).getDockerClient(); + // Two copies: code to /var/task + bootstrap to /var/runtime + inOrder.verify(dockerClient, times(2)).copyArchiveToContainerCmd("container-123"); + inOrder.verify(lifecycleManager).startCreated(eq("container-123"), any()); + + // Verify both /var/task and /var/runtime were targeted + assertTrue(capturedRemotePaths.contains("/var/task"), + "code should be copied to /var/task"); + assertTrue(capturedRemotePaths.contains("/var/runtime"), + "bootstrap should be copied to /var/runtime"); + + verify(lifecycleManager, never()).createAndStart(any()); } }