BLACK LABELAcademy
← Our Failures

A failure recorder that re-entered itself wrote 249 rows per call and poisoned the green baseline for 28h

advanced6 min read · updated 2026-06-20

Market & numbers — every figure sourced

rows_written_per_call249 rowsest: Observed write amplification from the live incident: one logical failures.record() call produced 249 physical rows before the guard landed.
baseline_poisoned_duration28 hoursest: Measured wall-clock window during which the self-improvement loop's quality gate stayed red and merged nothing, from incident timeline.
amplification_factor249 xest: rows_written_per_call divided by the intended 1 row per call (249 / 1).

A failure recorder that re-entered itself wrote 249 rows per call and poisoned the green baseline for 28h

The cruelest bugs live inside the machinery you built to catch bugs. We had a `failures.record()` helper whose only job was to write one row when something went wrong. It wrote 249 rows per call instead, and because those rows fed the baseline that gated our self-improvement loop, the loop went red and merged nothing for 28 hours. Nothing else was broken. The detector ate the system.

What we tried

The design looked innocent. `failures.record(err)` appended one row to a table, then notified a few sinks so a human could see the failure (one of them mirrored the message to a chat channel). The intent was: one failure in, one row out, one ping out.

The trap is that the notify path could itself fail — a flaky network call to the chat mirror — and the obvious, well-meaning thing to do when notification fails is... record the failure. So `record()` called the mirror, the mirror raised, and the error handler called `record()` again. Same thread, same call still on the stack, before the first one finished. That is the textbook definition of a non-reentrant routine being re-entered: a function interrupted and resumed before it finishes, re-entering its own shared state (reentrancy).

What broke

It wasn't a clean infinite loop that crashed loudly. It was bounded enough to survive and amplify quietly:

This is the same failure family the standard tooling warns about. Python's `logging` is thread-safe but explicitly not reentrant inside a handler — calling logging APIs from within `emit()` can deadlock or recurse (not-reentrant). Loguru says it plainly: use the logger inside a sink and you "logically result in an infinite recursive loop," which is why it ships a reentrancy guard that raises rather than spins (reentrancy-guard). PostgreSQL hit the identical shape in memory-context logging and fixed it the identical way: a guard that detects "already in progress" and returns immediately (guard-pattern). When the Python core team debated unbounded recursion in logging handlers, the consensus mechanism was the same: a per-thread "we're already inside" flag (thread-flag).

The fix

A thread-local re-entrancy guard. Cheap, correct, and exactly what the references converge on.

```python

import threading

_in_record = threading.local()

def record(err):

Already recording on THIS thread? Don't recurse — drop the nested call.

if getattr(_in_record, "active", False):

return

_in_record.active = True

try:

_write_row(err) # the one row we actually want

_notify_sinks(err) # if a sink raises, the re-entry is a no-op

finally:

_in_record.active = False # finally is non-negotiable

```

Why each piece matters:

After the guard landed: one failure in, one row out. Baseline went green, the loop started compounding again, and the 28-hour gap closed.

Apply it

Sources

© 2026 Black Label · Education, not financial or legal advice. Every number is sourced or labeled an estimate. Subscribe for $30/month