0061. Release reader buffers after their last scoped view
- Status: Accepted
- Date: 2026-10-31
- Plan: ADR-0061 follow-up (M6.2)
- Review: Memory and representation
Context
ADR-0054 retains closed and abandoned reader buffers until exit. Opening, streaming and closing 10,000 distinct paths of 64 KiB peaks at 645,796 KiB. Close through a helper, another open, or a new head token can all run under a live view.
Decision
Supersedes in part ADR-0054:
its persistent-buffer/head-switch and never-free-on-close
retention rules. Read-path buffers are independently owned,
budgeted allocations with one reader reference and one lease per
exposed volatile view. ReadView already has its
iteration region in IR; cgen passes that arena to
hrt_read_view_scoped. Cleanup nodes drop leases
before reset/poison, respecting nested same-arena marks. The
cleanup list stays ordered by end because every reset
rewinds top. Replacement/close drops reader
ownership; the last reference frees the buffer. No per-line malloc
is needed. Whole reads keep ADR-0046's arena path.
Soundness: regions.rs forbids home raising for Volatile; every
retaining flow to an older home (including return, containers and
aliases of views) inserts deep Promote. Non-copied
aliases therefore die at their iteration reset. lower_platform.rs
emits close exactly at the method call (and after fs.read), not at
a lifetime boundary. cgen.rs already resets scopes at the next
loop head, break/continue exits and returns; those resets now
release leases too. Multiple heads/recursive frames hold
independent leases, so neither close nor a refill can free an
enclosing view. Leases in a scope that is never reset (an unreset
function root or HRT_NO_RESET) only retain buffers: a
leak, not unsoundness.
Close immediately recycles slots through a free list. Handles
encode a u32 index and a non-wrapping 31-bit generation; stale
reads still trap file closed and repeated close
cannot affect a replacement. Exhausted generations retire their
slot. Reader paths and replaced tables are freed; table capacity
follows peak concurrent readers. Owned bytes share
HRT_MEMORY_LIMIT, stats and poison. Both backends discard the
reader name on close; internal FileName then returns
empty bytes, whether the interpreter keeps the closed slot
(release: false) or removes its last private reader
slot (release: true).
Mappings retain ADR-0055's until-exit policy and static SIGBUS table unchanged, even after read() fallback: no munmap or mapping entry reuse is attempted. Legacy unscoped C access retains buffers until exit; generated code uses scoped access.
Alternatives considered
- Free at close/next open/token switch — each can run under a live view.
- Free slots without generations — old File aliases would access new files.
- Size-bucketed pool — still needs the same death proof and a bounded eviction policy; individually freed buffers already meet the measured bound.
- Copy every view — loses zero-copy same-iteration operations.
Consequences
CC=gcc python3 bench/reader-release/run.py checks
byte-identical output before timing 10,000 distinct hardlinked
paths, mapping disabled. Before/after: 645,796 / 1,460 KiB RSS;
0.330 / 0.037 s (Linux, gcc C11 -O2). The Linux gate enforces
<65,536 KiB; sanitizer tests pin leases and budget return.
Differential cases cover close/live aliases, promotion, reopen,
interleaving and stdin. Memory follows live readers/views (plus
≤64 mappings, retained values and retired slots).