Sub topic 13
Dealing with flaky failures
This chapter assumes you can write a reproducer script that follows the exit code contract (0 good, 1 to 127 bad, 125 skip, 128 or higher abort). It also assumes you have run git bisect run at least once.
Why flakiness breaks bisect
Binary search assumes each test answer is true. A flaky test is one that sometimes passes and sometimes fails on the same code. If it reports one wrong answer, Git discards half of the remaining commits on bad evidence. It then reports a first bad commit that has nothing to do with the bug. Git gives no warning when this happens.
The risk is not symmetric. Suppose a bug appears in 1 run out of 5 on the broken code.
- A failure is strong evidence. That commit is probably bad.
- A pass is weak evidence. The commit might be bad and just lucky.
A single false "good" answer is enough to send the search into the wrong half of the history.
Repeat the test inside the script
The usual fix is to run the test several times and call a commit good only if every run passes. A single failure marks it bad. The script keeps the exit code contract, so Git does not need to know anything about the repeats.
#!/bin/sh
# check_flaky.sh: exit 0 = good, 1 = bad, 125 = cannot test this commit
make >/dev/null 2>&1 || exit 125
runs=20
i=0
while [ "$i" -lt "$runs" ]; do
./run_regression_test.sh >/dev/null 2>&1 || exit 1
i=$((i + 1))
done
exit 0The || exit 1 is deliberate. If the test itself exits with 125 or 128 or higher, those codes would otherwise reach Git and skip or abort the session. Collapsing every test failure to 1 keeps the meaning clear.
Choosing the number of runs
Pick runs from the observed failure rate. If the bug shows up in about 1 run in 10, then 20 passes in a row on a bad commit has a chance of roughly 0.9 to the power 20, about 12%. That is still a lot of wrong answers across a search with many steps. Raise the count to 50 and the chance falls to about 0.5%.
Good commits cost the full runs executions, and bad commits usually stop early. So the cost grows with the repeat count. Measure the failure rate on a known bad commit first, then set the count.
Separate environment noise from the bug
Not every intermittent failure is the regression you are hunting. A network timeout or a full disk says nothing about the code. If you can detect these cases, report them as 125 so Git skips the commit instead of marking it bad.
Check the answer afterward
With flaky tests, treat the result as a suspect until you have confirmed it. Save the commit before resetting, then run the test many times on its parent. The parent should never fail.
BAD=$(git rev-parse refs/bisect/bad)
git bisect reset
git checkout "$BAD^"
fails=0
for i in $(seq 1 100); do
./run_regression_test.sh >/dev/null 2>&1 || fails=$((fails + 1))
done
echo "failures on parent: $fails of 100"If the parent fails even once, the answer was wrong. Raise the run count and bisect again. You can also use git bisect log to see which commits were marked good, then retest the ones nearest the answer.
Exercise
A bug fails in about 1 run in 4 on a bad commit. Your script runs the test once per commit and git bisect run blames a commit that looks unrelated. What went wrong, and what change to the script fixes it?
Show answer
A bad commit passes the single run about 75% of the time, so Git recorded false "good" answers and followed the wrong half of the history. Run the test several times in a loop, exit 1 on the first failure, and exit 0 only if every run passes. Choose enough runs (for example 20 or more) that all of them passing on a bad commit is unlikely. Then check the result by running the test many times on the parent of the reported commit.