Make WordPress Core

Opened 17 months ago

Closed 17 months ago

Last modified 17 months ago

#57876 closed enhancement (invalid)

class WP_Fatal_Error_Handler can not handle memory limit errors

Reported by: amisiewicz's profile amisiewicz Owned by:
Milestone: Priority: normal
Severity: minor Version:
Component: Bootstrap/Load Keywords:
Focuses: Cc:

Description

I found a potential issue with the following code:

<?php
register_shutdown_function( array( $handler, 'handle' ) );

In the handle function, one of the first actions taken is:

<?php
$error = $this->detect_error();

I believe this code may not execute properly if a PHP memory limit error has already occurred.

For example, in the Monolog library's ErrorHandler.php file, the handleFatalError function is registered as a shutdown function with the following code:

<?php
register_shutdown_function([$this, 'handleFatalError']);

$this->reservedMemory = str_repeat(' ', 1024 * $reservedMemorySize);

Then, in the handleFatalError function, the first action taken is to free up the reserved memory:

<?php
$this->reservedMemory = '';
$lastError = error_get_last();

Therefore, it should be safe to assign $lastError in this context.

Please note that I have not yet conducted any tests to confirm whether this is a potential bug or whether the code can be enhanced. However, I wanted to report this issue to ensure that it is not overlooked and will continue to investigate further. If I find any additional information or evidence, I will update this report accordingly.

Change History (4)

#1 @costdev
17 months ago

  • Component changed from General to Bootstrap/Load

#2 @amisiewicz
17 months ago

  • Keywords close added; dev-feedback removed

I couldn't find any information on that topic. I just tested it out manually with this simple script:

<?php
// Set the memory limit to a low value for testing purposes
ini_set('memory_limit', '5M');
// Disable error display
ini_set('display_errors', 'Off');

// Define the shutdown function
function shutdown_function() {
    $error = error_get_last();
    // still works in php5.6
    $some_more_memory = range(1, 2000);
    // do not work in php5.6
    // $some_more_memory = range(1, 3000);
    // still works in php7.4 and php8.1
    // $some_more_memory = range(1, 60000);
    // do not work in php7.4 and php8.1
    // $some_more_memory = range(1, 70000);

    if ($error !== null) {
        $message = "The script has been shut down due to an error: {$error['message']}\n";
    }
    echo $message;
}

// Register the shutdown function
register_shutdown_function('shutdown_function');

// Allocate an array that exceeds the memory limit
$large_array = range(1, 1000000);

My conclusion is that this is not a problem. We can still assign some variables (less memory to assign in php5.6, more in php7 and above), but php can still do it. I'm not sure why the monolog library does what it does.

#3 @costdev
17 months ago

  • Keywords close removed
  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed
  • Version trunk deleted

Hi @amisiewicz, thanks for looking into this!

Let's close this ticket as invalid for now, and if you find anything that suggests a change, feel free to reopen this ticket and post your findings.

#4 @costdev
17 months ago

  • Focuses performance removed
Note: See TracTickets for help on using tickets.