fix(docker): bound concurrent container teardown (#5860)

Signed-off-by: Andre Kurait <akurait@amazon.com>
This commit is contained in:
Andre Kurait authored and GitHub committed 2026-10-17 22:31:47 -05:00
1 parent 377c09733a
commit ae285fc3a8
2 files changed
+165

No files matched your search

@@ -48,6 +48,7 @@ import java.util.OptionalLong;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import java.util.regex.Pattern;
@@ -84,6 +85,8 @@ public class ContainerLifecycleManager {
/** Host interface a port marked loopback-only publishes on. */
private static final String LOOPBACK_HOST_IP = "127.0.0.1";
private final Semaphore teardownSlots = new Semaphore(8, true);
private final DockerClient dockerClient;
private final ImageCacheService imageCacheService;
private final ContainerDetector containerDetector;
@@ -484,6 +487,10 @@ public class ContainerLifecycleManager {
* SIGTERM before Docker sends SIGKILL. Must be a non-negative integer.
*/
public void stopAndRemove(String containerId, Closeable logStream, int stopTimeoutSeconds) {
withTeardownSlot(() -> stopAndRemoveAdmitted(containerId, logStream, stopTimeoutSeconds));
}
private void stopAndRemoveAdmitted(String containerId, Closeable logStream, int stopTimeoutSeconds) {
LOG.infov("Stopping container {0}", containerId);
boolean stoppedOrMissing = false;
@@ -537,6 +544,10 @@ public class ContainerLifecycleManager {
* only once the container is gone; when removal fails it is left open for the retry.
*/
public void stopAndRemoveStrict(String containerId, Closeable logStream) {
withTeardownSlot(() -> stopAndRemoveStrictAdmitted(containerId, logStream));
}
private void stopAndRemoveStrictAdmitted(String containerId, Closeable logStream) {
LOG.infov("Stopping container {0}", containerId);
Exception stopFailure = null;
@@ -571,6 +582,15 @@ public class ContainerLifecycleManager {
closeLogStreamIfPresent(logStream);
}
private void withTeardownSlot(Runnable stopAndRemove) {
teardownSlots.acquireUninterruptibly();
try {
stopAndRemove.run();
} finally {
teardownSlots.release();
}
}
private void closeLogStreamIfPresent(Closeable logStream) {
if (logStream != null) {
closeLogStreamAfterContainerStop(logStream);