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

Conversation

Sannis
Copy link
Contributor

@Sannis Sannis commented May 27, 2025

It looks like opcache was initially designed with short-lived requests in mind, so when checking if the file should be re-validated, the code uses 'request time' instead of 'now', where request time is calculated once on the script startup. Which basically means that in CLI this check will never succeed.

This PR containing the patch to fix this issue covered with a test that passes in this branch but not in PHP-8.3.
The issue itself is older, but I rebased the patch to PHP-8.3 as mentioned in CONTRIBUTING doc.

Not sure if I must fill the issue separately, but will be happy to do if needed.

@Sannis Sannis requested a review from dstogov as a code owner May 27, 2025 12:34
@Sannis Sannis changed the title Fix opcache revalidation in CLI opcache: fix revalidation in long running CLI scripts May 27, 2025
@Sannis Sannis marked this pull request as draft May 27, 2025 12:50
@Sannis Sannis force-pushed the fix_cli_opcache_revalidation branch from 8d75fcd to 702c189 Compare May 27, 2025 16:16
@Sannis Sannis force-pushed the fix_cli_opcache_revalidation branch from 702c189 to e210c08 Compare May 27, 2025 16:58
@Sannis Sannis marked this pull request as ready for review May 28, 2025 12:18
@Sannis Sannis changed the base branch from master to PHP-8.3 May 30, 2025 11:21
Copy link
Member

@dstogov dstogov left a comment

Choose a reason for hiding this comment

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

Technically the patch looks OK and the general idea makes sense.

However, the original behavior also makes sense for the most use cases and for the "static" CLI apps the new behaviour would introduce useless gettimeofday syscalls.

Instead of the new behavior for CLI mode only, I would think about consistent landing of this future in general. For example, we may define a new special opcache.revalidate_freq=-1 setting.

@Sannis
Copy link
Contributor Author

Sannis commented Jun 30, 2025

Hi @dstogov,

Thank you for taking the time to review the patch and for the thoughtful feedback.

I may have misread your suggestion to use opcache.revalidate_freq = -1. In today’s logic a non-positive value means “revalidate on every compile”, which effectively disables the interval check entirely. My goal is slightly different: I’d like to keep the existing period logic while simply advancing the reference time inside long-running CLI processes.

To achieve that without overloading revalidate_freq, would you be open to adding a dedicated switch, e.g.

; 0 = current behaviour (reference time fixed per process / request)
; 1 = dynamic reference time (re-evaluated on every include/require)
opcache.revalidate_use_dynamic_time = 0

Default would remain 0 for all SAPIs, preserving today’s behaviour; setting it to 1 would enable the new moving-window logic while still honouring revalidate_freq. In this case I can remove SAPI check in the patch as this may be controllable through separate php-cli.ini.

If this sounds acceptable I’ll update the patch, add the new ini entry to both development and production templates, and ping you for another look.

Thanks again for your guidance!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants