-
Notifications
You must be signed in to change notification settings - Fork 7.9k
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
Sannis
wants to merge
4
commits into
php:PHP-8.3
Choose a base branch
from
Sannis:fix_cli_opcache_revalidation
base: PHP-8.3
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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)
.There was a problem hiding this comment.
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 acli_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. Usingstrcmp()
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.There was a problem hiding this comment.
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. Themicrotime()
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.
There was a problem hiding this comment.
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.