fix(snapshot): verify and repair a sealed materialized tree
VoidBuildSystem CI / 1 · Requirements (push) Successful in 8s
VoidBuildSystem CI / 2B · Windows Wine build (debug) (push) Successful in 1m51s
VoidBuildSystem CI / 2A · Linux build (debug) (push) Successful in 2m55s
VoidBuildSystem CI / 2A · Linux build (sanitizers) (push) Successful in 3m10s
VoidBuildSystem CI / 2B · Windows Wine build (relwithdebinfo) (push) Successful in 3m6s
VoidBuildSystem CI / 3B · Windows Wine test (debug) (push) Successful in 17s
VoidBuildSystem CI / 3B · Windows Wine test (relwithdebinfo) (push) Successful in 18s
VoidBuildSystem CI / 2A · Linux build (relwithdebinfo) (push) Successful in 3m47s
VoidBuildSystem CI / 4B · Windows package (debug) (push) Successful in 18s
VoidBuildSystem CI / 4B · Windows package (relwithdebinfo) (push) Successful in 41s
VoidBuildSystem CI / 3A · Linux test (sanitizers) (push) Successful in 1m19s
VoidBuildSystem CI / 3A · Linux test (relwithdebinfo) (push) Successful in 5m16s
VoidBuildSystem CI / 3A · Linux test (debug) (push) Successful in 5m26s
VoidBuildSystem CI / 4A · Linux package (debug) (push) Successful in 1m14s
VoidBuildSystem CI / 4A · Linux package (sanitizers) (push) Successful in 1m22s
VoidBuildSystem CI / 4A · Linux package (relwithdebinfo) (push) Successful in 1m25s
VoidBuildSystem CI / 4A2 · VBS installers (self-hosted) (push) Failing after 1m27s
VoidBuildSystem CI / 5 · Release (push) Skipped
VoidBuildSystem CI / 4A2 · VBS installers (windows target) (push) Failing after 1m28s

A concurrent capture can seal a tree while it is incomplete, and the
seal marker makes that permanent (the installer jobs failed with
missing VoidLibrary sources in the same tree id). After sealing or
reusing a tree, check every entry exists; rebuild it (up to three
attempts) when files are missing.

Verified: the self-hosted VBS install still succeeds locally.
This commit is contained in:
faumaray committed 2026-12-06 10:48:43 +03:00
1 parent 4f70e0640c
commit 2f989f85ea
1 file changed
+70 -49
+70 -49
View File
@@ -491,62 +491,83 @@ Void::Result::Result<InputSnapshot, Void::Result::Error> capture_snapshot(
if (options.materialize) {
snap.materialized_root = options.snapshot_store_dir / "trees" / snap.snapshot_id;
const auto seal_marker = snap.materialized_root / ".sealed";
if (!std::filesystem::exists(seal_marker, ec)) {
// Unique per capture: concurrent captures in one process share the
// pid, and a shared staging directory lets one capture remove files
// another has already copied (sealing an incomplete tree).
static std::atomic<std::uint64_t> staging_sequence{0};
const auto tmp_tree =
options.snapshot_store_dir / "trees" /
(snap.snapshot_id + ".staging_" +
std::to_string(static_cast<long long>(::getpid())) + "_" +
std::to_string(staging_sequence.fetch_add(1, std::memory_order_relaxed)));
std::filesystem::remove_all(tmp_tree, ec);
std::filesystem::create_directories(tmp_tree, ec);
// A tree can be sealed while incomplete (interrupted concurrent capture),
// and the seal marker would make that permanent: verify and repair.
for (int materialize_attempt = 0; materialize_attempt < 3; ++materialize_attempt) {
if (!std::filesystem::exists(seal_marker, ec)) {
// Unique per capture: concurrent captures in one process share the
// pid, and a shared staging directory lets one capture remove files
// another has already copied (sealing an incomplete tree).
static std::atomic<std::uint64_t> staging_sequence{0};
const auto tmp_tree =
options.snapshot_store_dir / "trees" /
(snap.snapshot_id + ".staging_" +
std::to_string(static_cast<long long>(::getpid())) + "_" +
std::to_string(staging_sequence.fetch_add(1, std::memory_order_relaxed)));
std::filesystem::remove_all(tmp_tree, ec);
std::filesystem::create_directories(tmp_tree, ec);
for (auto &[logical, entry] : snap.entries) {
const auto dst = tmp_tree / logical;
std::filesystem::create_directories(dst.parent_path(), ec);
if (entry.kind == EntryKind::RegularFile && !entry.materialized_path.empty()) {
auto copied =
Platform::copy_isolated(entry.materialized_path, dst, entry.mode);
if (!copied) {
std::filesystem::remove_all(tmp_tree, ec);
return std::unexpected{copied.error()};
}
(void)Platform::set_canonical_mtime(dst, options.reproducibility_epoch);
} else if (entry.kind == EntryKind::Symlink) {
if (!entry.sha256.empty()) {
// Materialize symlinked regular file target inside snapshot tree
// safely.
const auto src_target = source_files_to_verify.find(logical);
if (src_target != source_files_to_verify.end()) {
auto copied =
Platform::copy_isolated(src_target->second, dst, entry.mode);
if (!copied) {
std::filesystem::remove_all(tmp_tree, ec);
return std::unexpected{copied.error()};
for (auto &[logical, entry] : snap.entries) {
const auto dst = tmp_tree / logical;
std::filesystem::create_directories(dst.parent_path(), ec);
if (entry.kind == EntryKind::RegularFile &&
!entry.materialized_path.empty()) {
auto copied =
Platform::copy_isolated(entry.materialized_path, dst, entry.mode);
if (!copied) {
std::filesystem::remove_all(tmp_tree, ec);
return std::unexpected{copied.error()};
}
(void)Platform::set_canonical_mtime(dst,
options.reproducibility_epoch);
} else if (entry.kind == EntryKind::Symlink) {
if (!entry.sha256.empty()) {
// Materialize symlinked regular file target inside snapshot
// tree safely.
const auto src_target = source_files_to_verify.find(logical);
if (src_target != source_files_to_verify.end()) {
auto copied = Platform::copy_isolated(src_target->second, dst,
entry.mode);
if (!copied) {
std::filesystem::remove_all(tmp_tree, ec);
return std::unexpected{copied.error()};
}
(void)Platform::set_canonical_mtime(
dst, options.reproducibility_epoch);
}
(void)Platform::set_canonical_mtime(dst,
options.reproducibility_epoch);
}
}
}
}
(void)Platform::atomic_write_file(tmp_tree / ".sealed", snap.snapshot_id + "\n",
false);
std::filesystem::rename(tmp_tree, snap.materialized_root, ec);
if (ec) {
// Another concurrent worker may have sealed the identical content-addressed
// tree.
std::filesystem::remove_all(tmp_tree, ec);
if (!std::filesystem::exists(seal_marker, ec)) {
return std::unexpected{
make_error(Void::System::SystemErrorCode::io_error,
std::format("snapshot: failed publishing tree '{}'",
snap.materialized_root.string()))};
(void)Platform::atomic_write_file(tmp_tree / ".sealed",
snap.snapshot_id + "\n", false);
std::filesystem::rename(tmp_tree, snap.materialized_root, ec);
if (ec) {
// Another concurrent worker may have sealed the identical
// content-addressed tree.
std::filesystem::remove_all(tmp_tree, ec);
if (!std::filesystem::exists(seal_marker, ec)) {
return std::unexpected{make_error(
Void::System::SystemErrorCode::io_error,
std::format("snapshot: failed publishing tree '{}'",
snap.materialized_root.string()))};
}
}
}
std::size_t missing = 0;
for (const auto &[logical, entry] : snap.entries) {
std::error_code check_ec;
if (!std::filesystem::exists(snap.materialized_root / logical, check_ec)) {
++missing;
}
}
if (missing == 0) {
break;
}
std::fprintf(stderr,
"snapshot: materialized tree '%s' misses %zu file(s); rebuilding\n",
snap.snapshot_id.c_str(), missing);
std::filesystem::remove_all(snap.materialized_root, ec);
}
for (auto &[logical, entry] : snap.entries) {
entry.materialized_path = snap.materialized_root / logical;