The Generator That Vanished — Python Bug Hunt
Inspired by a bug class unique to Python that has shipped wrong reports at real companies: a generator can be consumed exactly once.
- Language: Python
- Layer: Backend
- Difficulty: Medium
- Concepts: Iterators, Python Gotchas
- Modelled on: Python generators
- Visible tests: a generator is summarised correctly; plain lists still work
- Reward: 50 XP for a complete fix
Incident
- Severity: SEV-2 (priority: High)
- Ticket: BUG-GENEXH
- Reported by: analytics QA
- Time to fix: 20 minutes, the target for a medium incident
Failing checks on the shipped build
- a generator is summarised correctly — the second pass must see the data too: expected 60 but got 0
How to work this incident
- Read the report: write down the expected behaviour and the actual one in a sentence each.
- Reproduce it: run the visible tests and find the failing check that matches the report.
- Read the logs and the failing checks for the first value that is wrong — the earliest wrong value is nearer the cause than the loudest error.
- Follow the request through the handler, the validation and the service it calls; find the first step whose output disagrees with the report.
- Fix the cause rather than the symptom, run the visible tests again, and submit against the hidden ones — then write the root cause down.
Briefing
Inspired by a bug class unique to Python that has shipped wrong reports at real companies: a generator can be consumed exactly once. Count its rows, then sum them — and the sum sees an empty sequence. Totals: zero. Nobody notices until finance does.
The row source (locked) returns a generator. report.py iterates it twice.
Bug report
BUG-GENEXH · Priority: High · Reported by: analytics QA
summarize(rows) -> {"count": n, "total": sum of amt}:
- rows may be ANY iterable, including a one-shot generator
- both numbers must reflect the same data
Observed: count is right, total is 0 — the second pass over the generator finds it already exhausted.
Logs
[report] rows=3 revenue_total=0The code as shipped
src/data/report.py (editable)
# Summarises a row stream.
def summarize(rows):
count = sum(1 for _ in rows)
total = sum(r["amt"] for r in rows)
return {"count": count, "total": total}
Read-only context: src/data/source.py.
Open the hunt to start the incident clock, edit the files, run the visible tests and submit against the hidden ones, then write the root cause down. More Python bug hunts.