-
-
Notifications
You must be signed in to change notification settings - Fork 452
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix capturing OOM errors when very memory constrained (#1636)
- Loading branch information
1 parent
1988283
commit 33f6bf7
Showing
6 changed files
with
189 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
tests/phpt-oom/out_of_memory_fatal_error_increases_memory_limit.phpt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
--TEST-- | ||
Test that when handling a out of memory error the memory limit is increased with 5 MiB and the event is serialized and ready to be sent | ||
--INI-- | ||
memory_limit=67108864 | ||
--FILE-- | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sentry\Tests; | ||
|
||
use Sentry\ClientBuilder; | ||
use Sentry\Event; | ||
use Sentry\Options; | ||
use Sentry\SentrySdk; | ||
use Sentry\Serializer\PayloadSerializer; | ||
use Sentry\Serializer\PayloadSerializerInterface; | ||
use Sentry\Transport\Result; | ||
use Sentry\Transport\ResultStatus; | ||
use Sentry\Transport\TransportInterface; | ||
|
||
$vendor = __DIR__; | ||
|
||
while (!file_exists($vendor . '/vendor')) { | ||
$vendor = \dirname($vendor); | ||
} | ||
|
||
require $vendor . '/vendor/autoload.php'; | ||
|
||
$options = new Options([ | ||
'dsn' => 'http://public@example.com/sentry/1', | ||
]); | ||
|
||
$transport = new class(new PayloadSerializer($options)) implements TransportInterface { | ||
private $payloadSerializer; | ||
|
||
public function __construct(PayloadSerializerInterface $payloadSerializer) | ||
{ | ||
$this->payloadSerializer = $payloadSerializer; | ||
} | ||
|
||
public function send(Event $event): Result | ||
{ | ||
$serialized = $this->payloadSerializer->serialize($event); | ||
|
||
echo 'Transport called' . \PHP_EOL; | ||
|
||
return new Result(ResultStatus::success()); | ||
} | ||
|
||
public function close(?int $timeout = null): Result | ||
{ | ||
return new Result(ResultStatus::success()); | ||
} | ||
}; | ||
|
||
$options->setTransport($transport); | ||
|
||
$client = (new ClientBuilder($options))->getClient(); | ||
|
||
SentrySdk::init()->bindClient($client); | ||
|
||
echo 'Before OOM memory limit: ' . \ini_get('memory_limit'); | ||
|
||
register_shutdown_function(function () { | ||
echo 'After OOM memory limit: ' . \ini_get('memory_limit'); | ||
}); | ||
|
||
$array = []; | ||
for ($i = 0; $i < 100000000; ++$i) { | ||
$array[] = 'sentry'; | ||
} | ||
--EXPECTF-- | ||
Before OOM memory limit: 67108864 | ||
Fatal error: Allowed memory size of %d bytes exhausted (tried to allocate %d bytes) in %s on line %d | ||
Transport called | ||
After OOM memory limit: 72351744 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
--TEST-- | ||
Test that exceptions are only handled once | ||
--FILE-- | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sentry\Tests; | ||
|
||
use Sentry\ErrorHandler; | ||
|
||
$vendor = __DIR__; | ||
|
||
while (!file_exists($vendor . '/vendor')) { | ||
$vendor = \dirname($vendor); | ||
} | ||
|
||
require $vendor . '/vendor/autoload.php'; | ||
|
||
$errorHandler = ErrorHandler::registerOnceFatalErrorHandler(); | ||
$errorHandler->addFatalErrorHandlerListener(static function (): void { | ||
echo 'Fatal error listener called (should not happen)' . PHP_EOL; | ||
}); | ||
|
||
$errorHandler = ErrorHandler::registerOnceExceptionHandler(); | ||
$errorHandler->addExceptionHandlerListener(static function (): void { | ||
echo 'Exception listener called' . PHP_EOL; | ||
}); | ||
|
||
throw new \Exception('foo bar'); | ||
--EXPECTF-- | ||
Exception listener called | ||
|
||
Fatal error: Uncaught Exception: foo bar in %s:%d | ||
Stack trace: | ||
%a |