Skip to content

fix(cache): give each compose-config write its own temp file (BDS-133) - #93

Merged
simensen merged 1 commit into
mainfrom
beau/bds-133-concurrent-compose-cache-writes
Aug 19, 2026
Merged

simensen merged 1 commit into
mainfrom
beau/bds-133-concurrent-compose-cache-writes

Conversation

@simensen

Copy link
Copy Markdown
Member

Closes BDS-133.

Problem

__xcind-write-compose-config always wrote to ${dest}.tmp. Two concurrent xcind commands for the same application and cache SHA shared that one path:

  1. Process A opens and writes resolved-config.yaml.tmp.
  2. Process B opens and writes the same path.
  3. Process A renames the temp path into place.
  4. Process B tries the same rename, but the path is gone.

Process B fails with mv: cannot stat '.../resolved-config.yaml.tmp': No such file or directory. Process B also keeps writing through its open descriptor after the rename, so it can modify the artifact another process already published. That behavior defeats the atomic-write guarantee and can expose incomplete content.

Change

Create the temp file with mktemp in the destination directory, one per call. Rename it only after Docker Compose succeeds, and remove it on any error.

The rename stays atomic, because the temp file and the destination sit on one filesystem. Concurrent writers complete independently. The last writer to finish wins.

This matches the pattern __xcind-write-cache-file and __xcind-write-cache-config-json already use.

Test

The regression test re-enters __xcind-write-compose-config for the same destination from inside the docker stub, while the outer call's temp file is still open. Two writes therefore overlap inside one process, with no timing dependency and no flakiness.

Verified against the pre-fix code: the test reproduces the exact error from the issue. It covers four things.

  • A failed Compose command keeps the current artifact and removes its temp file.
  • A successful write replaces the artifact and leaves no temp file.
  • Both overlapping writers succeed.
  • Overlapping writes leave no temp files behind.

make check passes with zero failures.

Out of scope

__xcind-run-hooks removes and rebuilds one shared generated-hook directory without a lock. That is a separate concurrency risk. It needs a per-SHA lock around the full prepare pipeline, which carries different risk, so it belongs in its own issue.

__xcind-write-compose-config always wrote to "${dest}.tmp". Two concurrent
xcind commands for the same app and cache SHA therefore shared one temp
path: both opened it, the first renamed it into place, and the second
failed with "mv: cannot stat ... resolved-config.yaml.tmp". The second
writer also kept writing through its open descriptor after the rename,
which defeated the atomic-write guarantee and could expose partial content.

Use mktemp in the destination directory instead, matching
__xcind-write-cache-file and __xcind-write-cache-config-json. The rename
stays atomic on one filesystem, concurrent writers complete independently,
and the last one to finish wins.

The regression test re-enters the function from inside the docker stub, so
two writes overlap deterministically with no timing dependency.
@simensen
simensen merged commit 6cc0ca5 into main Aug 19, 2026
16 of 17 checks passed
@simensen
simensen deleted the beau/bds-133-concurrent-compose-cache-writes branch August 19, 2026 22:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant