Skip to content

fix(php): build on php 8.5.13 for the opcache gh-24577 fix - #143

Closed
abnegate wants to merge 2 commits into
mainfrom
fix/php-8.5.13
Closed

abnegate wants to merge 2 commits into
mainfrom
fix/php-8.5.13

Conversation

@abnegate

Copy link
Copy Markdown
Member

Draft: decision needed. This pins PHP 8.5.13RC1 because 8.5.13 GA is not published yet (php:8.5.13-alpine does not exist; php:8.5-alpine still serves 8.5.12). The choice is between shipping the RC now and holding this until GA (expected around 2026-11-19), then re-pinning the GA digest. The structure test added here works for both.

Symptom

Sentry CLOUD-3R2D (4827 events): TypeError: Utopia\Database\DateTime::addSeconds(): Argument #1 ($date) must be of type DateTime, DateTime given. The cloud call site passes new \DateTime().

The same pod and time windows also produced these sibling issues. Every one is a DateTime check, covering arguments, nullable arguments and return values across three repos:

Issue Failing check
CLOUD-3QZ9, CLOUD-3R1B, CLOUD-3R4W DateTime::format
CLOUD-3R4V Adapter\Pool::getMinDateTime(): Return value must be of type DateTime, DateTime returned
CLOUD-3RAW, CLOUD-3RAX, CLOUD-3RCX Validator\Query\Filter::__construct
CLOUD-3RCY Adapter\Limits::__construct
CLOUD-3RAV cloud Event\Message\Usage::__construct (?DateTime)

Root cause

PHP 8.5.12's OPcache has a race when it publishes the class-name cache (php-src GH-24577, fixed by 47d5c0c).

  1. When a script is persisted, zend_accel_pending_ce_cache_commit() does two things to each newly interned class name in shared memory:

    • it sets the IS_STR_CLASS_NAME_MAP_PTR flag;
    • it stores the name's map_ptr offset in the refcount field.

    On amd64, GCC emits the flag store first (orl $0x20,0x4(%rdx) at 0x47ff84, then movl %esi,(%rdx) at 0x47ff88).

  2. A forked Swoole worker can see the flag before the offset is stored. It then takes the string's placeholder refcount, 2, as the offset.

  3. At offset 2, the 8 bytes span map_ptr slot 0 (bytes 1 to 7) and the low byte of slot 1.

  4. ext/date registers first at startup, so slot 1 always holds DateTime's cached class entry.

  5. If the worker links a class in that window, it writes that class's pointer across those bytes. This zeroes the low byte of DateTime's cached zend_class_entry *.

  6. From then on, every DateTime type check in that worker fails: the object's class does not match the corrupted pointer, but both print as DateTime. This lasts until the worker exits.

  7. If the worker reads in the window instead, it gets a non-canonical pointer and segfaults.

This explains why only DateTime is affected, and why a band-aid on one method cannot fix it.

Confirmed

  • No source defect in utopia-php/database.
    • src/Database/DateTime.php declares use DateTime as PhpDateTime, which resolves to the global \DateTime.
    • The cloud call sites pass new \DateTime().
    • A namespace mistake would print Utopia\Database\DateTime, so that explanation does not fit.
  • The Sentry timeline matches a cold-start race. The data was read through the Sentry API, read-only.
    • First seen 2026-09-16, last seen 2026-10-30.
    • The events came in four bursts. Each burst came from exactly one pod, lasted between 3 seconds and 8 minutes, and started 4 to 35 minutes after its release, which is when pods start during rollout.
    • The releases were 1.50.60 (PHP 8.5.10), 1.50.99 (8.5.10), 1.51.44 (8.5.12) and 1.51.73 (8.5.12).
  • The JIT is not the cause.
    • Cloud commit 78911b06ff (opcache.jit=disable) is an ancestor of 1.50.99, 1.51.44 and 1.51.73.
    • Three of the four bursts ran with the JIT disabled.
  • The Rust utopia extension is not the cause. It was added to cloud on 2026-11-11, after the last event.
  • DateTime is never racily flagged itself.
    • It gets map_ptr slot 1 (offset 9) at module startup.
    • accel_copy_permanent_strings copies it into shared memory, flag included, before the fork.
    • This was measured in the cloud 1.51.73 image, in appwrite/base and in php:8.5.12-cli-alpine.
  • Store order in the stock alpine amd64 binaries:
    • 8.5.12 stores the flag before the offset.
    • 8.5.13RC1 stores the offset, then the flag. On x86 the release fence compiles to no instruction.
  • Simulating the corrupted write reproduces the exact error. In the cloud 1.51.73 image, writing one pointer at offset 2:
    • turned DateTime's cached pointer 0x…b60 into 0x…b00;
    • made every \DateTime-typed call throw must be of type DateTime, DateTime given;
    • left DateTimeZone, Closure, Throwable and user-class checks passing.
  • Fix location: 47d5c0c is in php-8.5.13RC1 and not in php-8.5.12. Production runs on amd64 nodes, checked with read-only kubectl get nodes -L kubernetes.io/arch.

Inferred

  • How a worker links a class inside another worker's few-instruction commit window in production. The likely cause is concurrent cold-start compiles across the swoole_cpu_num() * 6 workers sharing one cold shared-memory segment. The reproduction forces this case with a privately compiled class; it is not observed in production.
  • Behaviour on amd64. The amplified harness ran on arm64 because this host has no amd64 emulation. The byte arithmetic is the same on both little-endian architectures, and the amd64 disassembly shows the same store order.
  • Why the bursts stopped after 2026-10-30. This is unexplained. Grafana, which could show pod-start counts, was unreachable (Cloudflare Access redirect). With 4 bursts in about 6 weeks, a quiet period does not argue against a rare race.

Fix

Move the PHP base to 8.5.13RC1, which contains 47d5c0c. Add a structure test that fails on any image older than 8.5.13.

  • Why the digest sits under php:8.5-alpine. The pin is php:8.5.13RC1-alpine's multi-arch index digest, placed on the php:8.5-alpine@sha256:… line. The dependency updater's Dockerfile::pins, which StructureTest also runs, rejects any other tag.
  • What changes once GA ships. When php:8.5-alpine serves 8.5.13, the weekly updater re-pins the digest with no manual step.
  • Why this fix and not something else.
    • fix: accept any DateTimeInterface in DateTime helpers utopia-php/database#983 widens two methods to \DateTimeInterface. That only stops the corrupted DateTime cache slot being consulted in those two methods. The other DateTime checks in the sibling issues above stay broken, and the worker's instanceof \DateTime and new \DateTime remain corrupted.
    • Setting opcache.interned_strings_buffer=0 in cloud would also remove the race: 0 hits in the matrix below. It costs per-worker memory, so it is only an interim option if GA is far off.

Proof

1. Regression test. tests.yaml gains PHP is 8.5.13 or newer (OPcache fix for php-src GH-24577), which checks PHP_VERSION_ID >= 80513 (RC1 reports 80513). Commit order is test first, then fix. The images were built with --target final and run with container-structure-test.

ab4e9f4 (PHP 8.5.12, 80512)
--- FAIL: PHP is 8.5.13 or newer (OPcache fix for php-src GH-24577)
     Expected: 0, Actual: 1
Passes: 17  Failures: 1

99795ae (PHP 8.5.13RC1, 80513)
--- PASS: PHP is 8.5.13 or newer (OPcache fix for php-src GH-24577)
Passes: 18  Failures: 0

Other checks on HEAD:

  • tests-swoole.php: OK.
  • tests-xdebug.yaml: 1 of 1 pass.
  • Updater suite: pint OK, phpstan OK, phpunit OK (160 tests), parity OK (91 contracts).
  • php -m is unchanged from the 8.5.12 image.

2. Amplified race reproduction. These are PHP builds from source.

  • The 8.5.12amp build keeps 8.5.12's flag-then-offset order and adds an env-gated usleep between the two stores. That call widens the window without reordering anything.
  • The 8.5.13RC1amp build puts the same sleep after the release fence.
  • The harness: a fork master starts 1 writer, which persists an uncached class, and 8 readers behind a barrier. The readers call \DateTime, ?\DateTime and DateTime-return functions plus a user-class-typed function.
  • Each cell ran 100 cold starts.
Cell Exact must be of type DateTime, DateTime given SIGSEGV Clean
8.5.12amp, interned_strings_buffer=32 (cloud) 100 / 100 100 0
8.5.12amp, interned_strings_buffer=0 0 0 100
8.5.13RC1amp, interned_strings_buffer=32 0 0 100
8.5.13RC1amp, interned_strings_buffer=0 0 0 100
8.5.12 + 47d5c0c only, amplified 0 0 100
  • Write path: the exact TypeError covers argument, nullable argument, return, nullable return and the aliased PhpDateTime form. The user-class-typed checks never failed.
  • Persistence: once corrupted, a worker kept failing every DateTime check, including a probe made after the window had closed.
  • Read path: this gives the SIGSEGVs.
  • Without amplification: stock binaries gave 0 of 100 on both versions. The natural window is a single instruction, which matches bursts that are rare and confined to one pod.

Collisions

  • chore: update dependencies #137 (automation, CONFLICTING) re-pins the same line to another 8.5.12 digest. It would now fail the new test. It is superseded by this PR or by the next weekly run.
  • Weekly updater. Until php:8.5-alpine serves 8.5.13, the weekly updater's bundled pull request will fail the new test. Every other dependency update in that bundle is blocked with it.
  • Version number. If the run on Monday 2026-11-16 at 03:00 UTC publishes 2.3.1 before this merges, the CHANGES.md entry here moves to the next patch version.
  • base_image label. The image's label reads php:8.5-alpine@<RC digest> while the RC is pinned.
  • README. The README says the updater picks the newest stable digest. That is not true while the RC is pinned.

Follow-up

  • Bump cloud's ARG BASE_IMAGE to the release that contains this change.
  • Then watch the CLOUD-3R2D family over the next few rollouts.

Supersedes

🤖 Generated with Claude Code

abnegate and others added 2 commits November 14, 2026 12:36
PHP 8.5.12's OPcache can flag a newly interned shared class name as
having a CE-cache slot before it stores the slot's map_ptr offset
(php-src GH-24577; on amd64 GCC emits the flag store before the offset
store in zend_accel_pending_ce_cache_commit). A forked Swoole worker
that links a class in that window reads the string's refcount, 2, as the
offset and writes that class pointer across map_ptr slots 0 and 1.
ext/date registers first at startup, so slot 1 is always DateTime's: its
cached class entry loses its low byte and every DateTime type check in
that worker fails with "must be of type DateTime, DateTime given"
(Sentry CLOUD-3R2D) until the worker exits.

php-src 47d5c0c fixes it and first ships in 8.5.13RC1, so the test
requires PHP_VERSION_ID >= 80513 (RC1 reports 80513; version_compare
would reject "8.5.13RC1"). The image still runs 8.5.12 at this commit,
so the structure test fails until the following fix commit.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
PHP 8.5.12's OPcache can flag a newly interned shared class name as
having a CE-cache slot before it stores the slot's map_ptr offset
(php-src GH-24577; on amd64 GCC emits the flag store before the offset
store in zend_accel_pending_ce_cache_commit). A forked Swoole worker
that links a class in that window reads the string's refcount, 2, as the
offset and writes that class pointer across map_ptr slots 0 and 1.
ext/date registers first at startup, so slot 1 is always DateTime's: its
cached class entry loses its low byte and every DateTime type check in
that worker fails with "must be of type DateTime, DateTime given"
(Sentry CLOUD-3R2D) until the worker exits. php-src 47d5c0c fixes it
and first ships in 8.5.13RC1; php:8.5-alpine still serves 8.5.12.

The pinned digest is php:8.5.13RC1-alpine's multi-arch index, kept under
the php:8.5-alpine name because the updater's Dockerfile::pins, which
StructureTest also runs, rejects any other tag. Meanwhile the image's
base_image label reads php:8.5-alpine@<RC digest>.

Costs: until php:8.5-alpine serves 8.5.13, the weekly updater proposes
8.5.12's digest and its pull request fails the new structure test.
Because the updater bundles every pin into one pull request, all other
dependency updates are blocked meanwhile. Once php:8.5-alpine serves
8.5.13, the updater re-pins it with no manual step.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@loks0n

loks0n commented Nov 19, 2026

Copy link
Copy Markdown
Member

PHP 8.5.13 went GA today, so #150 pinned the GA digest and shipped as 2.4.2. This RC pin is no longer needed.

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