Skip to content

Fixed GH-22216: don't warn in OnUpdateMemoryConsumption on ZTS thread init - #22225

Open
dkkoma wants to merge 1 commit into
php:PHP-8.5from
dkkoma:fix-gh-22216-opcache-memcons-zts
Open

dkkoma wants to merge 1 commit into
php:PHP-8.5from
dkkoma:fix-gh-22216-opcache-memcons-zts

Conversation

@dkkoma

@dkkoma dkkoma commented Jun 4, 2026

Copy link
Copy Markdown

Fixes GH-22216

Testing

Put this as /tmp/ztsrepro/repro_min.c

#include <sapi/embed/php_embed.h>
#include <pthread.h>

static void *worker(void *arg)
{
    ts_resource(0);
    php_request_startup();
    php_printf("[worker] ");
    zend_eval_string("echo opcache_get_configuration()['directives']['opcache.memory_consumption'], PHP_EOL;", NULL, "w");
    php_request_shutdown(NULL);
    ts_free_thread();
    return NULL;
}

int main(int argc, char **argv)
{
    php_embed_init(argc, argv);
    php_printf("[main]   ");
    zend_eval_string("echo opcache_get_configuration()['directives']['opcache.memory_consumption'], PHP_EOL;", NULL, "m");
    pthread_t t;
    pthread_create(&t, NULL, worker, NULL);
    pthread_join(t, NULL);
    php_embed_shutdown();
    return 0;
}

Build php-src with --enable-zts --enable-embed, Run the following from the build root

cc /tmp/ztsrepro/repro_min.c -o /tmp/ztsrepro/repro_min -I. -Imain -IZend -ITSRM -Isapi/embed -Iext/date/lib -D_GNU_SOURCE -DZTS -DZEND_SIGNALS -DNDEBUG -pthread -Llibs -lphp -Wl,-rpath,libs

ini

echo 'opcache.log_verbosity_level=2' > /tmp/ztsrepro/opcache.ini

before

$ PHPRC=/tmp/ztsrepro/opcache.ini /tmp/ztsrepro/repro_min
[main]   134217728
Thu Jun  4 16:35:49 2026 (6129397760): Warning opcache.memory_consumption cannot be changed when OPcache is already set up.

[worker] 0

after

$ PHPRC=/tmp/ztsrepro/opcache.ini /tmp/ztsrepro/repro_min
[main]   134217728
[worker] 134217728

@dkkoma
dkkoma requested a review from dstogov as a code owner June 4, 2026 08:11
@dkkoma
dkkoma force-pushed the fix-gh-22216-opcache-memcons-zts branch from b56e49d to 57bacf8 Compare August 15, 2026 12:16
@dkkoma

dkkoma commented Aug 15, 2026

Copy link
Copy Markdown
Author

The previous CI failure appeared to be unrelated to this change. I've rebased the branch onto the latest PHP-8.5, and all checks are passing now. @dstogov, could you please take a look when you have a chance?

…ead init

The ZTS per-thread ini refresh runs on_modify at ZEND_INI_STAGE_STARTUP to
populate the new thread's globals; skip the accel_startup_ok guard there so
it is not treated as a post-startup change.
@dkkoma
dkkoma force-pushed the fix-gh-22216-opcache-memcons-zts branch from 57bacf8 to a5080ed Compare August 16, 2026 12:08
@pavimus

pavimus commented Oct 12, 2026

Copy link
Copy Markdown

Prepared with an AI assistant (Claude); the text below is AI-generated (quoted, as asked in CONTRIBUTING.md).

We hit the same issue under FrankenPHP (PHP 8.5 ZTS) and had made the identical change before finding this PR, so +1. Some additional data on why this is more than a cosmetic warning: with ZCG(accel_directives).memory_consumption at 0 in late-started threads,

  • zend_accel_schedule_restart_if_necessary() divides the wasted memory by 0 (+INF) and schedules a restart on the first wasted byte once SHM is full, instead of at opcache.max_wasted_percentage;
  • opcache_get_status() reports a negative used_memory and current_wasted_percentage NAN.

Measured from a FrankenPHP worker thread (1 thread, 32M SHM, files modified under load):

memory_consumption used_memory wasted % OOM restarts per run
PHP-8.5 0 -16792 NAN 8
with this change 32M 33.2M 0.98 4 (same as 8.4)

Since GH-23309 (8.4.26 / 8.5.11) opcache.interned_strings_buffer has the same problem; the same one-line change for it is #24275 (PHP-8.4).

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.

2 participants