Skip to content

Commit e51fa6a

Browse files
Add OPcache restart hook, fired when a restart is carried out (#23828)
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.
1 parent f5e5e34 commit e51fa6a

4 files changed

Lines changed: 19 additions & 0 deletions

File tree

‎UPGRADING.INTERNALS‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ PHP 8.7 INTERNALS UPGRADE NOTES
6969
doing an assignment followed by GC_ADDREF().
7070
. The zend_call_method_if_exists_ex() was added to accepts named parameters
7171
and retrieving the callability failure error message.
72+
. The zend_accel_restart_hook callback was added. OPcache calls it once a
73+
scheduled restart has been carried out and the SHM lock is released, on
74+
the thread that carried it out, with the same reason as the
75+
zend_accel_schedule_restart_hook callback.
7276

7377
========================
7478
2. Build system changes

‎Zend/zend.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ ZEND_API zend_string *(*zend_resolve_path)(zend_string *filename);
9898
ZEND_API zend_result (*zend_post_startup_cb)(void) = NULL;
9999
ZEND_API void (*zend_post_shutdown_cb)(void) = NULL;
100100
ZEND_API void (*zend_accel_schedule_restart_hook)(int reason) = NULL;
101+
ZEND_API void (*zend_accel_restart_hook)(int reason) = NULL;
101102
ZEND_ATTRIBUTE_NONNULL ZEND_API zend_result (*zend_random_bytes)(void *bytes, size_t size, char *errstr, size_t errstr_size) = NULL;
102103
ZEND_ATTRIBUTE_NONNULL ZEND_API void (*zend_random_bytes_insecure)(zend_random_bytes_insecure_state *state, void *bytes, size_t size) = NULL;
103104

‎Zend/zend.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,10 @@ extern ZEND_ATTRIBUTE_NONNULL ZEND_API void (*zend_random_bytes_insecure)(
384384
extern ZEND_API zend_result (*zend_post_startup_cb)(void);
385385
extern ZEND_API void (*zend_post_shutdown_cb)(void);
386386

387+
/* Called by opcache when a restart is scheduled, with a zend_accel_restart_reason */
387388
extern ZEND_API void (*zend_accel_schedule_restart_hook)(int reason);
389+
/* Called by opcache once a scheduled restart has been carried out and the SHM lock is released, with the same reason */
390+
extern ZEND_API void (*zend_accel_restart_hook)(int reason);
388391

389392
ZEND_API ZEND_COLD void zend_error(int type, const char *format, ...) ZEND_ATTRIBUTE_FORMAT(printf, 2, 3);
390393
ZEND_NORETURN ZEND_API ZEND_COLD void zend_error_noreturn(int type, const char *format, ...) ZEND_ATTRIBUTE_FORMAT(printf, 2, 3);

‎ext/opcache/ZendAccelerator.c‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2974,6 +2974,9 @@ ZEND_RINIT_FUNCTION(zend_accelerator)
29742974
ZCG(counted) = false;
29752975
}
29762976

2977+
bool restarted = false;
2978+
zend_accel_restart_reason restart_reason = ACCEL_RESTART_OOM;
2979+
29772980
if (ZCSG(restart_pending)) {
29782981
zend_shared_alloc_lock();
29792982
if (ZCSG(restart_pending)) { /* check again, to ensure that the cache wasn't already cleaned by another process */
@@ -3019,6 +3022,9 @@ ZEND_RINIT_FUNCTION(zend_accelerator)
30193022
ZCSG(last_restart_time)++;
30203023
}
30213024
accel_restart_leave();
3025+
3026+
restarted = true;
3027+
restart_reason = ZCSG(restart_reason);
30223028
}
30233029
}
30243030
zend_shared_alloc_unlock();
@@ -3029,6 +3035,11 @@ ZEND_RINIT_FUNCTION(zend_accelerator)
30293035
SHM_PROTECT();
30303036
HANDLE_UNBLOCK_INTERRUPTIONS();
30313037

3038+
/* Called once the SHM lock is released and the memory is protected again */
3039+
if (UNEXPECTED(restarted) && zend_accel_restart_hook) {
3040+
zend_accel_restart_hook(restart_reason);
3041+
}
3042+
30323043
if (ZCG(accelerator_enabled) && ZCSG(last_restart_time) != ZCG(last_restart_time)) {
30333044
/* SHM was reinitialized. */
30343045
ZCG(last_restart_time) = ZCSG(last_restart_time);

0 commit comments

Comments
 (0)