The Generator That Vanished

Medium

· Backend

Incident

SEV-2

BUG-GENEXH

Priority: High · reported by analytics QA

Time to fix: 20 min

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

  1. Read the report: write down the expected behaviour and the actual one in a sentence each.
  2. Reproduce it: run the visible tests and find the failing check that matches the report.
  3. 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.
  4. Follow the request through the handler, the validation and the service it calls; find the first step whose output disagrees with the report.
  5. Fix the cause rather than the symptom, run the visible tests again, and submit against the hidden ones — then write the root cause down.

Inspired by

Python generators

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.

Code map

# 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}

Sign in to edit the file, run the tests and submit a fix

A free account — no card, no trial clock. You come straight back here.

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

  1. Read the report: write down the expected behaviour and the actual one in a sentence each.
  2. Reproduce it: run the visible tests and find the failing check that matches the report.
  3. 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.
  4. Follow the request through the handler, the validation and the service it calls; find the first step whose output disagrees with the report.
  5. 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=0

The 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.