Bug #81104 [Com]: Warning: "Failed to set memory limit to ... bytes" emitted after exit in debug

From: Date: Thu, 03 Jun 2021 23:43:13 +0000
Subject: Bug #81104 [Com]: Warning: "Failed to set memory limit to ... bytes" emitted after exit in debug
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-234202@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=81104&edit=1 ID: 81104 Comment by: tandre@php.net Reported by: tandre@php.net Summary: Warning: "Failed to set memory limit to ... bytes" emitted after exit in debug Status: Open Type: Bug Package: Scripting Engine problem PHP Version: master-Git-2021-06-03 (Git) Block user comment: N Private report: N New Comment: I suspect that https://github.com/php/php-src/commit/e9b005158f689be47f318a544b236fd5c64a9ab3#diff-2978fe1c2c45b4eca89dc476376ddc7193bc4e5e7fff0c7d1c465f057b35a5e6L1791 may be related. It has a check for (size_t)PG(memory_limit) < zend_memory_usage(1) that was removed - I assume it was discarding all errors if the current memory usage exceeds the original system memory limit (but am not deeply familiar with the output control code) Previous Comments: ------------------------------------------------------------------------ [2021-06-03 23:38:12] tandre@php.net Description: ------------ This only happens when PHP is built with --enable-debug, and it's only a notice. Possibly, it's deliberate. php -d memory_limit=5M test.php on the attached script will run the script successfully, and while php is shutting down, php will internally reset original ini settings, including memory_limit. This will trigger an error because the restored memory_limit is smaller than the amount of memory recorded as being in use at the time - this is more likely to affect programs with data structures that can't be garbage collected (gc_disable(); public function __construct() { $this->x = [$this]; }) ``` (gdb) b zend_error Breakpoint 1 at 0x613f69: file /path/to/php-src/Zend/zend.c, line 1561. (gdb) run Starting program: /path/to/php-8.1.0-debug-intersection-types-install/bin/php -d memory_limit=5M test.php [Thread debugging using libthread_db enabled] Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1". Memory usage before shutting down: 11975904 Breakpoint 1, zend_error (type=21845, format=0x555556379140 "\001") at /path/to/php-src/Zend/zend.c:1561 1561 ZEND_API ZEND_COLD void zend_error(int type, const char *format, ...) { (gdb) bt #0 zend_error (type=21845, format=0x555556379140 "\001") at /path/to/php-src/Zend/zend.c:1561 #1 0x0000555555ac1824 in OnChangeMemoryLimit (entry=0x55555637ebe0, new_value=0x55555637ec60, mh_arg1=0x0, mh_arg2=0x0, mh_arg3=0x0, stage=8) at /path/to/php-src/main/main.c:276 #2 0x0000555555c1d8eb in zend_restore_ini_entry_cb (ini_entry=0x55555637ebe0, stage=8) at /path/to/php-src/Zend/zend_ini.c:54 #3 0x0000555555c1db8c in zend_ini_deactivate () at /path/to/php-src/Zend/zend_ini.c:129 #4 0x0000555555b672ac in zend_deactivate () at /path/to/php-src/Zend/zend.c:1290 #5 0x0000555555ac4d17 in php_request_shutdown (dummy=0x0) at /path/to/php-src/main/main.c:1823 #6 0x0000555555cd21d2 in do_cli (argc=4, argv=0x555556378cb0) at /path/to/php-src/sapi/cli/php_cli.c:1134 #7 0x0000555555cd2ab6 in main (argc=4, argv=0x555556378cb0) at /path/to/php-src/sapi/cli/php_cli.c:1366 ``` Perhaps the memory_limit should be a special case, and only be restored after everything else shuts down, and memory is freed? This is emitted as a notice to stdout when running https://github.com/phan/phan to self-analyze itself in php 8.1 with the default memory limit of 138 MB (phan generates cyclic data structures in some places, such as union type representations) Test script: --------------- <?php // Run this with php -d memory_limit=5M test.php class X { public $x; public function __construct() { $this->x = [$this]; } } gc_disable(); ini_set('memory_limit', '1G'); $y = []; for ($i = 0; $i < 20000; $i++) { $y[] = new X(); } $y[0]->y = &$y; $y[0]->z = $x; echo "Memory usage before shutting down: " . memory_get_usage() . "\n"; // Observed in php 8.1: // Warning: Failed to set memory limit to 5242880 bytes (Current memory usage is 12582912 bytes) in Unknown on line 0 Expected result: ---------------- No warning should be emitted during shutdown for a request Actual result: -------------- Warning: Failed to set memory limit to 5242880 bytes (Current memory usage is 12582912 bytes) in Unknown on line 0 is emitted when php automatically tries to restore the system memory limit (php -d memory_limit=5M) ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=81104&edit=1

« previous php.bugs (#234202) next »