From e4f72c2d4a8e03105b3dd8a144ec16cacc3c0be8 Mon Sep 17 00:00:00 2001 From: Edgar Arakelyan Date: Mon, 16 Nov 2026 19:40:22 -0800 Subject: [PATCH] fix(bootstrap): an infra image build holds this machine's lock for its tag Two test workers, each with a state root of its own, set up the gateway env at once on main's v0.9.1550 bump and built env-gateway together; the second failed with "image ... already exists", main went red and the release didn't publish. The build now holds image-build:, as a host image's build does, which the integration tier's workers share. Co-Authored-By: Claude Opus 5.5 --- src/agent_env/env/bootstrap.py | 7 +++++-- tst/unit/env/bootstrap_test.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/agent_env/env/bootstrap.py b/src/agent_env/env/bootstrap.py index b041dee91..db2f5e03d 100644 --- a/src/agent_env/env/bootstrap.py +++ b/src/agent_env/env/bootstrap.py @@ -139,9 +139,12 @@ def _image(label: str, artifact_id: str, description: str, dockerfile: Path, ima def _build_and_put(label: str, artifact_id: str, description: str, dockerfile: Path, image_name: str, *, platform: str | None, say: Callable[[str], None]) -> DockerImageArtifact: """Build ``dockerfile``'s image for ``platform`` from the folder it's in, and write it with that folder as its build - context, which a sandbox that can't run its tar.gz builds it from.""" + context, which a sandbox that can't run its tar.gz builds it from. The build holds this machine's lock for its tag, + as a host image's does: a run with a state root of its own builds the same tag with the same Docker, and two builds + of one tag at once fail with "image ... already exists".""" say(f"Building {label} image from {dockerfile}...") - build_image(dockerfile, dockerfile.parent, image_name, platform=platform) + with holding_locks([f"image-build:{image_name}"]): + build_image(dockerfile, dockerfile.parent, image_name, platform=platform) say(f"Creating {label} DockerImageArtifact...") artifact = DockerImageArtifact.put(id=artifact_id, description=description, image_name=image_name, build_context_path=str(dockerfile.parent), dockerfile_path=str(dockerfile)) diff --git a/tst/unit/env/bootstrap_test.py b/tst/unit/env/bootstrap_test.py index 7e54a0ff5..7de27355e 100644 --- a/tst/unit/env/bootstrap_test.py +++ b/tst/unit/env/bootstrap_test.py @@ -2,6 +2,7 @@ stores only, once each however many runs ask; pulled from the images published for their sources when every one of a kind's is, else built for this host's platform. One built here switches to them once they're published.""" +import contextlib import re from pathlib import Path @@ -69,6 +70,33 @@ def keep(id, *, description, image_name, build_context_path=None, dockerfile_pat assert kept == [(dockerfile.parent, str(dockerfile)) for dockerfile in bootstrap._DOCKERFILES[kind]] +@_INFRA_PUTS +def test_each_infra_image_is_built_holding_this_machines_lock_for_its_tag(local_stores, monkeypatch, kind, put): + """Every agent-env process here builds with the one Docker, a run with a state root of its own too, as each test + worker's is, and two builds of one tag at once fail with "image ... already exists".""" + held, built = [], [] + + @contextlib.contextmanager + def holding(ids, **kwargs): + held.extend(ids) + try: + yield + finally: + del held[-len(ids):] + + def keep(id, *, description, image_name, build_context_path=None, dockerfile_path=None, platform=None): + return get_artifact_store().put_document(DockerImageArtifact( + id=id, description=description, image_name=image_name, tar_gz_s3_url="file:///x.tar.gz")) + + monkeypatch.setattr(bootstrap, "holding_locks", holding) + monkeypatch.setattr(bootstrap, "build_image", lambda dockerfile, context, tag, **kwargs: built.append((tag, [*held]))) + monkeypatch.setattr(bootstrap.DockerImageArtifact, "put", keep) + + put("x", platform=None) + + assert built and all(f"image-build:{tag}" in locks for tag, locks in built), built + + @pytest.fixture def published(monkeypatch): """The kinds whose images the registry serves, all under ``_DIGEST``: those added to the set returned."""