fix(ecs): reclaim anonymous task volumes (#6793)

* fix(ecs): reclaim anonymous task volumes

Opt ECS lifecycle cleanup into anonymous-volume removal while preserving named volumes, shared defaults, admission and retries. Retain actual Docker regression coverage.

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

* fix(ecs): reclaim volumes after rejected task starts

Use the existing ephemeral lifecycle path for ordinary task containers so Docker start failures reclaim anonymous volumes before ECS records the container. Preserve named volumes and shared defaults, close fixture callbacks, and retain a real Docker rejected-entrypoint regression.

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

---------

Signed-off-by: Andre Kurait <akurait@amazon.com>
Signed-off-by: Andre Kurait <AndreKurait@gmail.com>
This commit is contained in:
Andre Kurait authored and GitHub committed 2026-10-28 07:02:11 -04:00
1 parent 967e87d7f7
commit 9fcf1683ee
17 files changed
+381 -82

No files matched your search

@@ -591,10 +591,18 @@ public class ContainerLifecycleManager {
}
public void stopAndRemoveStrict(String containerId, Closeable logStream, int stopTimeoutSeconds) {
withTeardownSlot(() -> stopAndRemoveStrictAdmitted(containerId, logStream, stopTimeoutSeconds));
stopAndRemoveStrict(containerId, logStream, stopTimeoutSeconds, false);
}
private void stopAndRemoveStrictAdmitted(String containerId, Closeable logStream, int stopTimeoutSeconds) {
/** Strict teardown that can also reclaim the container's anonymous volumes. */
public void stopAndRemoveStrict(String containerId, Closeable logStream, int stopTimeoutSeconds,
boolean removeAnonymousVolumes) {
withTeardownSlot(() -> stopAndRemoveStrictAdmitted(
containerId, logStream, stopTimeoutSeconds, removeAnonymousVolumes));
}
private void stopAndRemoveStrictAdmitted(String containerId, Closeable logStream, int stopTimeoutSeconds,
boolean removeAnonymousVolumes) {
LOG.infov("Stopping container {0}", containerId);
Exception stopFailure = null;
@@ -613,7 +621,11 @@ public class ContainerLifecycleManager {
}
try {
dockerClient.removeContainerCmd(containerId).withForce(true).exec();
RemoveContainerCmd remove = dockerClient.removeContainerCmd(containerId).withForce(true);
if (removeAnonymousVolumes) {
remove.withRemoveVolumes(true);
}
remove.exec();
LOG.debugv("Removed container {0}", containerId);
} catch (NotFoundException e) {
// Already gone, so cleanup succeeded.
@@ -5,6 +5,7 @@ import com.github.dockerjava.api.async.ResultCallback;
import com.github.dockerjava.api.command.InspectContainerResponse;
import com.github.dockerjava.api.command.InspectVolumeResponse;
import com.github.dockerjava.api.command.ListVolumesResponse;
import com.github.dockerjava.api.command.RemoveContainerCmd;
import com.github.dockerjava.api.command.StatsCmd;
import com.github.dockerjava.api.exception.NotFoundException;
import com.github.dockerjava.api.model.ContainerNetwork;
@@ -567,7 +568,7 @@ public class EcsContainerManager {
}
fluentHost = inspectContainerIp(dockerId);
} else {
ContainerInfo info = lifecycleManager.createAndStart(spec);
ContainerInfo info = lifecycleManager.createAndStartEphemeral(spec);
dockerId = info.containerId();
}
@@ -1241,7 +1242,7 @@ public class EcsContainerManager {
continue;
}
try {
lifecycleManager.removeIfExistsStrict(container.getId());
lifecycleManager.removeIfExistsStrict(container.getId(), true);
leftoverRemovalFailuresReported.remove(container.getId());
LOG.infov("Removed ECS container {0} ({1}) left by a previous run", container.getId(),
container.getNames() == null ? "" : String.join(",", container.getNames()));
@@ -1466,7 +1467,7 @@ public class EcsContainerManager {
*/
private boolean stopAndConfirmRemoved(String dockerId) {
try {
lifecycleManager.stopAndRemoveStrict(dockerId, null);
lifecycleManager.stopAndRemoveStrict(dockerId, null, 5, true);
return true;
} catch (RuntimeException e) {
LOG.warnv("Error removing ECS container {0}: {1}", dockerId, e.getMessage());
@@ -1522,7 +1523,10 @@ public class EcsContainerManager {
// Read before the removal below, which is the last moment the daemon still knows it.
handle.recordFinishedAt(name, getFinishedAtIfStopped(dockerId));
try {
lifecycleManager.getDockerClient().removeContainerCmd(dockerId).withForce(true).exec();
RemoveContainerCmd remove = lifecycleManager.getDockerClient()
.removeContainerCmd(dockerId).withForce(true);
remove.withRemoveVolumes(true);
remove.exec();
terminatedContainerIds.add(dockerId);
handle.recordContainerRemoved(name);
} catch (NotFoundException ignored) {