Skip to content

feat(ipc): recycle settled result slots through a generation-tagged free list - #25

Merged
lisachenko merged 2 commits into
mainfrom
claude/result-slot-recycling
Aug 16, 2026
Merged

lisachenko merged 2 commits into
mainfrom
claude/result-slot-recycling

Conversation

@lisachenko

Copy link
Copy Markdown
Owner

Substrate half of lisachenko/native-php-coroutines#16 — "Result slots are bounded and never reclaimed, so a long-running pool exhausts them". Option 1 from the issue. The consumer half is the companion PR on native-php-coroutines, which needs this on main first.

The problem

Slots were bump-allocated and never given back, so a pool spawning ten tasks a second exhausted the default 4096 in under seven minutes — however few were ever in flight at once.

Recycling, without ever freeing

The arena is bump-only and children may never free (AGENTS.md §2, §6), so a released slot is reused in place. The free list is a LIFO stack threaded through the slot records themselves — one header word for the head, one word per slot for the link — so the mechanism allocates nothing and every mutation of it is a word write under the mutex the slots already share.

A slot id is a ticket now

SlotTicket packs the slot index and its generation into 32 bits (16 each) — exactly what a WakeEvent's uint32 id field carries, so nothing on the wire changed shape, and a settling worker is told which generation it is answering for rather than merely which slot. Every verb (readSlot, await, complete, completePanic, releaseSlot) re-reads the slot's generation under the lock and refuses a mismatch with the slot and both generations named. A stale handle fails loudly instead of being handed the next task's result. Generation 0 never exists, so a bare index passed where a ticket belongs is refused too. Consequence, stated as a limit: a table holds at most 65536 slots, and create() refuses more rather than truncating ids.

Two choices worth reviewing:

  • the generation moves at release, not at reuse — the moment an owner gives up its claim is the moment the claim must stop being answerable, so a slot resting on the free list does not keep answering the handle that put it there (ResultState::Free makes the free-list state self-describing — two independent guards);
  • generations never wrap — a slot whose 16-bit counter is used up is retired (stamped generation 0, left off the free list) rather than started over, so no handle can be revived by a counter coming back round. ABA is eliminated by construction rather than by probability, at the cost of one slot after 65 535 uses of it.

Refusals

Releasing a pending slot is refused: recycling one while the process that owes its answer may still write it is the only way this design could hand a live record to another task. Release wipes the waiter table, and unpark() re-checks the generation under the same lock, so a waiter left parked across a recycle never writes into the new occupant's table — it fails its own generation check instead. Double release is refused by generation. Exhaustion is still typed, and now reports outstanding and retired counts alongside the knob.

LAYOUT_VERSION is deliberately not bumped

ResultSlotTable is a consumer structure published in the roots directory and adds no field to a registry record, which AGENTS.md §7 excludes from the version. The slot geometry did change, so the table carries its own guard: a FORMAT header word checked at attach. AGENTS.md §7 now says such structures must carry one. (The guard protects future readers, not past ones — an old build has no format word to check; what it buys is that the next shape change is loud.)

Merged with main

Rebuilt on #24. ResultSlotTable::create() keeps the prefault() call and computes its $size from this branch's larger geometry (8 header words, 12 per slot), so the whole table is made resident at creation; both of #24's ArenaTest prefault cases pass unchanged against it. AGENTS.md §6 carries both bullets, and the recycling one says why one does not replace the other: a steady-state pool reuses a handful of records and would look page-flat by accident, while a workload whose concurrency really grows still reaches records nothing has touched.

Breaking

allocateSlot() returns a ticket rather than an index, and every verb takes one. Allowed on this dev line; no external consumers.

Verification

  • Suite on PHP 8.5: 182 tests, 15 565 assertions, OK (also green under --order-by=random and MALLOC_CHECK_=3 MALLOC_PERTURB_=85).
  • tools/soak.php 5000: SOAK OK (final delta −2 488 bytes, allowed 65 536).
  • tools/soak-drop.php 5000: SOAK-DROP OK (3.62 kB/cycle, budget 6).
  • New tests/Ipc/ResultSlotRecyclingForkTest.php: recycling round trip, every verb refusing a stale handle, bare index refused, pending release refused, double release refused, exhaustion when genuinely held, generation retirement over 65 535 real cycles, a fork case where a slot recycled by the parent is settled by a child while the child's stale ticket is refused, and an argument slot released by its reader.
  • Not run on 8.4 locally: this checkout's vendor/ resolves php >= 8.5. CI covers both minors.

🤖 Generated with Claude Code

