Skip to content

Ownership: generator frames own what they hold; close-out leak fixes (ABI v17) - #104

Merged
blacktrs merged 3 commits into
mainfrom
ownership-gen
Oct 6, 2026
Merged

blacktrs merged 3 commits into
mainfrom
ownership-gen

Conversation

@blacktrs

@blacktrs blacktrs commented Oct 6, 2026

Copy link
Copy Markdown
Contributor

What

Closes the ownership redesign (plan Tasks 10–11, follow-up to #15): a generator frame owns everything it holds, and the leaks the close-out soak surfaced are fixed. Memory ABI goes to v17.

Generators (Task 10)

  • A frame owns its params (the creator takes their +1), locals, key, sent value and return value, and releases them however it ends: destroyed before the first resume, destroyed while suspended, finished, or left by an exception.
  • Destroying a suspended generator runs the finally blocks around the yield first, then drops its locals — php's order.
  • An exception leaving the body marks the generator finished; current() / key() answer null afterwards.
  • send() no longer leaks and the slot is cleared once read; a yielded key object is released; the loop's key variable owns its key.
  • next() on a finished generator no longer restarts the body.

How: the frame is still freed through the string release; __mir_str_reclaim now calls __mir_gen_destroy, which re-enters the resume function at -2 - state. Each yield carries the exit of a generator abandoned there, with the drop set OwnershipFlow computes for an unwind from that point. Resume functions get the same cleanup pads as any function.

Close-out (Task 11)

  • A loop variable bound by two iterator loops at different classes no longer keeps a reference per iteration.
  • Async: a task's arena (chunks + mark stack) is freed with its fiber context. Every task that reached an arena scope kept ~5 KB.
  • The generator getIterator() hands a foreach over an IteratorAggregate is released when the loop ends, and an exception out of a loop's own iterator step drops the frame's locals and that iterator.
  • A string a cast mints as a compare operand against a cell is freed by every operator — array_key_exists() with a string key kept one buffer per call.
  • A closure aliased into a local of a monomorphized callable callee (Closure::fromCallable()) gives its reference back.
  • stream_socket_get_name() declares its string|false return.

Evidence

  • macOS arm64 at ef9e3ac4: AOT suite 1671 passed / 0 failed; gen2 == gen3 IR identical.
  • 14 new cases; each one written for a fix was checked red with main's compiler. Five known-bug repros promoted.
  • WebSocket soak, 5000 connect/echo/close: 368 KB RSS growth, no per-connection block under leaks --atExit (main: 7.4 MB at 3000).
  • Self-build: 138.7 s, 4.21 GB max RSS.

Not run: tools/difftest.sh, the Linux gate, bench/. This touches the runtime, the fiber context and the ABI, so the Linux gate is required before merge.

Known gaps (in docs/ROADMAP.md)

Fixes #22
Fixes #23
Fixes #43
Fixes #44
Fixes #103

…turn value are released on destroy, finish and unwind

Destroying a frame that is not finished re-enters its resume function at the suspended yield, runs the enclosing finally blocks and drops the locals OwnershipFlow has Own there; an exception leaving the body marks the generator finished and drops through cleanup pads. ABI v17.

Fixes #22
Fixes #43
Fixes #44
…sk arenas, aggregate generator iterators, minted compare strings and closure aliases are released

A foreach over a generator or an iterator binds its value at the loop's own class, so one variable rebound by two such loops no longer keeps a reference per iteration. Found by the close-out soak: a fiber context frees the arena it saved; the generator getIterator() hands an IteratorAggregate foreach is released at loop end; a string a cast mints as a compare operand against a cell is freed by every operator (array_key_exists with a string key); a closure aliased into a local of a monomorphized callable callee is owned. Remaining leak repros pinned as tests.

Fixes #23
…nd the iterator the loop holds

The iterator getIterator() hands a foreach lives in a hidden local OwnershipFlow owns, and the loop's own protocol calls (a generator resume, rewind / valid / current / key / next) carry the locals Own at the loop head, so an unwind through them runs the same cleanup as any call's.

Fixes #103
@blacktrs
blacktrs merged commit 2133738 into main Oct 6, 2026
7 checks passed
@blacktrs
blacktrs deleted the ownership-gen branch October 7, 2026 08:33
blacktrs added a commit that referenced this pull request Oct 7, 2026
Three shipped designs had no design note: per-program-point ownership
(OwnershipFlow, #15/#104), the native fixed-width buffer runtime behind
SplFixedArray and Manticore\Ds (#108), and the pinned-release bootstrap
(#112/#114). Each is derived from the code it cites; AGENTS.md links the
stage0 note.
blacktrs added a commit that referenced this pull request Oct 7, 2026
…114)

* Remove the Zend cold seed; docs follow the pinned-release bootstrap

The compiler bootstraps from BOOTSTRAP_VERSION (#112), so the seed has no
consumer left. Removed: bin/compile, tools/compile_files_mir.php, bin/build
--seed/--auto-seed, gate.sh MC_ZEND_SEED, install.sh's Zend fallback, and the
dead tools composer_data_smoke.sh, typecheck_scan.sh, split_ll.php,
docker/iopoll/diag.sh, docker/symfony_probe.sh. selfhost_stability.sh rebuilds
through the manifest path instead of Zend; composer.json drops the php
requirement (the installer downloads the release or builds from the pin).

Docs: every guide, README and design note audited against the tree — seed
instructions removed or marked historical, ABI references point at
MemoryAbi::VERSION (18), memory-abi.md gains the 64-byte descriptor (json_fn),
the v14 BIGINT box and the v18 native buffer block, pass tables gain
NarrowScalarGuards, ResolveMethodFcc, ResolveOverloads, SpillFreshBases and
OwnershipFlow.

* docs/design: ownership flow, native buffers and stage0 bootstrap notes

Three shipped designs had no design note: per-program-point ownership
(OwnershipFlow, #15/#104), the native fixed-width buffer runtime behind
SplFixedArray and Manticore\Ds (#108), and the pinned-release bootstrap
(#112/#114). Each is derived from the code it cites; AGENTS.md links the
stage0 note.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment