Topic 2 of 5 · 4 min read
Isolating faults by halving the search
Goal: Narrow a bug to one component with halving and controlled comparisons in a small, planned number of steps.
Find the faulty part by cutting the search space in half at each step and changing one thing at a time. Halving finds one culprit among 1,000 candidates in about 10 checks. Guessing and poking can take all day.
Check yourself · 1 of 3
About how many checks does it take to find one bad commit among 64 with git bisect?
Why it matters
Most debugging time goes to looking in the wrong place. A bug could be in your code, a dependency, config, data, the environment or timing. Halving gives you a procedure that does not depend on a good hunch, which also makes it a good way to check an AI assistant's hunch.
How to do it
- Define good and bad. You need a fast check with a yes or no answer: the repro from the last topic. If the check is slow or flaky, fix that first.
- Find a known-good point. An older commit, an older config, a smaller input, the staging environment where it works.
- Halve. Pick the midpoint between good and bad, run the check, and keep the half that contains the change from good to bad.
- Repeat until one candidate is left.
Halving works along several axes:
- History.
git bisectautomates it over commits. With a script, it runs unattended. - Code. Comment out or stub half of a pipeline, then half of what remains.
- Data. Cut the input in half. Which half still fails?
- Config. Apply half of the differences between the working and broken config.
- Environment. Diff the two: versions, env vars, OS, region. Change one item at a time and re-run.
- Time. If it worked yesterday, what changed since: deploy, dependency update, certificate, data growth, clock?
Keep a short log: what you changed, what you saw. It stops you repeating a test and lets you hand the case over.
Before and after
Weak: "Production is slow, let's add an index, then upgrade the driver, then clear the cache." Three changes at once, and no way to know which one mattered.
Better:
git bisect start
git bisect bad HEAD
git bisect good v2.4.0
git bisect run ./scripts/check-latency.sh
With 64 commits between the tags, bisect needs about 6 runs and names the commit that made the check fail.
What goes wrong
- Changing two things at once. If the bug disappears you do not know why, and it may return.
- A midpoint that does not run. A broken build in the middle of history gives a false "bad". Use
git bisect skipfor those. - Trusting a flaky check. One wrong answer sends the search into the wrong half. Run flaky checks several times per step.
- Stopping at the commit. The commit is where the cause entered, not always why it fails.
Examples
Staging works, production fails
List the differences: runtime version, 14 env vars, one feature flag, database size. Apply half of the env vars from production to staging. It still passes, so apply the other half: it fails. Halve that group again. Four rounds point to TZ=UTC missing in staging. One environment difference, found in four checks.
Halving the input
A parser crashes on a 5,000-line file. The first 2,500 lines parse fine; the last 2,500 crash. Keep halving. At line 3,117 a line holds an unescaped quote. The repro is now that single line.
Key points
- Halving finds one culprit among N candidates in about log2(N) checks.
- You need a fast, reliable good-or-bad check and a known-good starting point.
- Halve along history, code, data, config, environment and time.
- Change one thing at a time, and keep a short log of changes and observations.
- Skip unbuildable midpoints and repeat flaky checks, or the search goes into the wrong half.
Common mistakes
- Making several changes between runs, so the result cannot be attributed.
- Running bisect with a check that is slow or sometimes wrong.
- Bisecting without a verified good point, so the 'good' end is also bad.
- Not writing down what was tried and repeating the same experiments.
Check yourself · 2 of 3
Staging works and production fails. What is the best next step?
Check yourself · 3 of 3
A bisect run lands on a commit that does not even build. What should you do?
Practice items, not counted towards your mastery.
Start here
- Debugging and Profiling (MIT Missing Semester) (opens in a new tab)CourseFree
Print debugging, logging, debuggers and reading traces, taught with short exercises.
- A debugging manifesto (Julia Evans) (opens in a new tab)ArticleFree
A short list of habits: reproduce first, simplify, and test your assumptions.
- The Debugging Book (opens in a new tab)BookFree
Free book on tracing, reducing failing inputs and finding root causes automatically.
More for this topic
- git-bisect documentation (opens in a new tab)DocsFree
How to binary-search commit history for the change that introduced a bug.