fix(lambda): give each published version its own copy of the code (#3041)

A published version's metadata was immutable but the directory it named was
not. CodeStore.getCodePath has no version component, so every version shared
one directory with $LATEST and publishVersion copied only the codeLocalPath
pointer. UpdateFunctionCode replaces that directory wholesale, so a redeploy
silently rewrote what an already-published version ran: the version kept
advertising the CodeSha256 it was published with while the bytes behind it
were a different build, and ContainerLauncher tar-copies that directory at
launch, so a qualified invoke executed $LATEST's code.

publishVersion now copies the code into a directory belonging to that version.
Image-backed and hot-reload functions keep the reference they had: an image is
already immutable by digest, and a hot-reload function's bind-mounted directory
is meant to track the working tree. A copy failure falls back to the shared
path rather than failing the publish.

Version directories live under a "<function>@versions" container rather than as
"<function>.v<n>" siblings. sanitizeName maps every name into [a-zA-Z0-9_.-],
so no function can produce a directory name containing "@" and the two
namespaces cannot overlap. A prefix scan over flat siblings could not be made
correct: FunctionName has no charset validation in floci today, so a function
genuinely named "foo.v1" owns the directory "foo"'s version 1 would claim, and
deleting either corrupted the other.

DeleteFunction with a qualifier now reclaims that version's directory, inside
the per-function lock it already held, and only when the version actually owned
one. Previously nothing but whole-function delete reclaimed version code, so a
repeated publish/delete cycle grew on disk without bound.

Closes #2958
This commit is contained in:
David authored and GitHub committed 2026-09-06 14:07:43 -05:00
1 parent f8a1c3134a
commit 4ee5a528f3
5 files changed
+295 -13

No files matched your search

@@ -110,14 +110,18 @@ class LambdaAccountScopedCodeTest {
}
@Test
void updateFunctionCodeDoesNotDeleteALegacyDirectoryStillReferencedByAPublishedVersion(
void aPublishedVersionsCodeSurvivesLatestMigratingOffTheLegacyPath(
@TempDir Path baseDir) throws Exception {
// publishVersion snapshots codeLocalPath verbatim (it must, or a version-qualified
// invoke launches a container with no code - see #1987). If $LATEST was still on the
// legacy path at publish time, that version's snapshot is now the ONLY thing keeping
// the legacy directory alive once $LATEST itself migrates. The unused-check only scanned
// $LATEST records, so it missed this and reclaimed the directory out from under the
// published version's own future invokes.
// publishVersion used to snapshot codeLocalPath verbatim (it had to, or a
// version-qualified invoke launched a container with no code - see #1987). If $LATEST was
// still on the legacy path at publish time, that version's snapshot became the ONLY thing
// keeping the legacy directory alive once $LATEST itself migrated, and the unused-check
// reclaimed it out from under the version's own future invokes.
//
// A version now copies the code into a directory of its own (#2958), so it no longer
// depends on the legacy directory surviving. The guarantee this test protects is unchanged
// and now stronger: whatever happens to the directory $LATEST was using, the version keeps
// the code it was published from.
CodeStore codeStore = new CodeStore(baseDir);
LambdaService svc = serviceFor(ACCOUNT_A, codeStore);
svc.createFunction(REGION, zipRequest("legacy-version-fn", "v1"));
@@ -129,13 +133,17 @@ class LambdaAccountScopedCodeTest {
latest.setCodeLocalPath(legacyPath.toAbsolutePath().normalize().toString());
LambdaFunction version = svc.publishVersion(REGION, "legacy-version-fn", null);
assertEquals(legacyPath.toAbsolutePath().normalize().toString(), version.getCodeLocalPath(),
"the published version must have snapshotted the legacy path");
Path versionPath = Path.of(version.getCodeLocalPath());
assertNotEquals(legacyPath.toAbsolutePath().normalize().toString(), version.getCodeLocalPath(),
"the published version must own its code rather than reference the legacy directory");
assertEquals("v1", Files.readString(versionPath.resolve("index.js")).trim());
svc.updateFunctionCode(REGION, "legacy-version-fn", Map.of("ZipFile", zipBase64("index.js", "v2")));
assertTrue(Files.exists(legacyPath),
"a legacy directory a published version still references must survive this update");
assertTrue(Files.isDirectory(versionPath),
"the published version's own code must survive $LATEST migrating off the legacy path");
assertEquals("v1", Files.readString(versionPath.resolve("index.js")).trim(),
"the version must still hold the code it was published from");
}
@Test
@@ -15,6 +15,8 @@ import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.io.ByteArrayOutputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Base64;
import java.util.HashSet;
@@ -1467,4 +1469,94 @@ class LambdaServiceTest {
"FunctionName", "arn:aws:lambda:us-west-2:000000000000:function:other-region-fn"
)));
}
/**
* Issue #2958: a published version stored a reference to {@code $LATEST}'s code directory
* rather than a copy of the code, and extraction replaces that directory wholesale on every
* deploy. A later UpdateFunctionCode therefore rewrote what an already-published version would
* run, leaving the version advertising one CodeSha256 over a different build.
*/
@Test
void aPublishedVersionKeepsItsOwnCodeWhenLatestIsRedeployed() throws Exception {
Map<String, Object> request = baseRequest("version-code-isolation-fn");
request.put("Code", Map.of("ZipFile", zipWithBody("v1")));
service.createFunction(REGION, request);
LambdaFunction v1 = service.publishVersion(REGION, "version-code-isolation-fn", null);
Path v1Path = Path.of(v1.getCodeLocalPath());
String v1Sha = v1.getCodeSha256();
assertTrue(Files.isDirectory(v1Path), "a published version must have its own code directory");
assertEquals("v1", Files.readString(v1Path.resolve("index.js")).trim());
// Redeploy $LATEST. Extraction replaces its directory wholesale, which is what used to take
// the published version's code with it.
service.updateFunctionCode(REGION, "version-code-isolation-fn",
Map.of("ZipFile", zipWithBody("v2")));
LambdaFunction latest = service.getFunction(REGION, "version-code-isolation-fn");
assertNotEquals(v1Path.toString(), latest.getCodeLocalPath(),
"a version must not share $LATEST's directory");
assertEquals("v2",
Files.readString(Path.of(latest.getCodeLocalPath()).resolve("index.js")).trim());
// The version still holds the bytes it was published from, and they still match the hash it
// advertises, which is the guarantee that was broken.
assertTrue(Files.isDirectory(v1Path), "the version's code must survive a redeploy of $LATEST");
assertEquals("v1", Files.readString(v1Path.resolve("index.js")).trim());
assertEquals(v1Sha, v1.getCodeSha256());
}
@Test
void deletingAFunctionRemovesItsPublishedVersionsCode() throws Exception {
Map<String, Object> request = baseRequest("version-code-delete-fn");
request.put("Code", Map.of("ZipFile", zipWithBody("v1")));
service.createFunction(REGION, request);
LambdaFunction v1 = service.publishVersion(REGION, "version-code-delete-fn", null);
Path v1Path = Path.of(v1.getCodeLocalPath());
assertTrue(Files.isDirectory(v1Path));
service.deleteFunction(REGION, "version-code-delete-fn");
assertFalse(Files.exists(v1Path),
"a version's code must not outlive the function it belongs to");
}
@Test
void deletingOnePublishedVersionReclaimsOnlyThatVersionsCode() throws Exception {
Map<String, Object> request = baseRequest("version-code-reclaim-fn");
request.put("Code", Map.of("ZipFile", zipWithBody("v1")));
service.createFunction(REGION, request);
LambdaFunction v1 = service.publishVersion(REGION, "version-code-reclaim-fn", null);
service.updateFunctionCode(REGION, "version-code-reclaim-fn",
Map.of("ZipFile", zipWithBody("v2")));
LambdaFunction v2 = service.publishVersion(REGION, "version-code-reclaim-fn", null);
Path v1Path = Path.of(v1.getCodeLocalPath());
Path v2Path = Path.of(v2.getCodeLocalPath());
Path latestPath = Path.of(
service.getFunction(REGION, "version-code-reclaim-fn").getCodeLocalPath());
assertNotEquals(v1Path, v2Path, "two versions must not share one code directory");
service.deleteFunction(REGION, "version-code-reclaim-fn", v1.getVersion());
// Only whole-function delete reclaimed any of this before, so every version ever published
// stayed on disk for as long as the data directory lived.
assertFalse(Files.exists(v1Path), "the deleted version's code must be reclaimed");
assertTrue(Files.isDirectory(v2Path), "a surviving version's code must be left alone");
assertEquals("v2", Files.readString(v2Path.resolve("index.js")).trim());
assertTrue(Files.isDirectory(latestPath), "$LATEST's code must be left alone");
}
private static String zipWithBody(String body) throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try (ZipOutputStream zos = new ZipOutputStream(baos)) {
zos.putNextEntry(new ZipEntry("index.js"));
zos.write((body + "\n").getBytes(StandardCharsets.UTF_8));
zos.closeEntry();
}
return Base64.getEncoder().encodeToString(baos.toByteArray());
}
}
@@ -103,6 +103,60 @@ class CodeStoreTest {
assertFalse(Files.exists(legacyPath));
}
@Test
void aVersionDirectoryCannotCollideWithAnotherFunctionsOwnDirectory(@TempDir Path baseDir)
throws IOException {
// Floci does not restrict the character set of FunctionName today, only that it is
// non-blank, so "foo.v1" is a function a user can genuinely create. A "<name>.v<n>" sibling
// naming scheme handed it the exact directory version 1 of "foo" would claim, so deleting
// either function silently corrupted the other. The suffix used instead is outside the
// character set sanitizeName can emit, which makes the two namespaces disjoint by
// construction rather than by a prefix match that has to guess where the name ends.
CodeStore store = new CodeStore(baseDir);
writeHandler(store.getVersionCodePath(ACCOUNT_A, "foo", "1"), "foo-v1");
writeHandler(store.getCodePath(ACCOUNT_A, "foo.v1"), "other-function");
writeHandler(store.getVersionCodePath(ACCOUNT_A, "foo.v1", "1"), "other-function-v1");
store.delete(ACCOUNT_A, "foo");
assertTrue(store.exists(ACCOUNT_A, "foo.v1"),
"deleting foo must not remove a function literally named foo.v1");
assertEquals("other-function",
Files.readString(store.getCodePath(ACCOUNT_A, "foo.v1").resolve("index.js")));
assertEquals("other-function-v1",
Files.readString(store.getVersionCodePath(ACCOUNT_A, "foo.v1", "1").resolve("index.js")),
"another function's published version code must survive too");
assertFalse(Files.exists(store.getVersionCodePath(ACCOUNT_A, "foo", "1")),
"foo's own version code must still be reclaimed");
}
@Test
void deleteVersionRemovesOneVersionAndLeavesTheRestInPlace(@TempDir Path baseDir) throws IOException {
CodeStore store = new CodeStore(baseDir);
writeHandler(store.getCodePath(ACCOUNT_A, "fn"), "latest");
writeHandler(store.getVersionCodePath(ACCOUNT_A, "fn", "1"), "one");
writeHandler(store.getVersionCodePath(ACCOUNT_A, "fn", "2"), "two");
store.deleteVersion(ACCOUNT_A, "fn", "1");
assertFalse(Files.exists(store.getVersionCodePath(ACCOUNT_A, "fn", "1")));
assertEquals("two",
Files.readString(store.getVersionCodePath(ACCOUNT_A, "fn", "2").resolve("index.js")));
assertTrue(store.exists(ACCOUNT_A, "fn"), "$LATEST's code must be untouched");
}
@Test
void versionCodeLivesOutsideTheDirectoryExtractionReplaces(@TempDir Path baseDir) {
CodeStore store = new CodeStore(baseDir);
Path latest = store.getCodePath(ACCOUNT_A, "fn");
Path version = store.getVersionCodePath(ACCOUNT_A, "fn", "1");
assertFalse(version.normalize().startsWith(latest.normalize()),
"extraction replaces $LATEST's directory wholesale, taking any nested version with it");
assertTrue(version.normalize().startsWith(baseDir.normalize()));
}
private void writeHandler(Path codePath, String content) throws IOException {
Files.createDirectories(codePath);
Files.writeString(codePath.resolve("index.js"), content);