I'm Eric Gitangu. I work on backend systems. Our merge check judges a branch the careful way: it creates a throwaway worktree, merges the branch into the target there, runs the gates against that result, and removes the tree on every exit.
Disposable workspaces are a good default. Nothing left over from yesterday can influence today's verdict. The catch is that "remove everything on exit" includes the thing you need when a gate breaks.
A crash is not a finding
One of the gates is a static analyzer that runs in a container. It can end three ways:
- It finishes and has nothing to report.
- It finishes and reports issues. The gate then rules on that report.
- It never writes a report.
The third case blocks the merge. It has to: a gate that treats "I could not look" as "I saw nothing" is decoration. The same rule applies to our test steps, where a run with no summary line counts as a failure whatever its exit code says.
Failing closed protects the branch. It does nothing for the person who now has to find out why.
What survived the crash
When the analyzer died, the merge check showed the last few lines of its output and then removed the throwaway tree, with the full log in it or in a temp file that was deleted on exit. This happened more than once.
The end of a crash log is often the least useful part: the bottom of a stack trace and a generic "crashed unexpectedly" sentence. The line that names the error was at the top.
An earlier change of mine had already tried to keep one of these logs, by copying it into the checkout's output folder. In a developer's commit hook that works, because the checkout stays. In the merge check, the checkout is the disposable tree. Saving a file inside a directory that is about to be deleted is not saving it.
Capture first, then clean up
The rule became: if an analyzer run ends with no report, copy its full output to the merge check's own log directory before anything is removed. That covers each analyzer run in that step, and the gate's log names the directory.
analyzer=${ANALYZER:?set ANALYZER to your analysis command}
(cd "$tree" && REPORT="$tree/report.txt" "$analyzer") >"$tree/run.log" 2>&1
rc=$?
if [[ ! -f "$tree/report.txt" ]]; then
# No report means no verdict. Keep everything BEFORE cleanup runs.
cp "$tree/run.log" "$logdir/crash.log" ||
echo "gate: could not save the crash log" >&2
echo "gate: BROKEN (exit $rc), full log in $logdir" >&2
exit 2
fiThe details that matter:
- The destination lives outside the disposable tree. The caller owns it, because the caller knows what will be deleted.
- Keep the whole output. A tail is a display choice. It should not be a retention policy.
- Saving the log never changes the verdict. If the copy fails, the gate is still blocked.
- Test it with a fake crash. Our test swaps the analyzer for a stub that prints a marker on the first line, many lines after it, and no report. It asserts that the marker survives. No container is needed, so the test is cheap to run.
Then read the first line
With the full output kept, the cause was readable: opening a file in the analyzer's cache failed with "Too many open files in system". That error means a system-wide open-file limit was hit. Several merge lanes were running in parallel, our best explanation.
That raised a better question than how to retry. Why was this run writing a cache at all?
The merge check judges a fresh tree. There is no earlier cache in it to reuse, and whatever it writes is deleted minutes later. The cache cost one file write per scanned file and could return nothing. So the merge check now runs the analyzer cold. On ordinary commits, the developer's hook still caches its run against the persistent checkout, where a later run can use it.
# In the merge check only: a fresh tree has nothing to reuse.
export USE_CACHE=0This has a limit worth stating. Turning the cache off removes the writes that failed. It does not raise the open-file limit. If the machine is still oversubscribed, something else can fail next. The difference is that the next failure should arrive with its full log attached.
Takeaways
- Give "the gate broke" its own outcome, separate from "the gate found something". Block on both.
- Before a cleanup trap deletes a workspace, ask what a person would need if the run had just crashed.
- Put retained logs somewhere the cleanup cannot reach, and print the path.
- Check every cache against the lifetime of the directory it lives in. A cache in a one-shot workspace is only a cost.
- Test the failure path with a stub. Real crashes do not arrive on request.
A runnable generic version, with a self-test for pass, findings, crash and a silent failure, is in this gist.
Also on dev.to.