-
-
Notifications
You must be signed in to change notification settings - Fork 0
feat(ext): add exception to data in end callback #2
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
Litarnus
wants to merge
2
commits into
main
Choose a base branch
from
add-exception-to-end-callback
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
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,45 @@ | ||
| --TEST-- | ||
| Tests that re-throwing different exceptions properly populates the 'exception' key. | ||
| --EXTENSIONS-- | ||
| sentry | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| class CustomException extends Exception {} | ||
|
|
||
| function test_throw() { | ||
| throw new CustomException("Oh no"); | ||
| } | ||
|
|
||
| function test_rethrow() { | ||
| try { | ||
| test_throw(); | ||
| } catch (Throwable $t) { | ||
| echo "Exception caught" . PHP_EOL; | ||
| } | ||
| } | ||
|
|
||
| \Sentry\setEndCallback(static function (array $data) { | ||
| $exception = $data['exception']; | ||
| if ($exception !== null) { | ||
| echo $exception->getMessage() . PHP_EOL; | ||
| echo get_class($exception) . PHP_EOL; | ||
| } else { | ||
| echo 'No exception here' . PHP_EOL; | ||
| } | ||
| }); | ||
|
|
||
| \Sentry\instrument(null, 'test_throw'); | ||
| \Sentry\instrument(null, 'test_rethrow'); | ||
| try { | ||
| test_rethrow(); | ||
| } catch (Throwable $t) { | ||
|
|
||
| } | ||
|
|
||
| ?> | ||
| --EXPECTF-- | ||
| Oh no | ||
| CustomException | ||
| Exception caught | ||
| No exception here |
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,32 @@ | ||
| --TEST-- | ||
| Tests that a thrown exception is captured and stored under the 'exception' key in the end callback | ||
| --EXTENSIONS-- | ||
| sentry | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| class CustomException extends Exception { | ||
|
|
||
| } | ||
|
|
||
| function test_throw() { | ||
| throw new CustomException("Oh no"); | ||
| } | ||
|
|
||
| \Sentry\setEndCallback(static function (array $data) { | ||
| $exception = $data['exception']; | ||
| echo $exception->getMessage() . PHP_EOL; | ||
| echo get_class($exception) . PHP_EOL; | ||
| }); | ||
|
|
||
| \Sentry\instrument(null, 'test_throw'); | ||
| try { | ||
| test_throw(); | ||
| } catch (Throwable $t) { | ||
|
|
||
| } | ||
|
|
||
| ?> | ||
| --EXPECTF-- | ||
| Oh no | ||
| CustomException |
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,40 @@ | ||
| --TEST-- | ||
| Tests that some fields are always present in the start and end callback and also assert their type. | ||
| --EXTENSIONS-- | ||
| sentry | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| function test_instrumented() { | ||
| return 10; | ||
| } | ||
|
|
||
| \Sentry\setStartCallback(static function (array $data) { | ||
| echo 'Start callback' . PHP_EOL; | ||
| echo 'start_time: ' . get_debug_type($data['start_time']) . PHP_EOL; | ||
| echo 'name: ' . get_debug_type($data['name']) . PHP_EOL; | ||
| }); | ||
|
|
||
| \Sentry\setEndCallback(static function (array $data) { | ||
| echo 'End callback' . PHP_EOL; | ||
| echo 'start_time: ' . get_debug_type($data['start_time']) . PHP_EOL; | ||
| echo 'name: ' . get_debug_type($data['name']) . PHP_EOL; | ||
| echo 'duration: ' . get_debug_type($data['duration']) . PHP_EOL; | ||
| echo 'end_time: ' . get_debug_type($data['end_time']) . PHP_EOL; | ||
| echo 'metadata: ' . get_debug_type($data['metadata']) . PHP_EOL; | ||
| }); | ||
|
|
||
| \Sentry\instrument(null, 'test_instrumented'); | ||
| test_instrumented(); | ||
|
|
||
| ?> | ||
| --EXPECTF-- | ||
| Start callback | ||
| start_time: float | ||
| name: string | ||
| End callback | ||
| start_time: float | ||
| name: string | ||
| duration: float | ||
| end_time: float | ||
| metadata: array |
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,27 @@ | ||
| --TEST-- | ||
| Tests that the 'metadata' key in data in the end callback is always populated and never null (Function) | ||
| --EXTENSIONS-- | ||
| sentry | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| function test_instrumented() { | ||
| return 10; | ||
| } | ||
|
|
||
| \Sentry\setEndCallback(static function (array $data) { | ||
| if (!isset($data['metadata'])) { | ||
| echo "metadata array is not set" . PHP_EOL; | ||
| } else if ($data['metadata'] === null) { | ||
| echo "metadata array is null" . PHP_EOL; | ||
| } else { | ||
| echo "[]" . PHP_EOL; | ||
| } | ||
| }); | ||
|
|
||
| \Sentry\instrument(null, 'test_instrumented'); | ||
| test_instrumented(); | ||
|
|
||
| ?> | ||
| --EXPECTF-- | ||
| [] |
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,27 @@ | ||
| --TEST-- | ||
| Tests that the 'metadata' key in data in the end callback is always populated and never null (Attribute) | ||
| --EXTENSIONS-- | ||
| sentry | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| #[\Sentry\Trace] | ||
| function test_instrumented() { | ||
| return 10; | ||
| } | ||
|
|
||
| \Sentry\setEndCallback(static function (array $data) { | ||
| if (!isset($data['metadata'])) { | ||
| echo "metadata array is not set" . PHP_EOL; | ||
| } else if ($data['metadata'] === null) { | ||
| echo "metadata array is null" . PHP_EOL; | ||
| } else { | ||
| echo "[]" . PHP_EOL; | ||
| } | ||
| }); | ||
|
|
||
| test_instrumented(); | ||
|
|
||
| ?> | ||
| --EXPECTF-- | ||
| [] |
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,43 @@ | ||
| --TEST-- | ||
| Tests that catching an exception in a function will make 'exception' be null in the end callback | ||
| --EXTENSIONS-- | ||
| sentry | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| class CustomException extends Exception {} | ||
|
|
||
| class TestException extends Exception {} | ||
|
|
||
| function test_throw() { | ||
| throw new CustomException("Oh no"); | ||
| } | ||
|
|
||
| function test_rethrow() { | ||
| try { | ||
| test_throw(); | ||
| } catch (Throwable $t) { | ||
| throw new TestException("throw different exception"); | ||
| } | ||
| } | ||
|
|
||
| \Sentry\setEndCallback(static function (array $data) { | ||
| $exception = $data['exception']; | ||
| echo $exception->getMessage() . PHP_EOL; | ||
| echo get_class($exception) . PHP_EOL; | ||
| }); | ||
|
|
||
| \Sentry\instrument(null, 'test_throw'); | ||
| \Sentry\instrument(null, 'test_rethrow'); | ||
| try { | ||
| test_rethrow(); | ||
| } catch (Throwable $t) { | ||
|
|
||
| } | ||
|
|
||
| ?> | ||
| --EXPECTF-- | ||
| Oh no | ||
| CustomException | ||
| throw different exception | ||
| TestException |
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.
Bug: A reference to the original exception is decremented twice in the unwind-exit path (e.g., from
die()), causing a premature free and potential use-after-free.Severity: HIGH
Suggested Fix
The
zval_ptr_dtor(&event)call insentry_observer_endshould be skipped or handled conditionally when an unwind-exit occurs. The reference to the original exception is already released withinsentry_exception_isolation_endin this specific path, so the second decrement causes the object to be freed too early.Prompt for AI Agent
Did we get this right? 👍 / 👎 to inform future reviews.
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.
Not sure this is correct.
ZVAL_OBJ_COPYincreases the ref-count andzval_ptr_dtor(&event)will release each element, meaning that it will just decrement the ref-count.