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

publishVersion copies roughly thirty fields off the live LambdaFunction
inside synchronized (lockForConcurrencyOp(...)), but neither updater took
that lock, so nothing was serialised. An update landing mid-copy produced a
snapshot that was part old code and part new configuration, carrying a
sourceRevisionId that identified neither state.

#2939 made extractZipCodeBytes publish the code identity fields under the
lock, which closed the CodeSha256 precondition hole specifically. The rest of
the snapshot copy stayed unprotected, and updateFunctionConfiguration never
took the lock at all.

Both updaters now take the same per-function lock. Their bodies move
unchanged into private *Locked methods behind a thin wrapper, so the change
is a small number of production lines rather than a few hundred lines of
reindentation. The monitor is one object per function ARN and synchronized is
reentrant, so the nested acquisitions inside extractZipCodeBytes, and
publishVersion's own when updateFunctionCode is called with Publish, are
no-ops rather than a second lock. Every existing acquisition is keyed to a
single function, so there is no cross-ARN ordering to invert.

Zip extraction now runs inside the lock. extractZipCodeBytes called extractTo
outside it and took the lock only to publish the code fields afterwards, so a
deploy could rewrite $LATEST's code directory while publishVersion held the
lock copying that directory into a version's own (#3041). Serialising the
whole updater closes that too.

UpdateFunctionConfiguration's structure members stay shape-checked ahead of
the function lookup, where #3051 put them, so a malformed member on a
function that does not exist still reports SerializationException rather than
404. The locked body re-reads them; structureMember is pure, so the second
read cannot fail once the first has passed. That ordering was not pinned by
any test, and now is.

This also corrects a comment from #2939 claiming the hash check ran "inside
the lock UpdateFunctionCode takes". UpdateFunctionCode did not take a lock.

Closes #3007
This commit is contained in:
David authored and GitHub committed 2026-09-08 07:23:24 -04:00
1 parent 1b1252505b
commit ebd849a956
3 files changed
+124 -7

No files matched your search

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