Repository navigation
Description
The glue that turns the substrate primitives into the package's public parallel API. This ticket writes no shared-memory machinery of its own — the arena, rings, slots and locks come from the extension; this is the PHP-side runtime that drives them and makes them selectable alongside local channels.
⛔ Blocked on lisachenko/php-shared-data-extension#16 (E1 — arena, arena-backed
persist) and lisachenko/php-shared-data-extension#18 (E3 —SharedChannel,SharedArray, result slots, event-record notification protocol). Everything else in #1 is independent of the substrate; only this ticket waits.For local development against unmerged substrate work, pin the extension to
dev-claude/php-coroutines-plan-5vovszincomposer.jsonand mark the temporary pin in the PR body. The pin must be removed before the milestone is undrafted.
✅ The premise is validated
The substrate spikes (ext#15) ran 84/84 green on PHP 8.4.19 and 8.5.9: an engine-formatted zend_object in MAP_SHARED|MAP_ANONYMOUS memory attaches as an ordinary PHP instance in several forked processes at once, and a plain $obj->prop = ... in one is visible to the others within ~200 µs. The reverse direction works too — a child bump-allocates and persists post-fork, sends 8 bytes, and the parent attaches. Zero-copy cross-process objects are real; this ticket is glue, not a gamble.
Binding corrections from those spikes
These are acceptance criteria, not advice. They come from measured failures, and each one is a defect that would otherwise be found in production rather than in review.
- A 16-byte record is not read atomically. ~1.3 % of unlocked reads saw payload and tag from different generations. Every record in an arena ring or result slot is therefore either accessed wholly under the slot's mutex, or published payload-first-tag-last and read tag-first-payload-second, with the tag as the publication flag. Never a mix of the two disciplines. (The control socket is unaffected — an ordered byte stream cannot tear.)
- An aligned 8-byte pointer read is atomic. A single-slot
OBJ/STR/ARRaddress read whose tag cannot change may skip the lock and get old-or-new, never a mix. Do not over-lock the hot read path inawait(). - Never
var_dump(),json_encode(),get_object_vars()or(array)a shared object unless the extension'sget_properties_forinterception is active. Those read-shaped operations make engine C code write a per-processpropertiespointer into the shared struct, segfaulting every sibling afterwards. This binds the runtime's own diagnostics hardest: a panic path or debug dump is exactly the code that reaches forvar_dump()on the value it is reporting. - Never key a shared object by
spl_object_id(), and never put one in anSplObjectStorage. Forked children inherit the same object-store free list and hand out identical handle numbers — handles collide by construction. Arena address is the only cross-process identity, andspl_object_id()on a shared object needs a documented story rather than an assumption. - Closures are rejected on provenance — compiled before the fork barrier — never on shape. A post-fork closure cannot be recognised by inspection: a stale address held a different, perfectly valid
Closurethat on PHP 8.5 executed the wrong function instead of failing. Any check that inspects bound variables, scope or arity will pass that case. - A worker that dies holding an arena lock surfaces
EOWNERDEAD. The extension handles recovery, but this ticket must make sure the resulting failure reaches the waiter as aWorkerCrashedExceptionrather than a silent hang, and that a recovered-but-inconsistent slot is not read as if it were valid.
Scope
- Named shared roots —
declareShared(string $name, string $class, int $capacity)at configuration time,shared(string $name)for lookup. Roots are created pre-fork so every worker inherits them by address; declaring a root afterrun()has forked is an error with a message that says so. persist()/ attach flow —$runtime->persist($obj)clones a graph into the arena and hands back the shared instance. Attaching in a worker is by address, through the extension's per-process side table; the package must never cache an arena address across aforkboundary in a way that assumes the parent's per-process state.SharedChannelintegration — expose the substrate ring throughChannelInterfaceso it drops into the existingSelectunchanged.readinessFd()returns the worker's wake-pipe FD; the poller must drain the wake pipe on readiness (level-triggered pokes: spurious wakeups are harmless, lost wakeups impossible, but an undrained pipe spins the poller).- Result slots +
JoinHandle::await()—spawnParallel()allocates a slot, returns a handle;await()parks the calling coroutine on the pipe FD via the poller, wakes on theRESULTrecord, and reads the value directly from shared memory. Awaiting an already-complete slot returns immediately without parking; a slot may be awaited from any process. - Panic path — an uncaught
Throwablein a parallel task completes its slot asPANICwith the address of a shared error-info object (class, message, trace as arena strings), rethrown atawait()asParallelTaskExceptionpreserving that information. The NSR applies here too: the panic must not be serialized to cross the boundary — and per correction 3, the path that builds it must notvar_dump()the offending value. NotShareableValueException— thrown when a value cannot cross a boundary (plain array, closure, resource, non-shared object). The message must name the remedy:$runtime->persist($obj), useSharedArray, implementTask.- Version-gate on the substrate's
LAYOUT_VERSION 4and fail fast with a clear message on mismatch.
Acceptance criteria
- A worker mutates a shared object and the parent observes the mutation on the same identity — zero-copy, no re-read from any encoding.
-
spawnParallel→await()returns each tag correctly:NIL/TRUE/FALSE,INT,FLOAT,STR(arena string),OBJ(shared object),ARR(SharedArray). -
await()on an already-completed slot does not park;await()from a different process than the spawner works. - Slot publication order is asserted, not assumed: a test that writes a slot and reads it concurrently under contention must never observe a tag newer than its payload.
- No
var_dump/json_encode/get_object_vars/(array)on a shared object anywhere in the shipped source — enforced by a source check, including the panic and diagnostic paths. - No
spl_object_id()/SplObjectStoragekeying of shared objects — enforced the same way. - A
SharedChanneland a localChannelin a singleSelectboth fire, and the shared one wakes the poller through its readiness FD. - The wake pipe is drained — a test asserts the poller does not spin (bounded wakeups for a bounded number of sends).
- Task panic surfaces as
ParallelTaskExceptionwith class, message and trace intact. - A worker killed while holding an arena lock surfaces
WorkerCrashedExceptionat the waiter — no hang, no silently-consumed inconsistent slot. -
NotShareableValueExceptionfor a plain array, a closure, a resource and a non-shared object — each message naming its remedy; the closure case documented as provenance-based. - No
serialize/igbinary/json_encodeanywhere on the data path — enforced by a test that greps the shipped source, so a regression fails CI rather than review. - A coroutine blocked on a shared primitive or an outstanding
JoinHandleis not reported byDeadlockException(externally wakeable — see A: local scheduler — run queue, timers, poller, deadlock detection #3). - Soak: arena watermark plateaus (no unbounded growth), memory flat, zero leftover children.
Activity
docs(contracts): encode the substrate spike corrections in the value …
lisachenko commentedon Aug 15, 2026
#5 and #6 were built in parallel and their seams do not fit as they stand. This ticket does the real wiring, so the fix belongs here. Recording it precisely so it is not rediscovered by watching workers silently fail to be preempted.
The mismatch
ProcessWorker::runChild()invokes the$afterForkclosure beforeWorkerChild::main()creates the child's scheduler — deliberately, since the closure exists to re-arm the interval timer that fork clears, and that must happen before any fiber exists.But
Preemptor::__construct()takes aScheduler, andPreemptor::shouldPreempt()answers by consulting$this->scheduler->current(). So neither available option works:Preemptorinside$afterFork— impossible, the child's scheduler does not exist at that point.Preemptor'srearmAfterFork()— the timer is armed correctly, but the bridge stays bound to the child's copy of the parent'sSchedulerobject.current()on that copy never returns the coroutine the child's real scheduler is running, soshouldPreempt()returns false forever and the child is never preempted, silently.Option 2 is the trap, because it looks right and fails quietly — the same silent-failure mode
ItimerClock's docblock warns about, reached by a different route.What the fix probably looks like
Not prescribing a design, but the shape is constrained:
ItimerClock::rearmAfterFork()) and is separable from the scheduler binding. Re-arming the clock in$afterForkis correct and should stay there.Preemptormust be constructed against the child's own scheduler, which means afterWorkerChild::main()builds it. That needs either a second seam insideWorkerChild, or$afterForkwidened to\Closure(int, SchedulerInterface): voidand moved after scheduler construction — but then it no longer runs before fibers exist, which was the point of its current placement. Splitting it into two callbacks (pre-scheduler clock re-arm, post-scheduler preemptor attach) is the option that preserves both invariants.Scheduler::attachPreemptor()already exists, so the child's scheduler can be given a preemptor after construction.Acceptance criterion to add
Nothing on
mainis affected today —Runtime::spawnParallel()still refuses with this ticket'sLogicException, so the combination is unreachable from the public API. Found by smoke-testing the wiring during the #12 integration; the signatures are type-compatible, which is exactly why the mismatch is semantic rather than something the type checker would catch.Generated by Claude Code
lisachenko commentedon Aug 15, 2026
✅ Unblocked — the substrate is merged, and it changes three things in this ticket
lisachenko/php-shared-data-extension#21 (E1 arena + E2 mutable mode + E3 IPC primitives) and lisachenko/php-shared-data-extension#22 (E4
AGENTS.md+ E5 Phase A closures) are both merged to that repo'smain. Everything this ticket was waiting for exists, so no temporary composer pin is needed — depend on the released line, not ondev-claude/php-coroutines-plan-5vovsz.1. The event record is theirs, not ours —
ControlRecordmust changeI committed on lisachenko/php-shared-data-extension#15 that if E3's wire shapes differed, this package would follow. They differ. The substrate's notification record is 16 bytes:
Our
ControlRecordis 32 bytes (opcode | 7 pad | uint64 slot_id | 16-byte TaggedRecord). The substrate's layout wins on anything crossing into shared memory or its wake sockets. Whether our own parent↔worker control socket (#6,ProcessWorker/ControlSocket) also converts is a judgement call for this ticket — one record shape everywhere is simpler and worth it unless there is a concrete reason to keep two.TaggedRecord(16 bytes,uint8 tag | 7 pad | uint64 payload) still matches their value record, so that half is already aligned.2.
LAYOUT_VERSIONis 5, not 4The ticket body says version-gate on 4. E1 took it to 4 (arena tables) and E2 to 5 (the per-object role). Gate on 5 and hard-fail on mismatch.
Also check our
Tagenum against theirValueTagnumerically, not just by name: they appendClosure = 9and state that tag numbers are the wire contract of this runtime and are never renumbered. IfNIL=0 … CLOSE=8does not match theirs exactly, ours changes.3. Closures are now shareable — and
Taskis no longer the only optionE5 Phase A landed
registerSharedClosure($name, $closure)before amarkForkBarrier(), with acceptance by provenance (a pre-fork register), never by shape — exactly as correction #8 requires. Captures are validated at registration:use (&$ref)andstatic $ninside the body are refused, as are plain arrays and request objects.So
NotShareableValueException::forClosure()'s message should now nameregisterSharedClosure()as the remedy rather than only pointing atTask. Post-fork closures stay refused, and Phase B (arena-cloned op_arrays) is documented but not implemented.4. What we get to build on
Shm\Arena(pre-forkmmap, robust pshared mutex bank, roots directory),PersistentStore::bootShared($arena),persist($key, $obj, mutable: true),mutableHandle()for the synchronized write path.Ipc\SharedChannel(capacity-0 rendezvous crosses processes;close()crosses processes),SharedArray,ResultSlotTable(allocateSlot/complete/completePanic/readSlot/await),SharedError,SharedMutex,AtomicInt,SharedWaitGroup,WakeRegistry.notificationStream()on every primitive — that is preciselyChannelInterface::readinessFd(). Their blocking helpers are a spin loop and they say outright that "parking a Fiber is deliberately not in this package" and belongs to the consumer runtime. That is this ticket.sharedIdOf($obj)is the identity, returning the arena address.spl_object_id()reads a deliberate sentinel and is meaningless — matching correction B: local channels, select, sync primitives, context #4 already recorded above.5. Constraints their limits section adds to ours
cefor the whole family. Our prefork ordering gains a step.$obj->prop = ...is unsynchronized; the synchronized path ismutableHandle(). Our docs should not show a bare property write on a shared object as the happy path.SharedErrorper store — a second panic replaces the first. That bounds whatParallelTaskExceptioncan promise when several tasks panic; say so rather than implying every panic is preserved.Still required, unchanged
The preemption-seam blocker from the previous comment stands and is part of this ticket.
Generated by Claude Code
Merge Agent F: Layer P integration — shared arena, channels and resul…
Merge pull request #13 from lisachenko/feature/php-coroutines-impleme…