diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 74c8637..0f6a477 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -9,9 +9,10 @@ on: permissions: contents: read -# z-engine tracks one PHP minor per release line; the "~8.4.2 || ~8.5.0" -# constraint lets Composer resolve the line matching each runner's PHP, so the -# suite runs on PHP 8.4 and 8.5 in parallel. +# z-engine tracks one PHP minor per release line; the +# "~8.4.2 || ~8.5.0 || ~8.6.0" constraint lets Composer resolve the stable line +# matching each runner's PHP, so the suite runs on PHP 8.4, 8.5 and 8.6 in +# parallel. Every leg is required. jobs: tests: name: Tests (PHP ${{ matrix.php }}) @@ -19,7 +20,7 @@ jobs: strategy: fail-fast: false matrix: - php: ['8.4', '8.5'] + php: ['8.4', '8.5', '8.6'] steps: - uses: actions/checkout@v7 @@ -43,7 +44,7 @@ jobs: strategy: fail-fast: false matrix: - php: ['8.4', '8.5'] + php: ['8.4', '8.5', '8.6'] steps: - uses: actions/checkout@v7 - uses: shivammathur/setup-php@v2 @@ -68,7 +69,7 @@ jobs: strategy: fail-fast: false matrix: - php: ['8.4', '8.5'] + php: ['8.4', '8.5', '8.6'] steps: - uses: actions/checkout@v7 - uses: shivammathur/setup-php@v2 diff --git a/AGENTS.md b/AGENTS.md index e2ff62f..afb3fa2 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -125,7 +125,14 @@ The mechanism was selected by experiment, and the negative result is the load-be happens inside the z-engine `InterruptHook` callback. - **The whole callback body must be `try`/`catch`-wrapped.** A throwable escaping an FFI callback is an uncatchable "Throwing from FFI callbacks is not allowed" fatal — no `catch` upstream ever runs. -- **Always chain `proceed()`** so a previously installed handler still runs. +- **Always chain `proceed()`, and chain it *before* deciding to suspend.** A previously installed + handler must still run — ext-pcntl's is where SIGALRM reaches the PHP-level tick handler. Chaining + first is also what makes preemption work at all on current PHP builds (8.4.28+, 8.5.13+, 8.6): + they check for an interrupt when an internal call returns, so the flag the tick handler raises is + taken *inside* that handler, under pcntl's fiber-switch block. `Preemptor::shouldPreempt()` + declines there (`dispatchingTick`) without consuming the request, and the outer callback takes + it once `proceed()` has returned. Suspending first and chaining second lost every request this + way: `Fiber::suspend()` threw a swallowed `FiberError` and no coroutine was ever preempted. - **No autoloading inside the callback.** Preload every class the callback touches; triggering the autoloader from an engine hook re-enters the compiler. - **Only ever `resume(null)` a preempt-suspended fiber. Never `Fiber::throw()` into one** — the spike @@ -311,7 +318,7 @@ in every process. ### z-engine's line must match the running PHP minor z-engine reads engine structures by byte offset and those offsets change on every PHP minor, so the -constraint is `~8.4.2 || ~8.5.0` — Composer resolves the stable line matching the running PHP. +constraint is `~8.4.2 || ~8.5.0 || ~8.6.0` — Composer resolves the stable line matching the running PHP. `ZEngine\Core::init()` enforces the exact match and refuses to boot on a mismatch. **Never loosen the constraint, skip `Core::init()`, or defeat the guard to make a failure go away.** A refusal is the guard working. @@ -334,7 +341,7 @@ Those two, and nothing else. A third line, `error_reporting=E_ALL & ~E_DEPRECATE alongside them: PHPUnit's `.phpt` runner forces `display_errors=1`, so a deprecation raised by the **dependency** — z-engine, back when it was consumed from a development branch — was prepended to a test's captured output and failed an `--EXPECT--` block over noise. The stable releases now required -(`~8.4.2 || ~8.5.0`) raise none, and the suppression was hiding **this package's own** deprecations +(`~8.4.2 || ~8.5.0 || ~8.6.0`) raise none, and the suppression was hiding **this package's own** deprecations too: that is how the `SplObjectStorage::contains()` call PHP 8.5 deprecated (#39) sat green through 12 tests. The suite now runs at the runner's default `error_reporting`, so a deprecation from our code fails a test the day it appears. **Do not re-add a diagnostic filter to a `.phpt`** — the guard @@ -343,6 +350,7 @@ test rejects any `error_reporting` line in an `--INI--` section. ```bash php8.4 -d ffi.enable=1 -d opcache.jit=off vendor/bin/phpunit php8.5 -d ffi.enable=1 -d opcache.jit=off vendor/bin/phpunit +php8.6 -d ffi.enable=1 -d opcache.jit=off vendor/bin/phpunit ``` ### Coding standards are a local step, not a CI job @@ -353,7 +361,7 @@ composer cs:check # the same, without writing composer phpstan # level max, must be clean ``` -CI runs **`tests`, `static-analysis` and the bounded `preemption-soak`**, all across PHP 8.4 and 8.5. +CI runs **`tests`, `static-analysis` and the bounded `preemption-soak`**, all across PHP 8.4, 8.5 and 8.6 — every leg required, 8.6 included. The coding-standards job was deliberately removed: php-cs-fixer is a development tool here, and re-checking on a runner only turns a fixable formatting nit into a red build. **Do not re-add it.** diff --git a/README.md b/README.md index be208e3..a18df58 100644 --- a/README.md +++ b/README.md @@ -24,8 +24,8 @@ fork-shared `mmap` arena and travel as addresses. composer require lisachenko/native-php-coroutines ``` -z-engine is required as `~8.4.2 || ~8.5.0` — one stable release line per supported PHP minor, -resolved by Composer to match the running PHP. `lisachenko/php-shared-data-extension` is still +z-engine is required as `~8.4.2 || ~8.5.0 || ~8.6.0` — one stable release line per supported PHP +minor, resolved by Composer to match the running PHP. `lisachenko/php-shared-data-extension` is still consumed from `dev-main` (it has no tags yet), so this package keeps `"minimum-stability": "dev"` with `"prefer-stable": true` at its root, and a consuming project needs the same pair until that dependency is tagged — Composer only resolves development stability at the root level. @@ -526,11 +526,11 @@ z-engine requires it, and z-engine is a hard dependency of this package. ## Requirements -- PHP **8.4** or **8.5**, NTS. +- PHP **8.4**, **8.5** or **8.6**, NTS. - `ext-ffi` with `ffi.enable=1`, and `opcache.jit=off` — the JIT rewrites the executor internals the engine hooks depend on. -- `lisachenko/z-engine`, resolved per PHP minor (`8.4.x` on 8.4, `8.5.x` on 8.5, from the - `~8.4.2 || ~8.5.0` constraint) — engine structures are read by byte offset, so the line must +- `lisachenko/z-engine`, resolved per PHP minor (`8.4.x` on 8.4, `8.5.x` on 8.5, `8.6.x` on 8.6, + from the `~8.4.2 || ~8.5.0 || ~8.6.0` constraint) — engine structures are read by byte offset, so the line must match the running minor. `ZEngine\Core::init()` enforces it and refuses to boot on a mismatch. - `ext-pcntl` and `ext-posix` for the parallel and preemption layers (suggested, not required). @@ -596,14 +596,15 @@ z-engine requires it, and z-engine is a hard dependency of this package. ## Development ```bash -php8.4 -d ffi.enable=1 -d opcache.jit=off vendor/bin/phpunit # the suite, on both minors +php8.4 -d ffi.enable=1 -d opcache.jit=off vendor/bin/phpunit # the suite, on every minor php8.5 -d ffi.enable=1 -d opcache.jit=off vendor/bin/phpunit +php8.6 -d ffi.enable=1 -d opcache.jit=off vendor/bin/phpunit composer phpstan # level max composer cs:fix # PER-CS2.0, a local step ``` Coding standards are deliberately **not** a CI job — CI runs `tests`, `static-analysis` and a -bounded `preemption-soak` across PHP 8.4 and 8.5. A segfault is an engine-level bug, never a flaky test: capture the command, the PHP +bounded `preemption-soak` across PHP 8.4, 8.5 and 8.6, every leg required. A segfault is an engine-level bug, never a flaky test: capture the command, the PHP version and a minimal reproducer instead of retrying. Contributor rules — the engine contracts, the shared-memory disciplines, the preemption obligations diff --git a/composer.json b/composer.json index 6b82f6f..569b304 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,7 @@ "php": "^8.4", "ext-ffi": "*", "lisachenko/php-shared-data-extension": "dev-main", - "lisachenko/z-engine": "~8.4.2 || ~8.5.0" + "lisachenko/z-engine": "~8.4.2 || ~8.5.0 || ~8.6.0" }, "minimum-stability": "dev", "prefer-stable": true, diff --git a/src/Preemption/InterruptBridge.php b/src/Preemption/InterruptBridge.php index d6f722e..c85d69f 100644 --- a/src/Preemption/InterruptBridge.php +++ b/src/Preemption/InterruptBridge.php @@ -131,32 +131,40 @@ public function requestInterrupt(): void /** * The engine callback. Runs inside the interrupted frame, in the interrupted coroutine's fiber. * - * Read the two `try` blocks as one rule each: the first must never let a throwable out, and the - * second must never let a chained handler be skipped — including when the first one suspended - * and the coroutine was resumed seconds later, which is exactly where control returns to. + * Read the two `try` blocks as one rule each: the first must never let a chained handler be + * skipped, and the second must never let a throwable out. + * + * The chained handler runs **first**. ext-pcntl's is where the slice timer's SIGALRM is + * dispatched to {@see Preemptor}, so chaining first means the tick that brought control here is + * already recorded when the policy is asked, and the suspend happens after pcntl's dispatch has + * returned — that dispatch blocks every fiber switch for as long as it runs. Current PHP builds + * (8.4.28+, 8.5.13+, 8.6) also check for an interrupt when an internal call returns, so this + * callback is re-entered from *inside* the PHP signal handler as well; the policy declines there + * without consuming the request, and this outer invocation takes it once `proceed()` returns. */ private function onInterrupt(InterruptHook $hook): void { + try { + if ($hook->hasOriginalHandler()) { + $hook->proceed(); + } + } catch (\Throwable) { + } + try { if (\Fiber::getCurrent() !== null && ($this->shouldPreempt)()) { $this->preemptions++; // Control leaves the process's PHP stack here and comes back in // Scheduler::runNext(), which sees SuspendCommand::PREEMPT and re-queues the - // coroutine. Execution resumes on the next line when it gets the CPU again. + // coroutine. Execution resumes on the next line when it gets the CPU again, with + // nothing left to do: the chained handler has already run. \Fiber::suspend(SuspendCommand::PREEMPT); } } catch (\Throwable) { // Deliberately swallowed and deliberately not logged: logging is user code, user code // can throw, and a throw from here is an uncatchable fatal. } - - try { - if ($hook->hasOriginalHandler()) { - $hook->proceed(); - } - } catch (\Throwable) { - } } /** diff --git a/src/Preemption/ItimerClock.php b/src/Preemption/ItimerClock.php index 00d287a..35ae688 100644 --- a/src/Preemption/ItimerClock.php +++ b/src/Preemption/ItimerClock.php @@ -265,7 +265,7 @@ private static function bindLibc(): \FFI } throw new \RuntimeException( - 'could not bind setitimer(2) through FFI: ' . ($failure?->getMessage() ?? 'unknown reason'), + 'could not bind setitimer(2) through FFI: ' . $failure->getMessage(), 0, $failure, ); diff --git a/src/Preemption/Preemptor.php b/src/Preemption/Preemptor.php index d259106..3194b88 100644 --- a/src/Preemption/Preemptor.php +++ b/src/Preemption/Preemptor.php @@ -27,11 +27,17 @@ * # The path a preemption takes * * 1. `setitimer(ITIMER_REAL)` raises `SIGALRM` every slice. - * 2. ext-pcntl's C handler raises `EG(vm_interrupt)`; {@see self::onTick()} — the PHP-level handler - * — records that a preemption is *wanted* and raises the flag again, so the callback is reached - * outside pcntl's own dispatch frame. - * 3. The engine calls the interrupt callback at the next check (a loop back-edge or a function - * entry), the callback asks {@see self::shouldPreempt()}, and suspends if the answer is yes. + * 2. ext-pcntl's C handler raises `EG(vm_interrupt)`. The engine calls the interrupt callback at + * the next check (a loop back-edge, a function entry, or the return of an internal call), and + * the callback first chains to ext-pcntl, whose dispatch runs {@see self::onTick()} — the + * PHP-level handler — which records that a preemption is *wanted*. + * 3. Back in the callback, now outside pcntl's dispatch frame, it asks {@see self::shouldPreempt()} + * and suspends if the answer is yes. + * + * Current PHP builds (8.4.28+, 8.5.13+, 8.6) also check for an interrupt when an internal call + * returns, so the flag {@see self::onTick()} raises is taken *inside* the handler, where the engine + * blocks every fiber switch. {@see self::$dispatchingTick} makes that nested callback decline + * without consuming the request, and the outer callback takes it once the dispatch has returned. * * # What makes the answer "no" * @@ -111,6 +117,15 @@ final class Preemptor private int $criticalDepth = 0; + /** + * {@see self::onTick()} is running, i.e. control is inside ext-pcntl's signal dispatch. + * + * The engine refuses every fiber switch there, so an interrupt callback reached from inside the + * handler must leave the request pending instead of consuming it on a suspend that can only + * fail with `FiberError`. + */ + private bool $dispatchingTick = false; + /** * The clock is stopped because the process is blocked in the poller with nothing running. * @@ -380,7 +395,7 @@ public function withCriticalSection(\Closure $body): mixed */ public function shouldPreempt(): bool { - if (!$this->armed || !$this->requested || $this->criticalDepth > 0) { + if (!$this->armed || !$this->requested || $this->criticalDepth > 0 || $this->dispatchingTick) { return false; } @@ -402,18 +417,27 @@ public function shouldPreempt(): bool * The PHP-level SIGALRM handler. * * It may not suspend anything — the engine refuses fiber switches from signal dispatch, always - * and on every dispatch path — so it records the request and raises the VM interrupt flag, - * which gets the interrupt callback invoked outside pcntl's dispatch frame. + * and on every dispatch path — so it records the request and raises the VM interrupt flag. On + * the async path the interrupt callback that is dispatching this handler takes the request as + * soon as the dispatch returns; on current PHP builds the raised flag is also seen by a nested + * callback while this method still runs, and {@see self::$dispatchingTick} makes that one + * decline. * * @param int $signal The delivered signal, always SIGALRM here. * @param mixed $details ext-pcntl's siginfo payload; unused. */ private function onTick(int $signal, mixed $details = null): void { - $this->requested = true; + $this->dispatchingTick = true; - if ($this->criticalDepth === 0) { - $this->bridge->requestInterrupt(); + try { + $this->requested = true; + + if ($this->criticalDepth === 0) { + $this->bridge->requestInterrupt(); + } + } finally { + $this->dispatchingTick = false; } }