Skip to content

Add OPcache restart hook, fired when a restart is carried out - #23828

Merged
arnaud-lb merged 1 commit into
php:masterfrom
nicolas-grekas:opcache-restart-hook
Nov 16, 2026
Merged

arnaud-lb merged 1 commit into
php:masterfrom
nicolas-grekas:opcache-restart-hook

Conversation

@nicolas-grekas

@nicolas-grekas nicolas-grekas commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

After #15590, which added zend_accel_schedule_restart_hook.

That hook says a restart of OPcache's shared memory is coming, not that it happened: the restart runs later, at request startup, on whichever thread or process first finds the cache unused, and it can stay pending for a long time. This adds zend_accel_restart_hook, called on that thread once per restart, right after it completed and the SHM lock is released, with the same reason.

FrankenPHP needs it for its logs and metrics: under ZTS the restart rewinds shared memory that other threads may still reference, so the exact moment matters for post-mortems, and the scheduling event alone doesn't tell an operator whether the restart ran at all.

@nicolas-grekas

Copy link
Copy Markdown
Contributor Author

Rebased. The hook is now called after the SHM lock is released, which is what dstogov asked for the first hook in #15590.

@arnaud-lb could you have a look? php/frankenphp#2634 is merged and reports scheduled restarts with the existing hook. This new hook is what's missing to report that the restart really happened.

zend_accel_schedule_restart_hook tells an observer that a restart of
OPcache's shared memory is coming, but not whether or when it happens:
the restart runs later, at request startup, on whichever thread or
process first finds the cache unused, and it can stay pending for a
long time. This adds zend_accel_restart_hook, called on that thread once
per restart, right after the restart completed, with the same reason.
A threaded SAPI needs it for its logs and metrics, since the restart
rewinds shared memory that other threads may still reference.

@arnaud-lb arnaud-lb left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

under ZTS the restart rewinds shared memory that other threads may still reference

I'm assuming this is related to #24276? I want to get this other PR merged as well.

php/frankenphp#2634 mentions that the metric will be removed once opcache handles restarts safely under ZTS. Should I still merge this PR or do you want to wait and see if #24276 fixes the issue?

@nicolas-grekas

Copy link
Copy Markdown
Contributor Author

I think adding this hook would still be nice, it's small enough.
I didn't spot #24276 earlier but this looks related indeed - restarts leading to invalid pointers to SHM currently.

@nicolas-grekas

Copy link
Copy Markdown
Contributor Author

Please merge it, #24276 makes this hook more useful, not less.

With #24276, a restart waits until no thread uses SHM. A FrankenPHP worker uses SHM for its whole life. So the restart waits until all workers are restarted, and regular requests run uncached meanwhile. A worker that boots before the restart happened also stays uncached until its next restart.

FrankenPHP will have to restart its workers when a restart is scheduled. This hook is how it knows that the restart really happened, so that it can restart the workers that booted too early.

The note in our docs was written before #24276, we'll update it.

@arnaud-lb
arnaud-lb merged commit e51fa6a into php:master Nov 16, 2026
18 checks passed
@arnaud-lb

Copy link
Copy Markdown
Member

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants