Skip to content

Commit 65a160e

Browse files
authored
fix(exec): roll back the model write when mxbuild crashes (#424)
Independent review: APPROVE. # Review: PR #424, exec.sh rolls the write back when mxbuild crashes (crash-rollback split from #311) - Head: `aade99b7` (`fix/exec-crash-rollback`), one commit on master `2def0679`. 3 files: `project-bin/exec.sh` (+24/-3, one hunk), new `tests/wave2/test-exec-crash-rollback.sh` (118), one CHANGELOG line. - Read-only detached worktrees under /tmp, removed afterwards. Synthetic fixtures only. macOS 26.6, bash 3.2 via `bash`. - Verdict: **APPROVE** (items below are small; 1 and 2 are worth doing before merge, 3 to 5 are notes). ## Claims checked | Claim | Result | |---|---| | New test 13/0 on the branch | **Reproduced**: 13 passed, 0 failed | | 9/4 on master's exec.sh | **Reproduced**: 9 passed, 4 failed, all four in case B (write kept, model differs, no message, no row) | | Extracted from #311 only, no guard | **Yes.** The hunk is the `elif [ "$MXBUILD_EXIT" -ne 0 ]` branch only. No refusal path, no new script, no routed text, no `applied.tsv`, no baseline words. | ## Behaviour against the T3 intent | Question | Finding | |---|---| | Restores exactly the pre-exec model? | Yes. `restore_snapshot` -> `restore-mpr.sh` replaces `mprcontents/` wholesale (copy to temp, `rm -rf`, `mv`), so units the write ADDED are removed too, then copies the `.mpr`, then checks the unit count. Case B asserts a byte-identical checksum of `.mpr` + unit. | | Only on a crash, not on a normal build error? | Yes. A written errors file goes down the existing CE branch (case D: ordinary `gate failed` row, crash branch not taken). The new code sits only in "non-zero exit and no/empty errors file". Matches T3-S2. | | Only when the baseline is known? | Yes. `BASE_KNOWN=1` comes from a measured baseline (clean or with errors) or the clean verification stamp. Case C (baseline crash too) keeps the old "kept, flagged" behaviour. I also ran T3-S4 by hand (baseline with a CE error list, then crash): restored, model identical, rc 1, one honest row. | | `--patch` | Unchanged: `PATCH_MODE=1` takes the old `else`, and the later `--patch` block restores once (`RESTORE_TRIED=0`). No double restore. Read only, not run (no test case, see item 1). | | Backup step fails | No data loss. `./bin/snapshot-mpr.sh` runs bare under `set -e` (exec.sh:79), and the snapshot script is `set -euo pipefail`, so a failed snapshot aborts the exec **before** the write. If the restore itself fails, the branch prints the STOP line and logs "NOT rolled back (restore did not run)". `restore-mpr.sh` leaves the working tree untouched when its copy fails. | | Exit code and stamp | `GATE_STATE=fail`, so the stamp is cleared and exec exits 1 (verified: rc 1 in B and S4). The next exec re-measures the baseline. | | `--on-copy` | Not on master (#384 is open), so no interaction yet. Whoever lands second has to look at this branch again: under `--on-copy`, rollback means "discard the copy", not "restore the snapshot". T3 order (this first, #384 rebases over one hunk) still holds. | | Ctrl-C | **Unchanged from master, not made worse.** I tested it: SIGINT to the process group while the post-write mxbuild runs. exec.sh dies (rc 130) in its `kill -0` / `sleep 1` poll, so the crash branch never runs. The write is kept, there is no BUILD-LOG row, and the background mxbuild keeps running (async children ignore SIGINT in a non-interactive shell). A Ctrl-C does not trigger a rollback. The only INT exposure is a Ctrl-C during `restore-mpr.sh`, between its `rm -rf mprcontents` and `mv`. That window already exists on the failed-gate path. This belongs to T3-S5 / E5, not to this PR. | ## Items 1. **Test gaps (small).** (a) Case B does not assert the exit code. Add `[ "$RC" -eq 1 ]`. (b) There is no case for a baseline that is known from the **verification stamp** rather than measured. That is the second source of `BASE_KNOWN=1`, and the one in item 2. (c) There is no `--patch` crash case (T3-S3), which would guard against a double restore. (d) The new file is mode 100644, while the other exec tests are 755. `run-all.sh` runs it with `bash`, so this is consistency only. 2. **The stamp case: wording and a possible false rollback.** The new message says "mxbuild ran on this model before the write". When the baseline was skipped because of the stamp, mxbuild did not run in this exec. The stamp fingerprint covers the model only (`model-stamp.sh`: `.mpr` + units), not the mxbuild or JDK version. So a Studio Pro or mxbuild change between the stamp and this exec can make mxbuild die from a machine problem, and that gets read as a write problem. The write is then rolled back. This is recoverable (re-run the script, snapshot kept), but the row then says something untrue. Either reword the message and row to "baseline known (measured, or verification stamp)", or keep the rollback only when the baseline was measured in this run, and treat a stamp-only baseline like case C. My recommendation is the rewording: T3 explicitly counts the stamp as known. 3. **CHANGELOG: two consequences after a pull.** (a) With `MXTK_GATE_TIMEOUT` set, a timed-out gate (exit 124, no errors file) now **rolls back** a write that may be fine. `_common.sh:608-611` already warns about exactly this. The default is unbounded, so only opt-in users see it. (b) T3 asked for the run output to say where the crash output is kept. The output prints the first 20 mxbuild lines and the snapshot path, which is enough, but one clause in the CHANGELOG line would help a project that relied on "kept, open Studio Pro". 4. Note, not this PR: `restore_snapshot` picks the newest snapshot by mtime (`ls -dt`), not `$SNAP_NOW`. This is pre-existing and shared with the failed-gate path. Under the exec lock it is the right directory, but passing `$SNAP_NOW` would make it exact. 5. Note: the V1-to-V2 repack case (the snapshot has no `mprcontents/` but the live model does) makes `restore-mpr.sh` refuse. The branch then logs "NOT rolled back" honestly. The failed-gate path behaves the same way. Fine. ## Verdict **APPROVE.** Before merge, preferably: 1 (exit-code assert, stamp case, patch case) and 2 (honest wording for a stamp-only baseline). 3 is one CHANGELOG clause. 4 and 5 are notes.
1 parent 9655978 commit 65a160e

3 files changed

Lines changed: 140 additions & 3 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Sections dated before 2026-09-19 predate the cycle and stay as they are. Named m
1919

2020
## Unreleased
2121

22+
- fix(exec): **when mxbuild crashes on the written model (non-zero exit, no error list) after a baseline that mxbuild did measure, exec.sh now rolls the write back instead of keeping it.** Before, the default path kept the write, the next exec printed "Baseline NOT measured" and built on a model nothing could load. One BUILD-LOG row says the script was rolled back and carries the mxbuild exit code; without a measured baseline (the crash may be the machine) and in `--patch` mode the old behaviour is unchanged. No new refusal, no guard. Extracted from the crash-rollback hunk of #311. Test: `tests/wave2/test-exec-crash-rollback.sh` (synthetic stub mxbuild; 4 of its checks fail on master) — field incident 2026-10-13 (#309)
2223
- fix(_common): **`find_java` on macOS picks the JDK that matches the model's Mendix version: 25 for 11.14 and above, 21 below (and when the version cannot be read).** It always asked for `java_home -v 21`, a pin added for 11.12.2 (JDK 25 breaks that build's gradle 8.5). On 11.15 the same pin broke the other way: mxbuild failed `release version 25 not supported` (exit 3) on the first Java-action build, 21 times in one lab phase, until `JAVA_HOME` was set by hand in `toolkit.env`. Mendix's system requirements list JDK 21 for Studio Pro 11.13 and below and JDK 25 for 11.14 and above. The version comes from `mxtk_model_version` (the .mpr's `_MetaData`). Detection uses `java_home -F -v N`, because plain `-v N` never fails on macOS (a missing JDK silently returns the default, exit 0). When the wanted JDK is missing, the default is still used, and the new `find_java_note` line is printed by exec.sh, verify-model.sh and doctor.sh (they all discard find_java's stderr). `JAVA_HOME` still wins; `MXTK_JAVA_HOME_BIN` overrides the java_home binary for tests. `tests/wave2/test-find-java-version.sh` (fake java_home that mimics the real fallback-with-exit-0, 14 cases) fails on master for 11.14, 11.15 and 12.x. Not run: a real two-JDK Mac build; the version table is taken from the Mendix docs. Field report: a five-app lab project (Mendix 11.15, mxcli v0.26.0), TK-018 — MendixMau
2324
- learn(workflow skills): **a Jump has one fixed target, so one failure handler for N steps is a child workflow, not a shared node; plus three probed workflow errors (CE0117 on a `System.Workflow` argument means map `$WorkflowInstance`, CE1613 means the called workflow's parameter must be `$WorkflowContext`, and `returns` goes before `exposed as workflow action`).** `workflow-structure-rules.md` §1 and `learned-workflow-patterns.md` §9. The §9 rows were probed on mxcli v0.26.0 / Mendix 11.15.0 with native `mx check`. The third row corrects the reading that a called microflow cannot return a value. Upstream issues: mendixlabs/mxcli#1529 (CE0117), #1530 (CE1613, and `syntax workflow call-workflow` shows the failing form), #1531 (describe drops every call workflow mapping), #1533 (`returns` parse messages). Source: a five-app lab project (Mendix 11.15, mxcli v0.26.0) — five-app lab report, WF-1
2425
- learn(fixture-seeding): **resetting workflow data: abort instances through the runtime (Workflow Commons admin or *Change workflow state*); if SQL is unavoidable, delete the link tables first (`system$workflow_currentactivity`, `system$workflow_workflowdefinition`, `system$workflowusertask_assignees`, `system$workflowusertask_targetusers`, `system$workflowusertask_targetgroups`, plus the `system$workflowendedusertask_*` equivalents; names read from a Mendix 11.15 database), and reset a context object only after its instances are gone.** A seed reset that deleted workflow rows by SQL failed on those links (WF-5). Text only, `ondemand` skill. — five-app lab report, WF-5

‎project-bin/exec.sh‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1769,9 +1769,27 @@ EOF
17691769
echo " ✗ mxbuild failed to run (exit $MXBUILD_EXIT) — gate could not verify the model."
17701770
[ -n "$MXB_WHY" ] && echo " mxbuild says: $MXB_WHY"
17711771
echo "$MXBUILD_OUT" | grep -v "^$\|icon\|Microsoft\|Assembly\|__" | head -20 || true
1772-
echo " → Snapshot preserved. Open in SP to verify manually before proceeding."
1773-
# --patch logs its own row below, after rolling back; one row per run.
1774-
[ "$PATCH_MODE" = "1" ] || log_build "❌ gate could not run" "mxbuild exit $MXBUILD_EXIT"
1772+
# mxbuild CRASHED ON THE WRITTEN MODEL (field 2026-10-13, Mendix 11.14.0, mxcli v0.25.0): a
1773+
# two-hop data source passed `check`, then mxbuild's loader died with no errors file. This
1774+
# branch kept the write, the next exec printed "Baseline NOT measured" and wrote on top of a
1775+
# model nothing could load. BASE_KNOWN=1 means mxbuild ran on this model a moment ago
1776+
# (measured baseline, or a clean verification stamp), so the crash is a property of the
1777+
# WRITE, not of the machine: roll it back like a failed gate. Without a measured baseline it
1778+
# may be the machine (wrong version, JDK), and the old behaviour stays: kept, flagged.
1779+
if [ "$PATCH_MODE" != "1" ] && [ "${BASE_KNOWN:-0}" = "1" ]; then
1780+
echo " ✗ mxbuild ran on this model before the write and crashed after it — rolling the write back."
1781+
restore_snapshot
1782+
if [ "$RESTORED" -eq 1 ]; then
1783+
log_build "❌ gate could not run" "mxbuild exit $MXBUILD_EXIT on the written model (ran on the baseline) — script rolled back${MXB_WHY:+; mxbuild: $MXB_WHY}"
1784+
else
1785+
echo " STOP: the model may not load. Restore it before the next exec: ./bin/restore-mpr.sh <newest .mpr-snapshots/ dir>"
1786+
log_build "❌ gate could not run" "mxbuild exit $MXBUILD_EXIT on the written model — NOT rolled back (restore did not run)"
1787+
fi
1788+
else
1789+
echo " → Snapshot preserved. Open in SP to verify manually before proceeding."
1790+
# --patch logs its own row below, after rolling back; one row per run.
1791+
[ "$PATCH_MODE" = "1" ] || log_build "❌ gate could not run" "mxbuild exit $MXBUILD_EXIT"
1792+
fi
17751793
else
17761794
GATE_STATE="pass"; GATE_ERRORS=0
17771795
echo " ✓ mxbuild: 0 errors — model is clean."
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
#!/usr/bin/env bash
2+
# test-exec-crash-rollback.sh — exec.sh on the DEFAULT path (no --patch, no --on-copy) when
3+
# mxbuild crashes on the written model.
4+
#
5+
# usage: bash tests/wave2/test-exec-crash-rollback.sh [path-to-exec.sh]
6+
#
7+
# Field (2026-10-13, Mendix 11.14.0): a script passed `mxcli check`, mxbuild's loader then died
8+
# with no errors file. exec.sh kept the write; the next exec printed "Baseline NOT measured" and
9+
# built on a model nothing could load. Now: when mxbuild ran on the model BEFORE the write (the
10+
# baseline is known) and exits non-zero with no error list AFTER it, the write is rolled back.
11+
#
12+
# A normal success: the write is kept, "applied" row [guard]
13+
# B baseline ok, then mxbuild crashes (exit 137, no errors file): model byte-identical to
14+
# before, one "rolled back" row, nothing left half-applied
15+
# C mxbuild crashes on the baseline too (baseline not measured): it may be the machine, so
16+
# the old behaviour stays, the write is kept and flagged [guard]
17+
# D a clean error list after the write (an ordinary gate failure): still the normal
18+
# gate-failed rollback, not the crash row [guard]
19+
#
20+
# Synthetic only: throwaway git repo, stub mxcli, stub mxbuild, stub java. Nothing real runs.
21+
set -uo pipefail
22+
23+
TOOLKIT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
24+
SUBJECT="${1:-$TOOLKIT/project-bin/exec.sh}"
25+
case "$SUBJECT" in /*) ;; *) SUBJECT="$PWD/$SUBJECT" ;; esac
26+
WORK="$(cd "$(mktemp -d "${TMPDIR:-/tmp}/exec-crash.XXXXXX")" && pwd -P)" || exit 2
27+
trap 'rm -rf "$WORK"' EXIT
28+
PASS=0; FAIL=0
29+
ok() { PASS=$((PASS+1)); echo " ok — $1"; }
30+
bad() { FAIL=$((FAIL+1)); echo " FAIL — $1${2:+: $2}"; }
31+
echo "== subject: $SUBJECT"
32+
33+
P="$WORK/proj"
34+
mkdir -p "$P/bin" "$P/mprcontents" "$P/mdlsource" "$WORK/jdk/bin"
35+
printf 'not a real model\n' > "$P/Fixture.mpr"
36+
printf 'bson\n' > "$P/mprcontents/a.mxunit"
37+
printf 'CREATE MODULE "Nope";\n' > "$P/mdlsource/test.mdl"
38+
cp "$TOOLKIT/project-bin/_common.sh" "$TOOLKIT/project-bin/restore-mpr.sh" "$P/bin/"
39+
cp "$SUBJECT" "$P/bin/exec.sh"
40+
cat > "$P/bin/snapshot-mpr.sh" <<'SNAP'
41+
#!/usr/bin/env bash
42+
set -e
43+
cd "$(dirname "$0")/.."
44+
D=".mpr-snapshots/$(date +%Y%m%d-%H%M%S)-$$-$RANDOM"
45+
mkdir -p "$D"; cp Fixture.mpr "$D/"; cp -r mprcontents "$D/"
46+
echo "Snapshot saved: $D"
47+
SNAP
48+
# The write: `mxcli exec` appends to the model, as a real script would.
49+
cat > "$P/mxcli" <<'MXCLI'
50+
#!/usr/bin/env bash
51+
D="$(cd "$(dirname "$0")" && pwd)"
52+
case "${1:-}" in
53+
check) exit 0 ;;
54+
exec) echo "written by the script" >> "$D/Fixture.mpr"; echo "x" >> "$D/mprcontents/a.mxunit"; echo "fake mxcli exec"; exit 0 ;;
55+
esac
56+
exit 0
57+
MXCLI
58+
chmod +x "$P/bin/"*.sh "$P/mxcli"
59+
# mxbuild: MXB_MODE per call number, comma list: ok | crash (exit 137, no errors file) | err (a CE0001 error list)
60+
cat > "$WORK/mxbuild" <<'MXB'
61+
#!/usr/bin/env bash
62+
n=$(( $(cat "$MXB_N" 2>/dev/null || echo 0) + 1 )); echo "$n" > "$MXB_N"
63+
mode="$(printf '%s\n' "$MXB_MODES" | tr ',' '\n' | sed -n "${n}p")"; mode="${mode:-ok}"
64+
err=""; for a in "$@"; do case "$a" in --write-errors=*) err="${a#--write-errors=}" ;; esac; done
65+
case "$mode" in
66+
ok) [ -n "$err" ] && printf '{"problems":[]}' > "$err"; exit 0 ;;
67+
crash) echo "Unhandled exception: loader died" >&2; exit 137 ;;
68+
err) [ -n "$err" ] && printf '{"problems":[{"severity":"Error","code":"CE0001","message":"boom","element":{"document":"M.X"}}]}' > "$err"; exit 1 ;;
69+
esac
70+
MXB
71+
printf '#!/usr/bin/env bash\nexit 0\n' > "$WORK/jdk/bin/java"
72+
chmod +x "$WORK/mxbuild" "$WORK/jdk/bin/java"
73+
( cd "$P" && git init -q . && printf '.mpr-snapshots/\ndocs/\n.claude/\n' > .gitignore && git add -A \
74+
&& git -c user.email=t@t -c user.name=t commit -qm fixture ) >/dev/null 2>&1
75+
76+
LOG="$P/docs/BUILD-LOG.md"
77+
sum() { cat "$P/Fixture.mpr" "$P/mprcontents/a.mxunit" | cksum; }
78+
BEFORE="$(sum)" # the committed model; run() resets to it
79+
# run <label> <modes> — resets the model, runs the default path, prints the exit code
80+
run() {
81+
( cd "$P" && git checkout -q -- Fixture.mpr mprcontents && rm -rf .mpr-snapshots docs .claude ) >/dev/null 2>&1
82+
rm -f "$WORK/n"; mkdir -p "$WORK/tmp"
83+
( cd "$P" && TMPDIR="$WORK/tmp" SP_RESTART=0 MXTK_NO_INSTALL=1 MXB_N="$WORK/n" MXB_MODES="$2" \
84+
MXBUILD_PATH="$WORK/mxbuild" JAVA_HOME="$WORK/jdk" ./bin/exec.sh mdlsource/test.mdl ) >"$WORK/out.$1" 2>&1
85+
echo $?
86+
}
87+
lastrow() { tail -1 "$LOG" 2>/dev/null; }
88+
kept() { grep -q 'written by the script' "$P/Fixture.mpr"; }
89+
90+
echo "== A: baseline ok, gate ok: kept, applied row [guard] =="
91+
RC=$(run A ok,ok)
92+
[ "$RC" -eq 0 ] && kept && ok "exit 0, write kept" || bad "A rc=$RC" "$(tail -8 "$WORK/out.A")"
93+
lastrow | grep -q 'applied' && ok "applied row" || bad "A row" "$(lastrow)"
94+
grep -q 'rolling the write back' "$WORK/out.A" && bad "A: rollback message on a clean run" || ok "no rollback message"
95+
96+
echo "== B: baseline ok, mxbuild crashes after the write: rolled back =="
97+
RC=$(run B ok,crash)
98+
kept && bad "B: the write was KEPT after a crash" || ok "write rolled back"
99+
[ "$(sum)" = "$BEFORE" ] && ok "model identical to before the exec" || bad "B: model differs from before"
100+
grep -q 'rolling the write back' "$WORK/out.B" && ok "says it is rolling back" || bad "B: no message" "$(tail -10 "$WORK/out.B")"
101+
[ "$(grep -c 'gate could not run' "$LOG")" -eq 1 ] && lastrow | grep -q 'mxbuild exit 137 on the written model.*script rolled back' \
102+
&& ok "one BUILD-LOG row, says rolled back with the exit code" || bad "B: row" "$(cat "$LOG" 2>/dev/null | tail -3)"
103+
104+
echo "== C: baseline crashes too (not measured): old behaviour, kept and flagged [guard] =="
105+
RC=$(run C crash,crash)
106+
kept && ok "write kept (machine may be the cause)" || bad "C: rolled back without a measured baseline"
107+
grep -q 'Snapshot preserved' "$WORK/out.C" && ok "old message" || bad "C: message" "$(tail -8 "$WORK/out.C")"
108+
lastrow | grep -q 'gate could not run | mxbuild exit 137 ·' && ok "old row, no rollback claim" || bad "C: row" "$(lastrow)"
109+
110+
echo "== D: a clean error list after the write: the ordinary gate-failed path [guard] =="
111+
RC=$(run D ok,err)
112+
kept && bad "D: a new error was KEPT" || ok "rolled back by the normal gate"
113+
lastrow | grep -q 'gate failed' && ok "gate failed row, not the crash row" || bad "D: row" "$(lastrow)"
114+
grep -q 'rolling the write back' "$WORK/out.D" && bad "D: took the crash branch" || ok "crash branch not taken"
115+
116+
echo ""
117+
echo "PASS: $PASS FAIL: $FAIL"
118+
[ "$FAIL" -eq 0 ]

0 commit comments

Comments
 (0)