Sub topic 13
Dealing with flaky failures
This chapter assumes you can already run git bisect run with a script that follows the exit code contract (0 good, 1 to 127 except 125 bad, 125 skip). It also assumes you know how to check a script against the good and bad endpoints. Here you will learn what happens when the test itself gives different answers on the same commit, and how to keep the search trustworthy.
Why one wrong answer ruins the search
Binary search assumes every verdict is correct. Each step discards half of the remaining commits based on a single answer. If one step says "good" for a commit that is really bad, Git throws away the half that contains the culprit. The search then finishes without complaint and names an innocent commit.
A flaky test is one that gives different results on the same code. Common causes are:
- Timing: race conditions, sleeps that are too short, slow CI disks.
- Randomness: unseeded random numbers, hash ordering, test data generated on the fly.
- Shared state: ports already in use, leftover temp files, parallel test runs.
- External services: network calls, package mirrors, license servers.
There are two different situations, and they need opposite fixes.
| Situation | What is flaky | Which verdict is unreliable | Fix |
|---|---|---|---|
| The bug itself is intermittent | The regression only fails some of the time | "Good" (a pass proves little) | Repeat the test, fail on the first failure |
| The environment is noisy | Unrelated failures appear on good commits | "Bad" (a failure may be noise) | Retry on failure, pass on the first success |
| The environment is unavailable | The test cannot run at all | Neither | Exit 125 so Git skips the commit |
Decide which row you are in before you write the script. The first question to ask is whether the failure you are hunting is reproducible on a single commit.
Case 1: the bug is intermittent
Suppose the regression makes a test fail about 20% of the time. On a bad commit, one run passes with probability 0.8. If your script runs the test once, it will wrongly say "good" for a bad commit 80% of the time.
A failure is strong evidence: the commit is bad. A pass is weak evidence. So the script should repeat the test and stop at the first failure. It should report "good" only after many passes in a row.
The chance of missing a bug that fails with probability p in n runs is (1 - p)^n. For p = 0.2:
| Runs (n) | Chance of a false "good" |
|---|---|
| 1 | 80% |
| 10 | about 10.7% |
| 20 | about 1.2% |
| 30 | about 0.12% |
Bisect takes about log2(N) steps, and every step is a chance to be misled. Over a 1000-commit range that is roughly 10 steps. Only the steps on bad commits can produce a false "good", but you do not know in advance which those are. With 30 runs per step, the risk across the whole search stays near 1%. You pay for it in time: 10 steps of 30 runs is 300 test runs.
Here is a script that does this:
#!/bin/sh
# flaky_check.sh: repeat the test; any failure means bad.
# exit 0 = good, 1 = bad, 125 = cannot test this commit
RUNS=${RUNS:-30}
make >/dev/null 2>&1 || exit 125
i=0
while [ "$i" -lt "$RUNS" ]; do
./run_regression_test.sh >/dev/null 2>&1
status=$?
case "$status" in
0) ;; # this run passed, keep going
3) exit 125 ;; # test reported "environment unavailable"
*) exit 1 ;; # real failure: this commit is bad
esac
i=$((i + 1))
done
exit 0The case block assumes your test uses exit code 3 for "the environment is broken". Adapt that number to whatever your project uses, or delete the line if there is no such code.
Because the script reads RUNS from the environment, you can change the count without editing the file:
RUNS=50 git bisect run ~/bisect/flaky_check.shKeep the script outside the repository, as in the earlier chapter on script location. Git rewrites the working tree at every step, and a script inside it can vanish or change between commits.
Choosing how many runs
Estimate the failure rate p first. On a commit you know is bad, run the test 100 times and count failures:
git checkout main
fails=0
for i in $(seq 1 100); do
./run_regression_test.sh >/dev/null 2>&1 || fails=$((fails + 1))
done
echo "failed $fails of 100"
git checkout -If you see 20 failures, p is about 0.2. Pick n so that (1 - p)^n is small, for example under 0.5%. A rule of thumb is n of about 5 divided by p, which gives a miss chance near 0.7%. For p = 0.2 that is 25 runs. If p is 0.01, you need about 500 runs per step, and you should consider whether you can make the failure more likely instead.
Making the failure more likely
Raising p is often cheaper than adding runs:
- Run the test under load, for example with several copies running in parallel.
- Reduce timeouts or add artificial delays that widen the race window.
- Use a fixed random seed that you know triggers the failure, if you have one.
- Run a single focused test instead of the whole suite.
Any change you make to the script must apply to every commit in the search. Check it against both endpoints again afterward.
Case 2: unrelated noise
Now suppose the regression is deterministic, but the test suite fails by chance on good commits too, for example because of a network timeout. A single failure no longer proves the commit is bad. Here the pass is the strong evidence, and the script should retry:
#!/bin/sh
# retry_check.sh: any pass means good; only repeated failure means bad.
ATTEMPTS=${ATTEMPTS:-3}
make >/dev/null 2>&1 || exit 125
i=0
while [ "$i" -lt "$ATTEMPTS" ]; do
./run_regression_test.sh >/dev/null 2>&1 && exit 0
i=$((i + 1))
done
exit 1This is safe only when the real regression fails every time. If the bug is also intermittent, this script will mark bad commits as good whenever one retry passes. Never combine the two approaches without thinking through which verdict each one protects.
Environment failures belong to 125
Some failures say nothing about the code. A missing dependency download, a full disk, or a dead test server will fail on every commit, and a script that exits 1 will mark everything bad. Send those to 125 so Git skips the commit.
Two details to watch:
- A
timeoutwrapper exits with 124 when the command runs too long. Since 124 is in the 1 to 127 range, Git reads it as "bad". If a hang is not the bug you are hunting, translate it:timeout 60 ./test.sh; [ $? -eq 124 ] && exit 125. - Exit codes of 128 and above abort the whole bisect. A script killed by a signal (for example, out-of-memory) produces such a code, and the run stops. That is usually what you want, because it makes a broken machine visible instead of silently misleading you.
Do not overuse 125. If most commits in the range are skipped, Git may be unable to narrow the answer and will report a set of candidate commits rather than one.
Checking the result
With flaky tests, never accept the first bad commit on faith. Verify it with more runs than the search used:
- Check out the reported commit and run the test 100 or more times. You should see failures at roughly the rate you measured.
- Check out its parent (
git checkout <commit>^) and run the same number of times. You should see zero failures. - Read the diff with
git show <commit>and ask whether it plausibly causes the symptom.
If the parent also fails, the true culprit is earlier, and a false "good" happened somewhere in the search.
Recovering from a bad verdict
You do not need to start over. Save the log, find the wrong line, and replay without it:
git bisect log > bisect.log
git bisect reset
# edit bisect.log: delete the "git bisect good <hash>" line you now distrust
git bisect replay bisect.logAfter the replay, Git picks the next commit to test given the remaining verdicts. Rerun git bisect run with a higher RUNS value.
You can also override a single step by hand. If the script ran and you doubt its answer, mark the commit yourself with git bisect good or git bisect bad after testing it more thoroughly.
Summary of decisions
- Intermittent bug: loop the test, exit 1 on the first failure, exit 0 only after N passes.
- Noisy environment on good commits: retry up to a few times, exit 0 on the first pass, exit 1 only if all attempts fail.
- Environment broken: exit 125 for that commit, or let a 128+ code stop the search.
- Always: estimate p, choose N so (1 - p)^N is tiny, and verify the final commit and its parent with many runs.
Exercise
A regression makes a test fail on about 10% of runs. Your script runs the test once per commit. Roughly how likely is it that a single step on a bad commit gives the wrong verdict, and how many runs per step would you need to bring that below 1%?
Show answer
With one run, the test passes 90% of the time, so a bad commit is wrongly called good about 90% of the time. You need (0.9)^n below 0.01. Since 0.9^43 is about 0.0108 and 0.9^44 is about 0.0097, n = 44 is the first value below 1%. In practice, use 45 to 50 runs and make the script exit 1 on the first failure.
Exercise
Your bisect script runs timeout 30 ./test.sh. Some commits hang because of an unrelated deadlock you do not care about. Without changes, what does Git do when timeout fires, and how should the script handle it?
Show answer
timeout exits with 124. That is in the 1 to 127 range (and is not 125), so Git marks the commit bad, which may misdirect the search. Capture the status and map it: timeout 30 ./test.sh; status=$?; [ "$status" -eq 124 ] && exit 125; exit "$status". Git then skips the hanging commits instead of treating them as bad.
Exercise
A flaky bisect reports commit abc123 as the first bad commit. What two checks should you run before trusting it, and what result would show the search went wrong?
Show answer
Run the test many times (100 or more) on abc123 and on its parent abc123^. The commit should fail at roughly the measured failure rate and the parent should never fail. If the parent also fails, the real culprit is earlier and the search got a false "good" verdict. Then save the log with git bisect log, remove the doubtful verdict, and replay with more runs per step.