Skip to content

Commit

Permalink
Free reserved memory before handling fatal errors (#42630)
Browse files Browse the repository at this point in the history
When the PHP process exceeds the configured memory limit,
a fatal error such as the following occurs:

```
Allowed memory size of 1073741824 bytes exhausted (tried to allocate 3358976 bytes)
```

Currently, the process abruptly exits in such cases without
and indication as to what has caused the error. This is
because the handler from `register_shutdown_function()`
attempts to instantiate a `FatalError` instance, thus trying
to use memory that is not available.

This clears the reserved memory before this instantiation,
thus ensuring that the process has enough memory to
properly handle the error. Reserved memory is also cleared
in `handleException` in the same way.
  • Loading branch information
spawnia authored Jun 2, 2022
1 parent 51efb37 commit 5094350
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/Illuminate/Foundation/Bootstrap/HandleExceptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,9 @@ protected function ensureNullLogDriverIsConfigured()
*/
public function handleException(Throwable $e)
{
try {
self::$reservedMemory = null;
self::$reservedMemory = null;

try {
$this->getExceptionHandler()->report($e);
} catch (Exception $e) {
//
Expand Down Expand Up @@ -217,6 +217,8 @@ public function handleShutdown()
*/
protected function fatalErrorFromPhpError(array $error, $traceOffset = null)
{
self::$reservedMemory = null;

return new FatalError($error['message'], 0, $error, $traceOffset);
}

Expand Down

0 comments on commit 5094350

Please sign in to comment.