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)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

https://www.php.net/manual/en/function.php-sapi-name.php - maybe there is a better way to detect cli /wo extra property.

Also the time getter can be probably deduplicated to something like microtime(true).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the suggestion.

We considered using php_sapi_name() for detecting the environment at runtime, but opted to precompute this value once during initialization and store it in a cli_mode flag for performance reasons.

The motivation is that validate_timestamp_and_record() is invoked on every include or require, not just on actual cache invalidations. Using strcmp() or a similar runtime check each time would introduce measurable overhead. By storing this as a precomputed boolean, we ensure this logic has minimal runtime cost.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Regarding microtime(true), I believe there may be some confusion here. The microtime() function referenced is a userland PHP function and not directly available within the engine context.

The implementation I used is based on the internal php_time() logic, with slight adjustments to return a double for better precision.

I also avoided using microtime() as-is because it's not available consistently across all platforms.

If there’s a preferred helper or abstraction for obtaining a double-precision current time in the engine, I’m happy to switch to that but I wanted to avoid introducing inconsistencies across platforms.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Anyway, it looks like this part of PR may be weak, and I'm happy for any suggestions to it. Maybe I can drop double precision logic and switch to php_time() here.

#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)