fix(ecr): release registry port when container start fails (#1010)

ensureStarted() reserved a port from the 5100-5199 pool but never
released it when the registry container failed to start. Because
PortAllocator is @ApplicationScoped, each failed start leaked a port
permanently, eventually exhausting the pool and masking the real
failure with "No free port available in range 5100-5199".

Widen the try to cover spec building and addPersistenceMounts (the
first Docker call in named-volume mode) and release the port on
failure unless the container started, mirroring the existing pattern
in RuntimeApiServerFactory and Ec2ContainerManager.
This commit is contained in:
Juano Morello authored and GitHub committed 2026-05-23 22:40:50 -05:00
1 parent 214d404733
commit c7317c1626
2 files changed
+117 -21

No files matched your search

@@ -142,28 +142,28 @@ public class EcrRegistryManager {
config.services().ecr().registryBasePort(),
config.services().ecr().registryMaxPort());
String image = config.services().ecr().registryImage();
// Build environment variables
List<String> env = new ArrayList<>(List.of(
"REGISTRY_STORAGE_DELETE_ENABLED=true",
"REGISTRY_HTTP_ADDR=0.0.0.0:" + CONTAINER_INTERNAL_PORT
));
// Build container spec
ContainerBuilder.Builder specBuilder = containerBuilder.newContainer(image)
.withName(name)
.withEnv(env)
.withPortBinding(CONTAINER_INTERNAL_PORT, chosenPort)
.withDockerNetwork(config.services().ecr().dockerNetwork())
.withLogRotation();
// Handle persistence mounting based on storage configuration
addPersistenceMounts(specBuilder, env);
ContainerSpec spec = specBuilder.build();
try {
String image = config.services().ecr().registryImage();
// Build environment variables
List<String> env = new ArrayList<>(List.of(
"REGISTRY_STORAGE_DELETE_ENABLED=true",
"REGISTRY_HTTP_ADDR=0.0.0.0:" + CONTAINER_INTERNAL_PORT
));
// Build container spec
ContainerBuilder.Builder specBuilder = containerBuilder.newContainer(image)
.withName(name)
.withEnv(env)
.withPortBinding(CONTAINER_INTERNAL_PORT, chosenPort)
.withDockerNetwork(config.services().ecr().dockerNetwork())
.withLogRotation();
// Handle persistence mounting based on storage configuration
addPersistenceMounts(specBuilder, env);
ContainerSpec spec = specBuilder.build();
ContainerInfo info = lifecycleManager.createAndStart(spec);
this.containerId = info.containerId();
this.hostPort = chosenPort;
@@ -173,6 +173,12 @@ public class EcrRegistryManager {
// Attach log streaming (new feature)
attachLogStream();
} catch (Exception e) {
// Release the reserved port unless the container actually started, so a
// failed start (e.g. Docker unreachable) does not permanently exhaust the
// registry port pool across retries.
if (!started) {
portAllocator.release(chosenPort);
}
throw new RuntimeException("Failed to start ECR backing registry container: " + e.getMessage(), e);
}
runReconcileOnce();