Skip to content

armgit / floci

Public Imported from GitHub
0

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

fix(lambda): serialise the version snapshot against function updates (#3010)

Davidebd849a95614
diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaService.java b/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaService.java
index 0512207290..38a24a1b16 100644
--- a/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaService.java
@@ -81,6 +81,15 @@ public class LambdaService implements ResourceProvider {
private static final int MAX_HANDLER_LENGTH = 128;
private static final List<String> FUNCTION_ARCHITECTURES = List.of("x86_64", "arm64");
+ /**
+ * Structure members {@code UpdateFunctionConfiguration} accepts. Shape-checked before the
+ * function lookup so a malformed member reports SerializationException rather than 404 (#3051),
+ * which is why the list is named here rather than left implicit in the extraction below.
+ */
+ private static final List<String> CONFIG_STRUCTURE_MEMBERS = List.of(
+ "Environment", "EphemeralStorage", "TracingConfig", "DeadLetterConfig",
+ "VpcConfig", "SnapStart", "LoggingConfig", "ImageConfig");
+
private final LambdaFunctionStore functionStore;
private final LambdaExecutorService executorService;
private final LambdaConcurrencyLimiter concurrencyLimiter;
@@ -599,7 +608,20 @@ public class LambdaService implements ResourceProvider {
public LambdaFunction updateFunctionCode(String region, String functionName, Map<String, Object> request) {
LambdaFunction fn = getFunction(region, functionName);
- functionName = fn.getFunctionName();
+ // publishVersion copies roughly thirty fields off this same live object. Without the
+ // updaters holding the lock it copies under, an update landing mid-copy yields a snapshot
+ // that is part old code and part new configuration, carrying a sourceRevisionId that
+ // identifies neither state (issue #3007). The monitor is reentrant and per function ARN, so
+ // the nested acquisitions further down, and publishVersion's own when Publish is set, are
+ // no-ops rather than a second lock.
+ synchronized (lockForConcurrencyOp(fn.getFunctionArn())) {
+ return updateFunctionCodeLocked(region, fn, request);
+ }
+ }
+
+ private LambdaFunction updateFunctionCodeLocked(String region, LambdaFunction fn,
+ Map<String, Object> request) {
+ String functionName = fn.getFunctionName();
List<String> architectures = validateArchitectures(request.get("Architectures"));
String zipFileBase64 = (String) request.get("ZipFile");
@@ -642,6 +664,27 @@ public class LambdaService implements ResourceProvider {
}
public LambdaFunction updateFunctionConfiguration(String region, String functionName, Map<String, Object> request) {
+ // Shape-checked ahead of the lookup, which is where #3051 put these: a malformed member on
+ // a function that does not exist reports SerializationException, not 404. Splitting the
+ // mutation into a locked body below must not move them behind the lookup, so they stay
+ // here and the locked body re-reads them. structureMember is pure, so the second read
+ // cannot fail once these have passed.
+ for (String member : CONFIG_STRUCTURE_MEMBERS) {
+ structureMember(request, member);
+ }
+
+ LambdaFunction fn = getFunction(region, functionName);
+ // Same reason as updateFunctionCode: publishVersion's snapshot copy must not observe a
+ // half-applied configuration change (issue #3007).
+ synchronized (lockForConcurrencyOp(fn.getFunctionArn())) {
+ return updateFunctionConfigurationLocked(region, fn, request);
+ }
+ }
+
+ private LambdaFunction updateFunctionConfigurationLocked(String region, LambdaFunction fn,
+ Map<String, Object> request) {
+ String functionName = fn.getFunctionName();
+ List<String> architectures = validateArchitectures(request.get("Architectures"));
Map<String, Object> environment = structureMember(request, "Environment");
Map<String, String> environmentVariables = environmentVariables(environment);
Map<String, Object> ephemeralStorage = structureMember(request, "EphemeralStorage");
@@ -652,9 +695,6 @@ public class LambdaService implements ResourceProvider {
Map<String, Object> loggingConfig = structureMember(request, "LoggingConfig");
Map<String, Object> imageConfig = structureMember(request, "ImageConfig");
- LambdaFunction fn = getFunction(region, functionName);
- List<String> architectures = validateArchitectures(request.get("Architectures"));
-
// Validated before any field mutation below, not inline where Layers is applied further
// down - fn is the live object backing this store entry (InMemoryStorage#get returns the
// same reference, not a copy), so validating this late would leave every
@@ -1948,9 +1988,10 @@ public class LambdaService implements ResourceProvider {
// delete, persisting a snapshot.codeLocalPath (below) that names a directory about to
// be removed as unreferenced.
synchronized (lockForConcurrencyOp(fn.getFunctionArn())) {
- // Inside the lock UpdateFunctionCode takes, so the hash cannot be checked against one
- // version of $LATEST and the snapshot then taken from another. Checking it outside
- // would let an overlapping deploy publish code the caller never authorised.
+ // Inside the lock UpdateFunctionCode and UpdateFunctionConfiguration now take, so the
+ // hash cannot be checked against one version of $LATEST and the snapshot then taken
+ // from another. Checking it outside would let an overlapping deploy publish code the
+ // caller never authorised.
// No isBlank() exclusion here. A present but empty value was previously treated as
// absent, so it skipped the comparison entirely and published without checking
// anything, which is the failure this precondition exists to prevent. It is simply
diff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaServiceTest.java
index b756fec58d..6daf321c07 100644
--- a/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaServiceTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaServiceTest.java
@@ -20,6 +20,7 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Base64;
import java.util.HashSet;
+import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@@ -1580,4 +1581,61 @@ class LambdaServiceTest {
}
return Base64.getEncoder().encodeToString(baos.toByteArray());
}
+
+ @Test
+ void updateFunctionCodeWaitsForAConcurrentHolderOfTheFunctionsConcurrencyLock() throws Exception {
+ assertUpdaterWaitsForTheLock("update-code-lock-fn",
+ (service, name) -> service.updateFunctionCode(REGION, name,
+ new HashMap<>(Map.of("ImageUri", "public.ecr.aws/x/y:1"))));
+ }
+
+ @Test
+ void updateFunctionConfigurationWaitsForAConcurrentHolderOfTheFunctionsConcurrencyLock() throws Exception {
+ assertUpdaterWaitsForTheLock("update-config-lock-fn",
+ (service, name) -> service.updateFunctionConfiguration(REGION, name,
+ new HashMap<>(Map.of("Timeout", 42))));
+ }
+
+ /**
+ * publishVersion copies roughly thirty fields off the live function inside this lock. Neither
+ * updater used to take it, so nothing was serialised and a snapshot could be part old code and
+ * part new configuration (issue #3007). Proving each updater blocks on the lock closes that
+ * window regardless of the exact interleaving, rather than relying on timing to catch it.
+ */
+ private void assertUpdaterWaitsForTheLock(
+ String functionName,
+ java.util.function.BiFunction<LambdaService, String, LambdaFunction> updater) throws Exception {
+ LambdaFunction fn = service.createFunction(REGION, baseRequest(functionName));
+ Object lock = service.lockForConcurrencyOp(fn.getFunctionArn());
+
+ ExecutorService pool = Executors.newFixedThreadPool(1);
+ CountDownLatch acquired = new CountDownLatch(1);
+ CountDownLatch release = new CountDownLatch(1);
+ Thread holder = new Thread(() -> {
+ synchronized (lock) {
+ acquired.countDown();
+ try {
+ release.await();
+ } catch (InterruptedException ignored) {
+ Thread.currentThread().interrupt();
+ }
+ }
+ });
+ try {
+ holder.start();
+ assertTrue(acquired.await(2, java.util.concurrent.TimeUnit.SECONDS));
+
+ Future<LambdaFunction> updateFuture = pool.submit(() -> updater.apply(service, functionName));
+ assertThrows(java.util.concurrent.TimeoutException.class,
+ () -> updateFuture.get(200, java.util.concurrent.TimeUnit.MILLISECONDS),
+ "the updater must block while another operation holds this function's lock");
+
+ release.countDown();
+ holder.join();
+ assertNotNull(updateFuture.get(5, java.util.concurrent.TimeUnit.SECONDS));
+ } finally {
+ release.countDown();
+ pool.shutdownNow();
+ }
+ }
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaStructureSerializationIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaStructureSerializationIntegrationTest.java
index c15966bf3d..15a6a37268 100644
--- a/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaStructureSerializationIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaStructureSerializationIntegrationTest.java
@@ -56,6 +56,24 @@ class LambdaStructureSerializationIntegrationTest {
}
}
+ @Test
+ void updateFunctionConfigurationRejectsScalarStructureMembersOnAnUnknownFunction() {
+ // The shape check runs ahead of the function lookup, so a malformed member reports
+ // SerializationException rather than the 404 the lookup would raise. Pinned because this is
+ // ordering rather than logic: it holds only while the checks stay in front of getFunction,
+ // and nothing else fails if a refactor moves them behind it.
+ for (String member : FUNCTION_STRUCTURE_MEMBERS) {
+ given()
+ .contentType("application/json")
+ .body(Map.of(member, 5))
+ .when()
+ .put(BASE_PATH + "/functions/structure-check-on-missing-function/configuration")
+ .then()
+ .statusCode(400)
+ .body("__type", equalTo("SerializationException"));
+ }
+ }
+
@Test
void environmentVariablesRejectsScalarBeforeUpdateMutation() {
Map<String, Object> createRequest = functionRequest("nested-environment-create");