fix(lambda): check published versions, not just $LATEST, before reclaiming a legacy code directory

publishVersion snapshots codeLocalPath verbatim (required so a version-qualified
invoke doesn't launch a container with no code, #1987), so a version published
while $LATEST was still on the pre-account-scoped legacy path keeps that
directory alive on its own after $LATEST migrates. The unused-check only
scanned $LATEST records via listAllAccounts(region), so a later delete or code
update reclaimed the directory out from under that published version's own
future invokes. Switched to functionStore.listAll(), which has no $LATEST or
region filter, matching the fact that the legacy path itself carries neither.
This commit is contained in:
Sam Fakhreddine committed 2026-08-28 02:57:24 -06:00
1 parent bede98fc18
commit f3837be323
2 files changed
+41 -7

No files matched your search

@@ -712,7 +712,7 @@ public class LambdaService implements ResourceProvider {
}
codeStore.delete(ownerAccount(fn), functionName);
functionStore.delete(region, functionName);
reclaimLegacyCodeDirectoryIfUnused(region, functionName);
reclaimLegacyCodeDirectoryIfUnused(functionName);
versionCounters.remove(versionCounterKey(region, fn));
versionCounters.remove(legacyVersionCounterKey(region, functionName));
if (aliasStore != null) {
@@ -735,13 +735,18 @@ public class LambdaService implements ResourceProvider {
/**
* The pre-account-scoped code layout gave every account's same-named function the exact
* same on-disk directory, so it is only safe to reclaim once no account's {@code $LATEST}
* still has {@code codeLocalPath} pointing at it — otherwise a delete or code update in one
* account destroys a directory another account's function still serves invocations from.
* same on-disk directory (CodeStore never scoped it by region either), so it is only safe
* to reclaim once nothing at all still has {@code codeLocalPath} pointing at it. That
* includes published versions, not just {@code $LATEST}: publishVersion snapshots
* codeLocalPath verbatim, so a version published while $LATEST was still on the legacy path
* keeps that directory alive on its own even after $LATEST itself migrates. Checking only
* $LATEST (via listAllAccounts) missed this and let a later delete or code update reclaim
* the directory out from under that version's own future invokes. listAll() has no region
* or $LATEST filter, matching the fact that the legacy path itself carries neither.
*/
private void reclaimLegacyCodeDirectoryIfUnused(String region, String functionName) {
private void reclaimLegacyCodeDirectoryIfUnused(String functionName) {
String legacyPath = codeStore.getLegacyCodePath(functionName).toAbsolutePath().normalize().toString();
boolean stillLive = functionStore.listAllAccounts(region).stream()
boolean stillLive = functionStore.listAll().stream()
.anyMatch(other -> functionName.equals(other.getFunctionName())
&& legacyPath.equals(other.getCodeLocalPath()));
if (!stillLive) {
@@ -1885,7 +1890,7 @@ public class LambdaService implements ResourceProvider {
storeDeploymentPackage(fn, zipBytes, region);
// Reclaim a directory left behind by a function created before account-scoping;
// codePath above is already the new location, so this is a no-op once migrated.
reclaimLegacyCodeDirectoryIfUnused(region, fn.getFunctionName());
reclaimLegacyCodeDirectoryIfUnused(fn.getFunctionName());
} catch (AwsException e) {
throw e;
} catch (IOException e) {
@@ -109,6 +109,35 @@ class LambdaAccountScopedCodeTest {
assertFalse(Files.exists(legacyPath), "the pre-account-scoped directory must be reclaimed on update");
}
@Test
void updateFunctionCodeDoesNotDeleteALegacyDirectoryStillReferencedByAPublishedVersion(
@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.
CodeStore codeStore = new CodeStore(baseDir);
LambdaService svc = serviceFor(ACCOUNT_A, codeStore);
svc.createFunction(REGION, zipRequest("legacy-version-fn", "v1"));
Path legacyPath = codeStore.getLegacyCodePath("legacy-version-fn");
Files.createDirectories(legacyPath);
Files.writeString(legacyPath.resolve("index.js"), "v1");
LambdaFunction latest = svc.getFunction(REGION, "legacy-version-fn");
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");
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");
}
@Test
void updateFunctionCodeDoesNotDeleteALegacyDirectoryStillLiveForAnotherAccount(@TempDir Path baseDir) throws Exception {
// Before account-scoping, two accounts' same-named functions shared the exact same