Embed SAPI (PHP_EMBED_START_BLOCK/PHP_EMBED_END_BLOCK) memory leak(?)
Description
I'm assuming it should be possible to run multiple (independent) embedded blocks, one after the other - I can't find information saying otherwise.
My use case is somewhat advanced, but the repro is really simple. I cut out code until there was almost nothing left, and basically running multiple PHP_EMBED_START_BLOCK/PHP_EMBED_END_BLOCK result in an ever increasing memory consumption, which I guess is a leak of some kind.
In my real use case, at the rate I'm executing embedded blocks, I see a steady linear increase in RSS of ~200MB/h, with the repro below it's ~1GB/h. Same result in PHP 8.2.4 and PHP 7.4.33.
I've tried various properties/functions of the sapi_module_struct, it doesn't seem to make a difference, but for reference I have custom callbacks for the following methods:
- php_embed_module.ub_write
- php_embed_module.log_message
- php_embed_module.sapi_error
- zend_error_cb
In essence, the PHP_EMBED_START_BLOCK/PHP_EMBED_END_BLOCK seem to be the culprit.
The following code:
int main()
{
while (1)
{
for(int i = 0; i < 1000; ++i)
{
PHP_EMBED_START_BLOCK(0, NULL)
//zend_eval_string("echo phpinfo();", NULL, "");
PHP_EMBED_END_BLOCK()
}
//Check/print memory consumption
}
}
./test > /dev/null
Resulted in this output: An increase of ~1GB RAM / hour in memory consumption
But I expected this output instead: A somewhat steady memory consumption
PHP Version
PHP 8.2.4
Operating System
No response
I cannot say much about the leak, but to run multiple scripts you should extract and modify php_embed.c, everything what that script does you can do in your code (php_embed.c is like hello world). Basically php_request_startup and php_request_shutdown should be called for every new script, everything before and after should be executed once at the start and end of program respectively. This will provide the same separation of the "requests" as in fpm or apache.
Without modification you can probably call php_request_shutdown();php_request_startup(); after every script to reset state (not recommended).
More about PHP lifecycle: https://www.phpinternalsbook.com/php7/extensions_design/php_lifecycle.html
@KapitanOczywisty , thank you for your response, specifically the "hello world" analogy, it helps in understanding. I thought php_embed.h was more "ready to use", not a "sample" (as I've seen it "used" like that before).
As I understand it now, parts of what's covered by the macros (PHP_EMBED_START_BLOCK/PHP_EMBED_END_BLOCK, or specifically php_embed_init/php_embed_shutdown) should only be run once, while other parts should be executed "per request", which makes sense. The important thing for me, as you noted, is to reset state between requests. I'll rewrite some code...
I suppose the "leak" may have been caused by my code running multiple sapi init/shutdown and/or module init/shutdown, instead of only request startup/shutdown. Closing this as PEBKAC for now.
I thought php_embed.h was more "ready to use", not a "sample" (as I've seen it "used" like that before).
It's good enough for simple tasks, and most uses are simple. What also contributes to that is poor documentation, I'll try to update it.
As I understand it now, parts of what's covered by the macros (PHP_EMBED_START_BLOCK/PHP_EMBED_END_BLOCK, or specifically php_embed_init/php_embed_shutdown) should only be run once
Unfortunate part is that these functions include per-request startup/shutdown, which makes hard to reuse them in multi-request scenario.