Skip to content

opcache: fix revalidation in long running CLI scripts #18671

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: PHP-8.3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions ext/opcache/ZendAccelerator.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,12 @@ typedef int gid_t;
#endif
#include <fcntl.h>
#include <signal.h>

#ifndef PHP_WIN32
#include <time.h>
#else
#include "win32/time.h"
#endif

#ifndef ZEND_WIN32
# include <sys/types.h>
Expand Down Expand Up @@ -1162,13 +1167,32 @@ zend_result validate_timestamp_and_record(zend_persistent_script *persistent_scr
{
if (persistent_script->timestamp == 0) {
return SUCCESS; /* Don't check timestamps of preloaded scripts */
} else if (ZCG(accel_directives).revalidate_freq &&
persistent_script->dynamic_members.revalidate >= ZCG(request_time)) {
}

double revalidate_reference_time = 0.0;

if (ZCG(cli_mode)) {
#if HAVE_GETTIMEOFDAY
struct timeval tp = {0};

if (UNEXPECTED(gettimeofday(&tp, NULL) != 0)) {
revalidate_reference_time = (double)time(NULL);
} else {
revalidate_reference_time = (double)(tp.tv_sec + tp.tv_usec / 1000000.00);
}
#else
revalidate_reference_time = (double)time(NULL);
#endif
} else {
revalidate_reference_time = (double)ZCG(request_time);
}

if (ZCG(accel_directives).revalidate_freq && persistent_script->dynamic_members.revalidate >= revalidate_reference_time) {
return SUCCESS;
} else if (do_validate_timestamps(persistent_script, file_handle) == FAILURE) {
return FAILURE;
} else {
persistent_script->dynamic_members.revalidate = ZCG(request_time) + ZCG(accel_directives).revalidate_freq;
persistent_script->dynamic_members.revalidate = revalidate_reference_time + ZCG(accel_directives).revalidate_freq;
return SUCCESS;
}
}
Expand Down Expand Up @@ -2832,6 +2856,7 @@ static inline zend_result accel_find_sapi(void)
if (ZCG(accel_directives).enable_cli && (
strcmp(sapi_module.name, "cli") == 0
|| strcmp(sapi_module.name, "phpdbg") == 0)) {
ZCG(cli_mode) = true;
return SUCCESS;
}
}
Expand Down
1 change: 1 addition & 0 deletions ext/opcache/ZendAccelerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ typedef struct _zend_accel_globals {
zend_persistent_script *cache_persistent_script;
/* preallocated buffer for keys */
zend_string *key;
bool cli_mode;
} zend_accel_globals;

typedef struct _zend_string_table {
Expand Down
42 changes: 42 additions & 0 deletions ext/opcache/tests/opcache_revalidation_in_cli.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
--TEST--
opcache revalidation should work properly in CLI mode
--EXTENSIONS--
opcache
--INI--
opcache.enable=1
opcache.enable_cli=1
opcache.validate_timestamps=1
opcache.revalidate_freq=1
--FILE--
<?php
$file = __DIR__ . DIRECTORY_SEPARATOR . pathinfo(__FILE__, PATHINFO_FILENAME) . '.inc';

file_put_contents($file, <<<PHP
<?php
return 42;
PHP);

var_dump(opcache_invalidate($file, true));

var_dump(include $file);

file_put_contents($file, <<<PHP
<?php
return 1234;
PHP);

var_dump(include $file);

sleep(2);
touch($file);

var_dump(include $file);

@unlink($file);

?>
--EXPECT--
bool(true)
int(42)
int(42)
int(1234)