Skip to content

Commit 94cdd0f

Browse files
fix: two builds that find a std module missing compile it once
Co-authored-by: speak-agent <248744407+speak-agent@users.noreply.github.com>
1 parent 48003ab commit 94cdd0f

3 files changed

Lines changed: 74 additions & 1 deletion

File tree

‎CHANGELOG.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,13 @@
105105

106106
### Fixed
107107

108+
- Two builds that find a std module missing compile it once. The std module of a
109+
toolchain and flag set is one entry of the home's build cache, and two
110+
processes that found it missing compiled into the same files at once: one
111+
compiler read a BMI the other was rewriting (a bus error) or found its
112+
output gone, and a build went on with no std object. The entry is built
113+
under its own lock; a second build waits, naming the first, and finds it
114+
built (e2e 1039).
108115
- A dependency's source whose `main` is compiled only under a switch no command
109116
defines -- zlib's `crc32.c` (`#ifdef MAKECRCH`), QuickJS's `libregexp.c`
110117
(`#ifdef TEST`) -- is linked into a test that has its own `main`. Which

‎src/toolchain/stdmod.cppm‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ export module mcpp.toolchain.stdmod;
3131

3232
import std;
3333
import mcpp.home;
34-
import mcpp.home.files; // mark_used
34+
import mcpp.home.files; // mark_used, FileLock
35+
import mcpp.build.dirlock;
36+
import mcpp.ui;
3537
import nlohmann.json;
3638
import mcpplibs.os.platform;
3739
import mcpp.build.command_line;
@@ -473,6 +475,24 @@ std::expected<StdModule, StdModError> ensure_built(
473475
const auto& compatCommands = desc.compatCommands;
474476
const auto& metadata = derivation->metadata;
475477
auto metaPath = metadata_path(sm.cacheDir);
478+
// ONE BUILDER PER ENTRY. The entry is shared by every build of this home, and two
479+
// processes that found it missing compiled into the same files at once: the
480+
// second clang read a std.pcm the first was rewriting (SIGBUS), and a build
481+
// went on with no std.o. The check and the build happen under the entry's
482+
// lock, so a second process waits and then finds the entry built.
483+
{
484+
std::error_code ec;
485+
std::filesystem::create_directories(sm.cacheDir, ec);
486+
}
487+
namespace dirlock = mcpp::build::dirlock;
488+
auto entryLock = mcpp::home::FileLock::acquire(
489+
sm.cacheDir, ".mcpp-build.lock", dirlock::owner_record(), std::chrono::milliseconds(300),
490+
[&](std::string_view record) {
491+
const auto now = std::chrono::duration_cast<std::chrono::seconds>(
492+
std::chrono::system_clock::now().time_since_epoch()).count();
493+
mcpp::ui::status("Waiting", std::format("for the std module, compiled by {}",
494+
dirlock::describe(dirlock::parse_holder(record), now)));
495+
});
476496
bool std_cached = std::filesystem::exists(sm.bmiPath)
477497
&& std::filesystem::exists(sm.objectPath)
478498
&& metadata_matches(metaPath, metadata);
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/usr/bin/env bash
2+
# resources: machine
3+
# 1039 -- two builds that find the std module missing compile it once (#906 self-review).
4+
#
5+
# The std module of a toolchain and flag set is one entry of the home's build cache, shared by every
6+
# build. Two processes that found it missing compiled into the same files at once: the second clang
7+
# read a std.pcm the first was rewriting (SIGBUS), and a build went on with no std.o (e2e 957 and 987,
8+
# run side by side on one runner). The entry is now built under its own lock.
9+
#
10+
# Criteria: with the entry two projects use removed, the two build at once and both succeed; the entry
11+
# is whole afterwards (its BMI, object and record).
12+
#
13+
# It holds the machine: it removes an entry of the shared home's cache, which no other test may be
14+
# building from at that moment.
15+
set -e
16+
TMP=$(mktemp -d)
17+
trap 'rm -rf "$TMP"' EXIT
18+
fail() { echo "FAIL: $1"; shift; for f in "$@"; do echo "--- $f ---"; cat "$f" 2>/dev/null; done; exit 1; }
19+
PY=python3; command -v python3 >/dev/null 2>&1 || PY=python
20+
21+
cd "$TMP"
22+
for p in one two; do "$MCPP" new "$p" > /dev/null 2>&1 || fail "mcpp new $p"; done
23+
(cd one && "$MCPP" build > ../first.log 2>&1) || fail "the first build" first.log
24+
25+
# The entry the first build used: the std module whose record it marked last.
26+
home="${MCPP_HOME:-$HOME/.mcpp}"
27+
entry="$("$PY" - "$home/build-cache/v1/std" <<'PY'
28+
import os, sys
29+
root = sys.argv[1]
30+
records = [os.path.join(root, d, "std-module.json") for d in os.listdir(root)] if os.path.isdir(root) else []
31+
records = [r for r in records if os.path.isfile(r)]
32+
print(os.path.dirname(max(records, key=os.path.getmtime)) if records else "")
33+
PY
34+
)"
35+
[ -n "$entry" ] || fail "no std module entry under $home/build-cache/v1/std" first.log
36+
rm -rf "$entry"
37+
38+
(cd one && "$MCPP" build > ../one.log 2>&1) & a=$!
39+
(cd two && "$MCPP" build > ../two.log 2>&1) & b=$!
40+
sa=0; sb=0; wait $a || sa=$?; wait $b || sb=$?
41+
[ "$sa" = 0 ] || fail "the first of two builds at once (exit $sa)" one.log two.log
42+
[ "$sb" = 0 ] || fail "the second of two builds at once (exit $sb)" one.log two.log
43+
[ -f "$entry/std-module.json" ] || fail "the entry has no record afterwards" one.log two.log
44+
ls "$entry"/*.o > /dev/null 2>&1 || fail "the entry has no object afterwards" one.log two.log
45+
46+
echo "PASS: 1039 two builds that find the std module missing compile it once"

0 commit comments

Comments
 (0)