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

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