-
-
Notifications
You must be signed in to change notification settings - Fork 28
fix(sentry): bound endContext flush timeout and guarantee context release #1084
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
huangdijia
wants to merge
1
commit into
main
Choose a base branch
from
sentry-pr/r2
base: main
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
| /** | ||
| * This file is part of friendsofhyperf/components. | ||
| * | ||
| * @link https://github.com/friendsofhyperf/components | ||
| * @document https://github.com/friendsofhyperf/components/blob/main/README.md | ||
| * @contact huangdijia@gmail.com | ||
| */ | ||
|
|
||
| namespace FriendsOfHyperf\Tests\Sentry; | ||
|
|
||
| use RuntimeException; | ||
| use Sentry\ClientInterface; | ||
| use Sentry\Options; | ||
| use Sentry\State\HubInterface; | ||
| use Sentry\State\RuntimeContextManager; | ||
| use Sentry\Transport\Result; | ||
| use Sentry\Transport\ResultStatus; | ||
|
|
||
| // Load the component's class_map replacement for the SDK class (the same file the | ||
| // ConfigProvider registers via the container class_map), so these tests exercise the | ||
| // coroutine-safe implementation instead of the raw SDK class. | ||
| require_once __DIR__ . '/../../src/sentry/class_map/RuntimeContextManager.php'; | ||
|
|
||
| beforeEach(function () { | ||
| // Mock hub + client so the RuntimeContextManager constructor never resolves the container | ||
| // ($baseHub->getClient() returns a truthy mock client). | ||
| $this->client = $this->createMock(ClientInterface::class); | ||
| $this->client->method('getOptions')->willReturn(new Options()); | ||
| $this->client->method('captureEvent')->willReturn(null); | ||
|
|
||
| $this->baseHub = $this->createMock(HubInterface::class); | ||
| $this->baseHub->method('getClient')->willReturn($this->client); | ||
| }); | ||
|
|
||
| test('startContext creates an isolated hub and marks the context active', function () { | ||
| // Tests run inside a coroutine via FriendsOfHyperf\Tests\TestCase, so the | ||
| // CoArrayObject-backed manager has a clean per-coroutine context here. | ||
| $manager = new RuntimeContextManager($this->baseHub); | ||
| $manager->startContext(); | ||
|
|
||
| expect($manager->hasActiveContext())->toBeTrue(); | ||
| expect($manager->getCurrentContext()->getHub())->not->toBe($this->baseHub); | ||
| }); | ||
|
|
||
| test('endContext forwards the flush timeout to the client', function () { | ||
| $received = []; | ||
| $this->client->method('flush')->willReturnCallback(static function (?int $timeout) use (&$received) { | ||
| $received[] = $timeout; | ||
|
|
||
| return new Result(ResultStatus::success()); | ||
| }); | ||
|
|
||
| $manager = new RuntimeContextManager($this->baseHub); | ||
|
|
||
| $manager->startContext(); | ||
| $manager->endContext(1500); | ||
| expect($received)->toBe([1500]); | ||
|
|
||
| $manager->startContext(); | ||
| $manager->endContext(null); | ||
| expect($received)->toBe([1500, 0]); | ||
| }); | ||
|
|
||
| test('endContext does not throw when the client flush throws and still releases the context', function () { | ||
| $this->client->method('flush')->willThrowException(new RuntimeException('transport unavailable')); | ||
|
|
||
| $manager = new RuntimeContextManager($this->baseHub); | ||
| $manager->startContext(); | ||
|
|
||
| $manager->endContext(); | ||
|
|
||
| expect($manager->hasActiveContext())->toBeFalse(); | ||
| }); | ||
|
|
||
| test('startContext is idempotent for the current execution key', function () { | ||
| $manager = new RuntimeContextManager($this->baseHub); | ||
|
|
||
| $manager->startContext(); | ||
| $firstId = $manager->getCurrentContext()->getId(); | ||
|
|
||
| $manager->startContext(); | ||
| $secondId = $manager->getCurrentContext()->getId(); | ||
|
|
||
| expect($secondId)->toBe($firstId); | ||
| }); |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When
CoHttpTransport's channel is full, this timeout does not preventendContext()from hanging: the logs and metrics flushes run first and eventually callCoHttpTransport::send(), whose channel push uses the separate$this->timeoutatsrc/sentry/src/Transport/CoHttpTransport.php:73. The new0only reaches$client->flush()afterward, and this component'sCoHttpTransport::close()at lines 78-81 ignores that argument entirely. Consequently the deferredSentrySdk::endContext()calls can still block in the exact full-channel scenario this change targets; the enqueue timeout itself must be bounded or the aggregator flushes must avoid blocking.Useful? React with 👍 / 👎.