# LMDB after reader death: cleanup and page reuse

A controlled C-API demonstration and source synthesis for the existing
[LMDB page-reuse analysis](https://hesela.com/analyses/lmdb-readers-page-reuse-map-full/).
Canonical evidence bundle for the article's reader-death section.

## Result

With the parent environment kept open, killing its reader did not by itself
restore writes in this LMDB 0.9.33 fixture. A fresh write still returned
MDB_MAP_FULL. Explicit mdb_reader_check removed the abandoned slots, and writes
resumed without resizing or shrinking the database.

The same cleanup succeeded while the killed child remained unreaped and
kill(pid, 0) still succeeded. That is compatible with the implementation's POSIX
lock-based test; it is not a reason to treat arbitrary existing PIDs as dead.

The 40 cases are five lifecycles x two map sizes x one or three reader handles
x two repetitions. They are constructed schedules, not 40 independent field
incidents. No frequency, confidence interval or failure probability is inferred.

## Reproduce

Obtain upstream LMDB commit 3a29a24777c82a0165de813ae696a5068b5add30, including
libraries/liblmdb/LICENSE. Keep the files in this research directory together.

```sh
python3 -B run.py --lmdb /path/to/lmdb/libraries/liblmdb --output fresh.json
python3 -B validate.py fresh.json
LMDB_SOURCE=/path/to/lmdb/libraries/liblmdb python3 -B -m unittest -v test_research.py
LMDB_SOURCE=/path/to/lmdb/libraries/liblmdb python3 -O -B -m unittest -q test_research.py
python3 -B summarize.py
```

Requires Linux with waitid/WNOWAIT, Python 3.9+, a C compiler, pthreads and the
pinned source. No pip packages. The recorded geometry and evidence validator
require 4 KiB pages. The runner refuses to overwrite an existing output file.
It verifies SHA-256 of all four compiler inputs and the upstream license before
compilation; there is no network access during a run.

All database directories are newly created under private temporary directories.
Only spawned reader children receive SIGKILL. No existing database path is
accepted by the public command. The internal --worker entry point is only for
the runner. Every child is reaped and every environment is closed on normal
completion; failures have bounded handshakes and a child alarm.

## Method

Each fresh environment has eight keys containing 16 KiB values, a 1 or 2 MiB
map, and a 16-slot reader-table limit. A separately executed worker opens one or
three MDB_NOTLS read transactions after seeding. The writer commits single-key
overwrites until the first MDB_MAP_FULL, aborting failed put transactions.
The worker verifies all original bytes through every old snapshot.

After that shared starting point, the worker either remains active, resets its
handles while staying alive, aborts and closes cleanly, is killed and reaped, or
is killed but deliberately left waitable. The last schedule uses waitid with
WEXITED|WNOWAIT, checks CLD_KILLED/SIGKILL and does not call Popen.poll or wait
before the cleanup observation. There are no scheduling sleeps.

The parent records the table, attempts a new write, performs reader_check,
records the table again, and attempts another write. Live controls are then
closed, and the unreaped child is collected. All cases run 64 final writes.
The final oracle checks eight keys, exact lengths and every payload byte against
the committed generation history, then closes and reopens the environment and
checks again. The retained JSON contains payload and final database hashes.

Raw process/thread IDs are normalized to parent/child roles; transaction IDs,
slot multiplicity and inactive markers are retained. Reader occupancy comes
from mdb_reader_list, not the me_numreaders high-water field. The runner calls
the native C API through ctypes; it does not use the py-lmdb binding.

## Scope and files

See synthesis.md for the result table and source-review.md for claim provenance.
results.json is the original 40-case receipt. summarize.py derives its review
table rather than keeping a second hand-entered numeric dataset.
validate.py binds the recorded runner hash to run.py and checks the matrix, state transitions, counts, deterministic byte
oracles and matching repetitions. test_research.py has 23 distinct tests,
including native replay and mutations. Native tests are skipped only when
LMDB_SOURCE is absent; the recorded verification set it explicitly.

The recorded host was Linux 6.8.0-146-generic, x86_64, glibc 2.39, Python 3.12.3
and GCC 13.3.0. stat -f reported the temporary filesystem family as ext2/ext3;
that string is not a verified exact ext-generation label. MDB_NOTLS was the
only environment flag; no relaxed synchronization or lock-bypass flags were used.

Death was induced only after the read handshakes, while the worker waited for a
command. This does not test death while holding the reader/writer mutex, writer
crashes, robust-mutex recovery, process-ID reuse, Windows, other LMDB releases,
a real storage outage, or power-loss durability. Reopening a file is not a
power-loss experiment. Exact write counts are fixture-specific.

Evidence checks establish internal consistency, not independent authenticity.
The database hash can be checked against a native replay; the JSON-only validator
cannot reconstruct an LMDB file from its hash. Source and payload manifests are
review aids, not signatures.

Original harness, synthetic observations and prose: CC0-1.0. Upstream source
is not redistributed; its OpenLDAP Public License remains separate.
