Debug systematically
Core loop
- Get the exact failure. Full error text, stack trace, input, environment (versions, OS, config, feature flags). Ask for them if missing. Read the whole stack trace: the first frame in your code is usually where to look, the last frame is where it exploded.
- Reproduce it. Smallest command that shows the bug. No reproduction means you are fixing a guess.
- Isolate. Shrink input, remove steps, comment out code paths until the bug disappears. The last removal is involved.
- Hypothesize, then test one hypothesis at a time with the cheapest observation (a print, a debugger breakpoint, a one-line test). Write down what each test ruled out.
- Fix the root cause, not the symptom, with a test that failed before and passes after.
- Look for siblings. Search for the same pattern elsewhere; bugs come in families.
Find when it broke
git bisect start
git bisect bad # current commit is broken
git bisect good v2.3.0 # last known good
git bisect run ./repro.sh # script exits 0 = good, 1-127 (except 125) = bad, 125 = skip
git bisect reset
git log -S 'functionName' --oneline finds commits that added or removed a string. git log -p -- path/to/file shows the file’s history.
Playbooks by symptom
Exception / crash
- Check the inputs at the failing frame (log or breakpoint). Null/undefined, wrong type, empty collection and off-by-one are the usual suspects.
- Check recent changes to the data source, not just the code.
Wrong output, no error
- Find the first place where the data is wrong: binary-search the pipeline by logging intermediate values.
- Suspect: implicit type coercion, float math on money, time zones and DST, mutated shared objects, caching of stale values, integer division.
Flaky test
- Measure it:
python3 scripts/flake_check.py --runs 20 -- <test command>reports pass rate and distinct failure messages. - Usual causes: test order dependence (shared state, globals, DB not reset), real time and dates, random data without a seed, async operations not awaited, network calls, parallel tests sharing ports or files.
- Run the test alone and in the full suite; if it only fails in the suite, look for state leaking from earlier tests.
Slow
- Measure before changing anything (see
performance-investigation). Do not guess.
Works locally, fails in CI / production
- Diff the environments: runtime and dependency versions (lockfile honored?), env vars, OS and file-system case sensitivity, time zone and locale, CPU count, network access, file permissions, data volume.
- Reproduce the CI environment locally (same container image) before changing code.
Heisenbug (disappears when observed)
- Timing or memory related: race conditions, uninitialized memory, optimizer differences. Use logging with timestamps rather than breakpoints; look for shared mutable state across threads/async tasks.
Rules
- Do not change two things at once.
- Do not “fix” by adding retries, sleeps, broad try/except or loosened assertions unless you can explain why that is the correct behavior.
- Remove temporary debug output before finishing.
- If you cannot find the cause, report what you know, what you ruled out and the next experiment. That is progress.