fix(codeartifact): share PyPI retention locks across package spellings (#8201)

* fix(codeartifact): share PyPI retention locks across package spellings

Normalize the per-version retention lock with the existing PEP 503 package-name helper so a background sibling import and a direct wheel request cannot race through different aliases. Add a regression for hyphenated sdist and underscored wheel requests.

Signed-off-by: Andre Kurait <AndreKurait@gmail.com>

* test(codeartifact): observe the shared retention monitor

Await the request thread reaching its shared retention monitor instead of spending a fixed timeout window. Preserve completion and single-write assertions.

Signed-off-by: Andre Kurait <akurait@amazon.com>

---------

Signed-off-by: Andre Kurait <AndreKurait@gmail.com>
Signed-off-by: Andre Kurait <akurait@amazon.com>
This commit is contained in:
Andre Kurait authored and GitHub committed 2026-11-13 07:56:11 -04:00
1 parent 4963472362
commit 5816d8a908
2 files changed
+58 -1

No files matched your search

@@ -294,7 +294,8 @@ public class CodeArtifactPypiExternalConnectionImporter {
importedRequested = importedAny;
return importedRequested;
}
String importLockKey = repoId + "|" + packageName.get() + "|" + version.get();
String importLockKey = repoId + "|" + PypiserverSidecarClient.normalizePackageName(packageName.get())
+ "|" + version.get();
// Splits the version's own files into the one filename actually requested (imported
// synchronously below, so downloadFile's own proxy retry has real bytes to serve the
@@ -6,6 +6,7 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
import io.floci.aws.core.common.AwsException;
import io.floci.aws.services.codeartifact.model.CodeArtifactRepository;
import io.floci.aws.services.codeartifact.model.ExternalConnection;
import io.floci.aws.testing.ThreadStates;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InOrder;
@@ -25,6 +26,7 @@ import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
@@ -532,6 +534,60 @@ class CodeArtifactPypiExternalConnectionImporterTest {
any(), anyLong(), anyLong(), anyLong(), any(), eq(SDIST_FILENAME), anyLong(), any());
}
@Test
void hyphenatedSdistAndUnderscoredWheelShareTheRetentionLock() throws Exception {
String sdist = "widget-name-1.0.0.tar.gz";
String wheel = "widget_name-1.0.0-py3-none-any.whl";
CountDownLatch firstWheelWrite = new CountDownLatch(1);
CountDownLatch releaseWheelWrite = new CountDownLatch(1);
AtomicBoolean sdistWritten = new AtomicBoolean(false);
AtomicBoolean wheelWritten = new AtomicBoolean(false);
AtomicInteger wheelWrites = new AtomicInteger();
when(externalConnectionClient.fetchSimpleIndex(eq("public:pypi"), anyString()))
.thenReturn(Optional.of(new PypiExternalConnectionClient.FetchedAsset(
simpleIndexJson(List.of(VERSION), sdist, wheel))));
stubFetchFile(urlFor(sdist), SDIST_CONTENT);
stubFetchFile(urlFor(wheel), WHEEL_CONTENT);
when(pypiserver.listPackageVersionAssetNames(anyString(), anyString(), anyString(), any(), anyString(),
anyString())).thenAnswer(invocation -> wheelWritten.get() ? List.of(sdist, wheel)
: sdistWritten.get() ? List.of(sdist) : List.of());
when(service.retainedVersionOriginConnection(anyString(), anyString(), anyString(), anyString(),
anyString(), any(), anyString(), anyString())).thenReturn(Optional.of("public:pypi"));
when(service.retainAssetUnderVersionFence(any(), any(), any(), any(), any(), any(), any(), any(),
anyLong(), anyLong(), anyLong(), any(), eq(sdist), anyLong(), any())).thenAnswer(invocation -> {
sdistWritten.set(true);
return new CodeArtifactService.AssetRetentionAttemptResult(true, true);
});
when(service.retainAssetUnderVersionFence(any(), any(), any(), any(), any(), any(), any(), any(),
anyLong(), anyLong(), anyLong(), any(), eq(wheel), anyLong(), any())).thenAnswer(invocation -> {
if (wheelWrites.incrementAndGet() == 1) {
firstWheelWrite.countDown();
assertTrue(releaseWheelWrite.await(5, TimeUnit.SECONDS), "wheel write was never released");
}
wheelWritten.set(true);
return new CodeArtifactService.AssetRetentionAttemptResult(true, true);
});
ExecutorService pool = Executors.newSingleThreadExecutor();
try {
assertTrue(importer.tryImportAsset(REGION, DOMAIN, OWNER, REPOSITORY, REPO_ID, sdist));
assertTrue(firstWheelWrite.await(5, TimeUnit.SECONDS), "background wheel write never started");
AtomicReference<Thread> requestThread = new AtomicReference<>();
Future<Boolean> requestedWheel = pool.submit(() -> {
requestThread.set(Thread.currentThread());
return importer.tryImportAsset(REGION, DOMAIN, OWNER, REPOSITORY, REPO_ID, wheel);
});
ThreadStates.awaitBlockedOrDone(requestThread, requestedWheel);
assertFalse(requestedWheel.isDone(), "the wheel request must wait for the background sibling");
assertEquals(Thread.State.BLOCKED, requestThread.get().getState());
releaseWheelWrite.countDown();
assertTrue(requestedWheel.get(5, TimeUnit.SECONDS));
assertEquals(1, wheelWrites.get());
} finally {
releaseWheelWrite.countDown();
pool.shutdownNow();
}
}
private static String urlFor(String filename) {
return "https://files.pythonhosted.org/packages/aa/bb/" + filename;
}