fix(codeartifact): stop sidecar containers on state reset and nuke (#4411)

A state reset wiped every CodeArtifact repository record without
stopping the Docker containers backing them. Reposilite's one shared
instance kept running with nothing left to reconcile it against;
Verdaccio, one container per repository, leaked one per repository
that existed at reset time, every reset.

Neither manager implemented ContainerTeardown, the interface
ContainerTeardowns.stopAll already runs on both /state/reset and
/state/nuke, and at process shutdown in place of an ad hoc @Observes
ShutdownEvent handler. Implementing it on both replaces their existing
onStop methods with stopManagedContainers(), the same shutdown
behavior now also reached from a reset.

A repository-record walk in CodeArtifactService.beforeReset() was
added as an earlier attempt at this, then removed once ContainerTeardown
turned out to be the actual, already-established mechanism: it is
driven by what each pool is really tracking rather than by
still-existing repository metadata, so it does not miss a container a
delete raced against, and it is what the rest of the codebase already
relies on for the exact same problem (Batch, CodeBuild, SageMaker,
Timestream for InfluxDB).

Verified live: starting a repository's Verdaccio container, calling
/state/reset, and confirming via a real Docker query that the
container is gone, not just that some Java method ran.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-09-25 17:39:03 -05:00
1 parent 8f5ba2b2a8
commit 61e7f07fb6
9 files changed
+231 -56

No files matched your search

@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.core.common.docker;
import org.jboss.logging.Logger;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;
/**
* Lazily starts, health-checks, and reuses one container per arbitrary key. Pulled out of
@@ -33,6 +34,15 @@ public class PerKeyContainerPool {
private final String healthPath;
private final ConcurrentHashMap<String, StartedContainer> containers = new ConcurrentHashMap<>();
private final ConcurrentHashMap<String, Object> startLocks = new ConcurrentHashMap<>();
/**
* Bumped by every {@link #stopAll()}. {@link #ensureReady} reads it before and after starting
* a container: {@code stopAll()} takes no per-key lock (it has no single key to take), so it
* cannot see a container that only starts registering itself after {@code stopAll()} already
* iterated the map. A changed generation is how a start still notices that a teardown happened
* while it was building, so it can stop what it just started instead of leaving it running with
* nothing left able to ever stop it again.
*/
private final AtomicLong generation = new AtomicLong();
/**
* @param healthPath path (e.g. {@code /-/ping}) appended to a container's base URL to probe
@@ -64,7 +74,16 @@ public class PerKeyContainerPool {
LOG.warnv("Sidecar container for key {0} is no longer healthy; restarting it", key);
lifecycleManager.stopAndRemove(existing.containerId(), null);
}
long startedGeneration = generation.get();
StartedContainer started = starter.start();
if (generation.get() != startedGeneration) {
// A stopAll() ran while this container was being built and could not have seen
// it: it was not in the map yet. Stop it now rather than register it, so a reset
// racing a first-use does not leave this one running behind.
LOG.warnv("Sidecar pool was reset while starting a container for key {0}; stopping it", key);
lifecycleManager.stopAndRemove(started.containerId(), null);
throw new IllegalStateException("Sidecar pool was reset while starting a container for key " + key);
}
// Tracked before the health wait, not after: a timeout below must not stop
// stopContainer()/stopAll() from ever finding this already-running container again.
containers.put(key, started);
@@ -96,6 +115,7 @@ public class PerKeyContainerPool {
/** Stops and removes every container this pool has started, then forgets them all. */
public void stopAll() {
generation.incrementAndGet();
if (containers.isEmpty()) {
return;
}
@@ -326,11 +326,7 @@ public class CodeArtifactService implements Resettable {
/**
* Generic over whatever formats a repository actually has sidecar ids for: adding the next
* format's sidecar needs no change here, only a new {@link RepositorySidecarManager}
* implementation that {@link CodeArtifactSidecarRegistry} discovers. Shared by
* {@link #deleteRepository} and {@link #beforeReset}, both of which forget the repository's
* record right after calling this and so must not leave its sidecar storage (a Docker
* container, for a per-repository sidecar such as Verdaccio) running with nothing left able to
* ever release it again.
* implementation that {@link CodeArtifactSidecarRegistry} discovers.
*/
private void releaseSidecarStorage(CodeArtifactRepository r, String domain, String repository) {
r.getSidecarContainerIds().forEach((format, containerId) ->
@@ -759,18 +755,6 @@ public class CodeArtifactService implements Resettable {
ref.repository()).getTags();
}
/**
* Releases every repository's sidecar storage before {@link #clear} wipes the records that
* name it. Without this, an emulator reset forgets which sidecar container backs a repository
* without ever stopping it: harmless for Maven's one shared Reposilite instance, but a real
* Docker container leak for a per-repository sidecar such as Verdaccio, one abandoned
* container per repository that existed at reset time.
*/
@Override
public void beforeReset() {
repositories.scanAllAccounts().forEach(r -> releaseSidecarStorage(r, r.getDomainName(), r.getName()));
}
@Override
public void clear() {
domains.clear();
@@ -1,6 +1,7 @@
package io.github.hectorvent.floci.services.codeartifact;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.ContainerTeardown;
import io.github.hectorvent.floci.core.common.docker.ContainerBuilder;
import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager;
import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager.ContainerInfo;
@@ -8,9 +9,7 @@ import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager.E
import io.github.hectorvent.floci.core.common.docker.ContainerSpec;
import io.github.hectorvent.floci.core.common.docker.ContainerStorageHelper;
import io.github.hectorvent.floci.core.common.docker.SidecarHealthHelper;
import io.quarkus.runtime.ShutdownEvent;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.event.Observes;
import jakarta.inject.Inject;
import org.jboss.logging.Logger;
@@ -30,9 +29,15 @@ import java.util.Optional;
* <p>One repository-agnostic container backs every CodeArtifact domain/repository pair; a
* CodeArtifact repository maps to a named Reposilite repository provisioned on first use (see
* {@link ReposiliteSidecarClient#ensureRepository}), not to its own container.
*
* <p>Implements {@link ContainerTeardown} rather than observing {@code ShutdownEvent} directly:
* {@code ContainerTeardowns.stopAll} already runs every implementation both at process shutdown
* and on {@code /state/reset}/{@code /state/nuke}, so a reset now actually stops the managed
* container instead of leaving it running with every {@code CodeArtifactRepository} record that
* named it gone.
*/
@ApplicationScoped
public class ReposiliteSidecarManager {
public class ReposiliteSidecarManager implements ContainerTeardown {
static final String IMAGE_ENV = "FLOCI_SERVICES_CODEARTIFACT_MAVEN_IMAGE";
static final String URL_ENV = "FLOCI_SERVICES_CODEARTIFACT_MAVEN_URL";
static final String MANAGED_TOKEN_NAME = "floci-manager";
@@ -161,7 +166,8 @@ public class ReposiliteSidecarManager {
LOG.infov("Reposilite sidecar is ready at {0}", resolvedUrl);
}
void onStop(@Observes ShutdownEvent event) {
@Override
public void stopManagedContainers() {
if (containerId == null) {
return;
}
@@ -1,6 +1,7 @@
package io.github.hectorvent.floci.services.codeartifact;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.ContainerTeardown;
import io.github.hectorvent.floci.core.common.docker.ContainerBuilder;
import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager;
import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager.ContainerInfo;
@@ -8,9 +9,7 @@ import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager.E
import io.github.hectorvent.floci.core.common.docker.ContainerSpec;
import io.github.hectorvent.floci.core.common.docker.ContainerStorageHelper;
import io.github.hectorvent.floci.core.common.docker.PerKeyContainerPool;
import io.quarkus.runtime.ShutdownEvent;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.event.Observes;
import jakarta.inject.Inject;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
@@ -35,9 +34,15 @@ import java.nio.charset.StandardCharsets;
* real CodeArtifact authorization check happens once at Floci's proxy layer
* ({@code CodeArtifactNpmDataPlane}), before a request ever reaches the container; the container
* itself is never reachable directly by a client.
*
* <p>Implements {@link ContainerTeardown} rather than observing {@code ShutdownEvent} directly:
* {@code ContainerTeardowns.stopAll} already runs every implementation both at process shutdown
* and on {@code /state/reset}/{@code /state/nuke}, so this is what actually stops every
* repository's container on reset, not the generic {@code CodeArtifactService} repository-record
* walk, which only knows about repositories that still exist in storage.
*/
@ApplicationScoped
public class VerdaccioSidecarManager implements RepositorySidecarManager {
public class VerdaccioSidecarManager implements RepositorySidecarManager, ContainerTeardown {
private static final Logger LOG = Logger.getLogger(VerdaccioSidecarManager.class);
private static final String FORMAT = "npm";
@@ -171,7 +176,8 @@ public class VerdaccioSidecarManager implements RepositorySidecarManager {
}
}
void onStop(@Observes ShutdownEvent ignored) {
@Override
public void stopManagedContainers() {
pool.stopAll();
}
}