https://claude.ai/code/session_01U5QRWPfmkZME7hDjU6i5EA


Generated by Claude Code

claude added 2 commits August 16, 2026 09:09
…ree list

Slots were bump-allocated and never given back, so a pool spawning ten tasks a
second exhausted the default supply of 4096 in under seven minutes regardless of
how few were ever in flight at once. This makes the supply circulate.

The arena is bump-only and children may never free (AGENTS.md §2, §6), so
recycling cannot mean releasing a block: a released slot is reused **in place**.
The free list is a LIFO stack threaded through the slot records themselves - one
header word for the head, one word per slot for the link - so the whole mechanism
allocates nothing, ever, and every mutation of it is a word write under the table
mutex the slots already share.

What makes reuse safe is that a slot id stops being an index. It is now a
`SlotTicket`: the slot index and its generation packed into 32 bits (16 each),
which is exactly what a `WakeEvent`'s uint32 id field can carry, so a settling
worker is told which *generation* it is answering for rather than merely which
slot. Every verb - readSlot(), await(), complete(), completePanic(),
releaseSlot() - re-reads the slot's generation under the lock and refuses a
mismatch with `IpcException` naming the slot and both generations. A stale handle
therefore fails loudly instead of being handed the next task's result, which is
the one failure mode recycling could otherwise introduce.

Two deliberate choices worth stating:

- **the generation moves at release, not at reuse.** The moment an owner gives up
  its claim is the moment that claim must stop being answerable; a slot resting on
  the free list must not keep answering the handle that put it there.
- **generations never wrap.** A slot whose 16-bit counter is used up is *retired*
  (stamped generation 0, left off the free list) rather than started over, so no
  handle can be revived by a counter coming back round. That costs one slot out of
  the supply after 65 535 uses of it, which is the ordinary leak-until-teardown
  bargain of this arena.

Releasing a pending slot is refused: recycling one while the process that owes its
answer may still write it is the only way this design could hand a live record to
another task. Release wipes the slot's waiter table too, and unparking re-checks
the generation under the same lock, so a waiter left parked on a recycled slot
never writes into the new occupant's table - it fails its own generation check
instead.

Spawn-argument slots (the one-shot parent->worker direction) ride the same API and
are releasable by their reader.

LAYOUT_VERSION is deliberately NOT bumped: `ResultSlotTable` is a consumer
structure published in the roots directory and adds no field to a registry record,
which AGENTS.md §7 excludes from the version. Because the slot geometry did change,
the table carries its own guard instead - a `FORMAT` header word checked at
attach(), so a reader of the old shape is refused rather than reading the right
addresses with the wrong meanings.

Suite on PHP 8.5: 180 tests, 15559 assertions, OK; both soaks pass
(tools/soak.php 5000, tools/soak-drop.php 5000).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01U5QRWPfmkZME7hDjU6i5EA
Brings in #24's prefault fix, which lands in the same two places this branch
rewrites.

`ResultSlotTable::create()` merged cleanly and correctly: the prefault call is
kept and its `$size` is computed from THIS branch's geometry, so the whole of the
larger table (8 header words, 12 words per slot, up from 4 and 8) is made resident
at creation rather than only the part main's arithmetic would have covered. The
inline comment's "one per eight slots" no longer matched either geometry and is
now stated without the arithmetic.

AGENTS.md §6 conflicted - both sides append a bullet after the shared-string rule.
Both are kept, prefault first, and the recycling bullet now says why one does not
replace the other: a steady-state pool reuses a handful of records and would look
page-flat by accident, while a workload whose concurrency really grows still
reaches records nothing has touched. The prefault is what makes that second case
flat too.

Suite on PHP 8.5: 182 tests, 15565 assertions, OK - including both of #24's
prefault tests against the new geometry, and green under --order-by=random with
MALLOC_CHECK_=3. Both soaks pass unchanged.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01U5QRWPfmkZME7hDjU6i5EA
@lisachenko
lisachenko marked this pull request as ready for review August 16, 2026 09:51
@lisachenko
lisachenko merged commit 2b248d1 into main Aug 16, 2026
8 checks passed
@lisachenko
lisachenko deleted the claude/result-slot-recycling branch August 16, 2026 09:51
lisachenko added a commit to lisachenko/native-php-coroutines that referenced this pull request Aug 16, 2026
The previous run installed the substrate from main before
lisachenko/php-shared-data-extension#25 landed there, so the
generation-ticket surface this branch consumes did not exist yet.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01U5QRWPfmkZME7hDjU6i5EA
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants