Bug #77575 [Opn->Nab]: set_error_handler() / ErrorException memory leak

From: Date: Wed, 28 Aug 2019 11:27:27 +0000
Subject: Bug #77575 [Opn->Nab]: set_error_handler() / ErrorException memory leak
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-222466@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=77575&edit=1

 ID:                 77575
 Updated by:         nikic@php.net
 Reported by:        frame86 at live dot com
 Summary:            set_error_handler() / ErrorException memory leak
-Status:             Open
+Status:             Not a bug
 Type:               Bug
 Package:            *General Issues
 Operating System:   Ubuntu 18.04.1 LTS
 PHP Version:        7.2.14
 Block user comment: N
 Private report:     N

 New Comment:

The reason is that the error handler receives the variable scope (including $e) and the exception
includes the backtrace including arguments, which also includes that variable scope. So effectively
you're constructing a very long and bulky linked list of exceptions.

What you can do:
 * On any version: unset($e)
 * On PHP 7.4: Set zend.exception_ignore_args=1 to disable arg collection
 * On PHP 8.0: Variable scope is no longer passed to error handler, so the issue doesn't exist.


Previous Comments:
------------------------------------------------------------------------
[2019-02-06 02:55:53] frame86 at live dot com

Description:
------------
Using an error handler, throwing and catching the ErrorException leaks memory.

Test script:
---------------
<?php

$last = time();
ini_set('memory_limit', '8G');

set_error_handler(function ($severity, $message, $file, $line) {
	throw new \ErrorException($message, 0, $severity, $file, $line);
});

while(true)
{
	try {
		trigger_error("Something");
	} catch(\Throwable $e) {
		
	}
	
	if(time() - $last >= 1) {
		$last = time();
		printf("\nMemory: %.4f", memory_get_usage() / (1024 * 1024));
	}

        gc_collect_cycles(); // doesn't help much
}

Expected result:
----------------
Nearly constant memory usage

Actual result:
--------------
Memory: 3.7187
Memory: 5.0504
Memory: 6.0593
Memory: 6.9050
Memory: 7.6437
Memory: 8.3095
Memory: 8.9218
Memory: 9.4904
...
Memory: 97.7963
Memory: 219.5202
Memory: 300.1706
Segmentation fault (core dumped)



------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=77575&edit=1


Thread (2 messages)

« previous php.bugs (#222466) next »