-
-
Notifications
You must be signed in to change notification settings - Fork 471
fix: Strip breadcrumb metadata on OOM to prevent silent event loss #2150
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
spawnia
wants to merge
8
commits into
getsentry:master
Choose a base branch
from
mll-lab:fix-oom-breadcrumb-serialization
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
39b208a
fix: Strip breadcrumb metadata on OOM to prevent secondary OOM during…
spawnia 05963e3
fix: preserve all breadcrumbs regardless of configured max_breadcrumbs
spawnia e23e980
test: add php85 variant of OOM breadcrumb stripping test
spawnia 369864a
docs: clarify why OOM_MESSAGE_MATCHER differs from ErrorHandler's
spawnia c30b256
style: use PHPDoc for OOM_MESSAGE_MATCHER explanation
spawnia 395aa52
fix: correct SKIPIF boundary in OOM tests to include PHP 8.4
spawnia 98b9e28
fix: suppress deprecation notices before autoloader in OOM tests
spawnia ae94cee
Merge branch 'master' into fix-oom-breadcrumb-serialization
spawnia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
104 changes: 104 additions & 0 deletions
104
tests/phpt-oom/php84/out_of_memory_with_large_breadcrumbs_strips_metadata.phpt
This file contains hidden or 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,104 @@ | ||
| --TEST-- | ||
| Test that when handling an OOM error with large breadcrumbs, breadcrumb metadata is stripped to prevent secondary OOM during serialization | ||
| --SKIPIF-- | ||
| <?php | ||
| if (PHP_VERSION_ID >= 80500) { | ||
| die('skip - only works for PHP 8.4 and below'); | ||
| } | ||
| --INI-- | ||
| memory_limit=67108864 | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Sentry\Tests; | ||
|
|
||
| use Sentry\Breadcrumb; | ||
| 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; | ||
|
|
||
| error_reporting(E_ALL & ~E_DEPRECATED & ~E_USER_DEPRECATED); | ||
|
|
||
| $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 | ||
| { | ||
| $breadcrumbs = $event->getBreadcrumbs(); | ||
| echo 'Breadcrumb count: ' . \count($breadcrumbs) . \PHP_EOL; | ||
|
|
||
| if (\count($breadcrumbs) > 0) { | ||
| $firstBreadcrumb = $breadcrumbs[0]; | ||
| echo 'First breadcrumb category: ' . $firstBreadcrumb->getCategory() . \PHP_EOL; | ||
| echo 'First breadcrumb has metadata: ' . (empty($firstBreadcrumb->getMetadata()) ? 'no' : 'yes') . \PHP_EOL; | ||
| } | ||
|
|
||
| $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); | ||
|
|
||
| // Add 100 breadcrumbs with ~100KB metadata each to simulate the real-world scenario | ||
| $hub = SentrySdk::getCurrentHub(); | ||
| $hub->configureScope(function (\Sentry\State\Scope $scope): void { | ||
| for ($i = 0; $i < 100; ++$i) { | ||
| $scope->addBreadcrumb(new Breadcrumb( | ||
| Breadcrumb::LEVEL_INFO, | ||
| Breadcrumb::TYPE_DEFAULT, | ||
| 'db.query', | ||
| 'SELECT * FROM large_table WHERE id = ?', | ||
| ['bindings' => str_repeat('x', 100 * 1024)] | ||
| )); | ||
| } | ||
| }); | ||
|
|
||
| // Trigger OOM - the remaining memory after breadcrumbs is limited | ||
| $array = []; | ||
| for ($i = 0; $i < 100000000; ++$i) { | ||
| $array[] = 'sentry'; | ||
| } | ||
| --EXPECTF-- | ||
| Fatal error: Allowed memory size of %d bytes exhausted (tried to allocate %d bytes) in %s on line %d | ||
| Breadcrumb count: 100 | ||
| First breadcrumb category: db.query | ||
| First breadcrumb has metadata: no | ||
| Transport called | ||
106 changes: 106 additions & 0 deletions
106
tests/phpt-oom/php85/out_of_memory_with_large_breadcrumbs_strips_metadata.phpt
This file contains hidden or 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,106 @@ | ||
| --TEST-- | ||
| Test that when handling an OOM error with large breadcrumbs, breadcrumb metadata is stripped to prevent secondary OOM during serialization | ||
| --SKIPIF-- | ||
| <?php | ||
| if (PHP_VERSION_ID < 80500) { | ||
| die('skip - only works for PHP 8.5 and above'); | ||
| } | ||
| --INI-- | ||
| memory_limit=67108864 | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Sentry\Tests; | ||
|
|
||
| use Sentry\Breadcrumb; | ||
| 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; | ||
|
|
||
| error_reporting(E_ALL & ~E_DEPRECATED & ~E_USER_DEPRECATED); | ||
|
|
||
| $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 | ||
| { | ||
| $breadcrumbs = $event->getBreadcrumbs(); | ||
| echo 'Breadcrumb count: ' . \count($breadcrumbs) . \PHP_EOL; | ||
|
|
||
| if (\count($breadcrumbs) > 0) { | ||
| $firstBreadcrumb = $breadcrumbs[0]; | ||
| echo 'First breadcrumb category: ' . $firstBreadcrumb->getCategory() . \PHP_EOL; | ||
| echo 'First breadcrumb has metadata: ' . (empty($firstBreadcrumb->getMetadata()) ? 'no' : 'yes') . \PHP_EOL; | ||
| } | ||
|
|
||
| $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); | ||
|
|
||
| // Add 100 breadcrumbs with ~100KB metadata each to simulate the real-world scenario | ||
| $hub = SentrySdk::getCurrentHub(); | ||
| $hub->configureScope(function (\Sentry\State\Scope $scope): void { | ||
| for ($i = 0; $i < 100; ++$i) { | ||
| $scope->addBreadcrumb(new Breadcrumb( | ||
| Breadcrumb::LEVEL_INFO, | ||
| Breadcrumb::TYPE_DEFAULT, | ||
| 'db.query', | ||
| 'SELECT * FROM large_table WHERE id = ?', | ||
| ['bindings' => str_repeat('x', 100 * 1024)] | ||
| )); | ||
| } | ||
| }); | ||
|
|
||
| // Trigger OOM - the remaining memory after breadcrumbs is limited | ||
| $array = []; | ||
| for ($i = 0; $i < 100000000; ++$i) { | ||
| $array[] = 'sentry'; | ||
| } | ||
| --EXPECTF-- | ||
| Fatal error: Allowed memory size of %d bytes exhausted (tried to allocate %d bytes) in %s on line %d | ||
| Stack trace: | ||
| %A | ||
| Breadcrumb count: 100 | ||
| First breadcrumb category: db.query | ||
| First breadcrumb has metadata: no | ||
| Transport called |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.