Repository navigation
feat(ipc): recycle settled result slots through a generation-tagged free list - #25
Merged
Merged
Conversation
…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
marked this pull request as ready for review
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 onmainfirst.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
SlotTicketpacks the slot index and its generation into 32 bits (16 each) — exactly what aWakeEvent'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, andcreate()refuses more rather than truncating ids.Two choices worth reviewing:
ResultState::Freemakes the free-list state self-describing — two independent guards);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
ResultSlotTableis 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: aFORMATheader 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 theprefault()call and computes its$sizefrom this branch's larger geometry (8 header words, 12 per slot), so the whole table is made resident at creation; both of #24'sArenaTestprefault 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
--order-by=randomandMALLOC_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).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.vendor/resolvesphp >= 8.5. CI covers both minors.🤖 Generated with Claude Code
https://claude.ai/code/session_01U5QRWPfmkZME7hDjU6i5EA
Generated by Claude Code