Skip to content

fix(preemption): chain the interrupt handler before suspending; run CI on PHP 8.6 - #41

Merged
lisachenko merged 2 commits into
mainfrom
feature/wonderful-johnson-03ot8f
Nov 21, 2026
Merged

lisachenko merged 2 commits into
mainfrom
feature/wonderful-johnson-03ot8f

Conversation

@lisachenko

@lisachenko lisachenko commented Nov 21, 2026 •

Copy link
Copy Markdown
Owner

The bug

With current PHP builds, preemption stopped working on all three minors. Locally, seven preemption tests fail on PHP 8.5.13 and on 8.6.0, for example testACallFreeLoopDoesNotStarveAnotherCoroutine and testAWorkerInAPreemptivePoolIsActuallyPreempted. CI was last green in August, on older patch builds.

Cause: php-src added ZEND_VM_FCALL_INTERRUPT_CHECK sites after internal calls return. There are 3 such sites in 8.4.0 and 8.5.0, and 7 in 8.4.28, 8.5.13 and 8.6.0. Here is what happens now:

  1. SIGALRM arrives and our callback runs first. No request is pending yet, so it does nothing and calls proceed(), which enters ext-pcntl's dispatch.
  2. Inside that dispatch, Preemptor::onTick() sets requested and calls requestInterrupt().
  3. When that FFI store returns, the new check fires and re-enters our callback inside the PHP signal handler. pcntl blocks fiber switches there with zend_fiber_switch_block().
  4. shouldPreempt() consumes the request, Fiber::suspend() throws FiberError: Cannot switch fibers in current execution context, and the callback swallows it.

The request is lost on every tick. preemptions() still counts up, but no coroutine is ever preempted. This is not a JIT issue: it reproduces with opcache and the JIT fully disabled.

The fix

  • InterruptBridge::onInterrupt() now calls proceed() first and decides on preemption afterwards. By then pcntl's dispatch has returned, so the suspend is legal, and the tick is taken on the same slice instead of one slice late.
  • Preemptor sets a dispatchingTick flag around onTick(), and shouldPreempt() declines while it is set. The nested callback therefore leaves the request pending for the outer one.
  • AGENTS.md records the new ordering rule and the reason for it.

Also in this PR

  • PHP 8.6 is added to every CI job, with every leg required. z-engine is now ~8.4.2 || ~8.5.0 || ~8.6.0, all stable tags (z-engine 8.6.0 is released). minimum-stability: dev / prefer-stable stay, because php-shared-data-extension is still consumed from dev-main.
  • ItimerClock::bindLibc(): dropped ?->/?? on $failure. PHPStan 2.3 proves it is non-null after the loop and reported both as errors.

Verified

  • PHP 8.6.0 (against z-engine 8.6.0) and PHP 8.5.13: 121/121 tests (7 failed before the fix)
  • tools/soak-preemption.php --seconds=20 on 8.6: PASS (1100 coroutines, none left in the callback, memory flat)
  • composer phpstan: clean; php-cs-fixer --dry-run: clean

Merge order: the 8.6 legs resolve php-shared-data-extension as dev-main, so merge lisachenko/php-shared-data-extension#34 first.

🤖 Generated with Claude Code

https://claude.ai/code/session_01FxhC1D34vSRsQVcgd3HwS6

Current PHP builds (8.4.28+, 8.5.13+ and 8.6.0) check EG(vm_interrupt) when
an internal call returns. The flag Preemptor::onTick() raises is therefore
taken while the tick handler is still running, inside ext-pcntl's signal
dispatch, where the engine blocks every fiber switch. The callback consumed
the request there, Fiber::suspend() threw a FiberError it swallows, and no
coroutine was ever preempted: seven preemption tests failed on all three
minors with current interpreter builds.

The callback now chains proceed() first and decides afterwards, outside the
dispatch frame, and shouldPreempt() declines while a tick is being
dispatched, leaving the request pending for the outer invocation.

Also adds PHP 8.6 to every CI job (z-engine ~8.6.0@dev, resolving 8.6.x-dev
until it is tagged), and drops a null-coalesce on a value PHPStan 2.3 proves
non-null.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FxhC1D34vSRsQVcgd3HwS6

Copy link
Copy Markdown
Owner Author

The PHP 8.6 legs are red at composer install, before any code runs. They resolve lisachenko/php-shared-data-extension:dev-main, and that branch's z-engine constraint has no 8.6 line yet (lisachenko/z-engine[8.5.0, ..., 8.5.x-dev] require php ~8.5.0). lisachenko/php-shared-data-extension#34 adds the line. Once it is merged, re-running these legs should turn them green: locally, against that branch on PHP 8.6.0, all 121 tests and the 20 s preemption soak pass.


Generated by Claude Code

z-engine tagged 8.6.0, so the PHP 8.6 line no longer needs its 8.6.x-dev
branch: the constraint becomes ~8.4.2 || ~8.5.0 || ~8.6.0, every minor
resolves a stable tag, and the docs drop the root-level workaround a
consumer on PHP 8.6 needed until now.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FxhC1D34vSRsQVcgd3HwS6
@lisachenko
lisachenko marked this pull request as ready for review November 21, 2026 14:45
@lisachenko
lisachenko merged commit 2989463 into main Nov 21, 2026
15 of 18 checks passed
@lisachenko
lisachenko deleted the feature/wonderful-johnson-03ot8f branch November 21, 2026 14:45
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