Skip to content

Commit d88821a

Browse files
committed
original opcache reset in 8.2
1 parent 7c28f3d commit d88821a

File tree

4 files changed

+26
-7
lines changed

4 files changed

+26
-7
lines changed

frankenphp.c

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,12 @@ HashTable *main_thread_env = NULL;
8686
__thread uintptr_t thread_index;
8787
__thread bool is_worker_thread = false;
8888
__thread HashTable *sandboxed_env = NULL;
89-
zif_handler orig_opcache_reset;
9089

90+
#if PHP_VERSION_ID >= 80300
9191
/* Forward declaration */
9292
PHP_FUNCTION(frankenphp_opcache_reset);
93+
zif_handler orig_opcache_reset;
9394

94-
#if PHP_VERSION_ID >= 80300
9595
/* Try to override opcache_reset if opcache is loaded.
9696
* Safe to call multiple times - skips if already overridden in this function
9797
* table. Uses handler comparison instead of orig_opcache_reset check so that
@@ -479,12 +479,14 @@ PHP_FUNCTION(frankenphp_getenv) {
479479
}
480480
} /* }}} */
481481

482+
#if PHP_VERSION_ID >= 80300
482483
/* {{{ thread-safe opcache reset */
483484
PHP_FUNCTION(frankenphp_opcache_reset) {
484485
go_schedule_opcache_reset(thread_index);
485486

486487
RETVAL_TRUE;
487488
} /* }}} */
489+
#endif
488490

489491
/* {{{ Fetch all HTTP request headers */
490492
PHP_FUNCTION(frankenphp_request_headers) {
@@ -768,13 +770,13 @@ static int frankenphp_startup(sapi_module_struct *sapi_module) {
768770
php_import_environment_variables = get_full_env;
769771

770772
int result = php_module_startup(sapi_module, &frankenphp_module);
773+
#if PHP_VERSION_ID >= 80300 && PHP_VERSION_ID < 80500
771774
if (result == SUCCESS) {
772-
#if PHP_VERSION_ID >= 80300
773775
/* All extensions are now loaded. Override opcache_reset if opcache
774776
* was not yet available during our MINIT (shared extension load order). */
775777
frankenphp_override_opcache_reset();
776-
#endif
777778
}
779+
#endif
778780

779781
return result;
780782
}
@@ -1238,7 +1240,9 @@ bool frankenphp_new_php_thread(uintptr_t thread_index) {
12381240
static int frankenphp_request_startup() {
12391241
frankenphp_update_request_context();
12401242
if (php_request_startup() == SUCCESS) {
1241-
#if PHP_VERSION_ID >= 80300
1243+
#if PHP_VERSION_ID >= 80300 && PHP_VERSION_ID < 80500
1244+
/* for php 8.5+ opcache is always compiled statically, so it's already
1245+
* hooked in main request startup */
12421246
frankenphp_override_opcache_reset();
12431247
#endif
12441248
return SUCCESS;
@@ -1440,12 +1444,20 @@ int frankenphp_execute_script_cli(char *script, int argc, char **argv,
14401444
}
14411445

14421446
int frankenphp_reset_opcache(void) {
1447+
#if PHP_VERSION_ID >= 80300
14431448
zend_execute_data execute_data;
14441449
zval retval;
14451450
memset(&execute_data, 0, sizeof(execute_data));
14461451
ZVAL_UNDEF(&retval);
14471452
orig_opcache_reset(&execute_data, &retval);
14481453
zval_ptr_dtor(&retval);
1454+
#else
1455+
zend_function *opcache_reset =
1456+
zend_hash_str_find_ptr(CG(function_table), ZEND_STRL("opcache_reset"));
1457+
if (opcache_reset) {
1458+
zend_call_known_function(opcache_reset, NULL, NULL, NULL, 0, NULL, NULL);
1459+
}
1460+
#endif
14491461
return 0;
14501462
}
14511463

threadregular.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ func (handler *regularThread) beforeScriptExecution() string {
5757
}
5858
handler.state.WaitFor(state.Ready, state.ShuttingDown)
5959
return handler.beforeScriptExecution()
60-
6160
case state.Ready:
6261
return handler.waitForRequest()
6362

threadworker.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,14 @@ func (handler *workerThread) waitForWorkerRequest() (bool, any) {
229229
globalLogger.LogAttrs(globalCtx, slog.LevelDebug, "shutting down", slog.String("worker", handler.worker.name), slog.Int("thread", handler.thread.threadIndex))
230230
}
231231

232+
if Version().VersionID < 80300 {
233+
// flush the opcache when restarting due to watcher or admin api
234+
// note: this is done right before frankenphp_handle_request() returns 'false'
235+
if handler.state.Is(state.Restarting) {
236+
C.frankenphp_reset_opcache()
237+
}
238+
}
239+
232240
return false, nil
233241
case requestCH = <-handler.thread.requestChan:
234242
case requestCH = <-handler.worker.requestChan:

worker.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ func DrainWorkers() {
173173
// RestartWorkers attempts to restart all workers gracefully
174174
// All workers must be restarted at the same time to prevent issues with opcache resetting.
175175
func RestartWorkers() {
176-
restartThreadsAndOpcacheReset(true)
176+
restartThreadsAndOpcacheReset(false)
177177
}
178178

179179
func (worker *worker) attachThread(thread *phpThread) {

0 commit comments

Comments
 (0)