Skip to content
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

fix(agent): Fix error reporting when EH_THROW is enabled #876

Merged
merged 11 commits into from
Apr 18, 2024
Merged

Conversation

zsistla
Copy link
Contributor

@zsistla zsistla commented Apr 11, 2024

This PR
1)checks if EH_THROW(which signals to PHP to throw an exception from the error) is toggled and if so does not record an error because if uncaught it will be handled by the exception handler and if caught there's no need to record it.
2) add tests to verify fix

More details:
/*

  • For a the following error codes:
  • E_WARNING || E_CORE_WARNING || E_COMPILE_WARNING || E_USER_WARNING
  • PHP triggers an exception if EH_THROW is toggled on and then immediately
  • returns after throwing the exception. See for more info:
  • https://github.com/php/php-src/blob/master/main/main.c In that case, we
  • should not handle it, but we should exist and let the exception handler
  • deal with it; otherwise, we could record an error even if an exception is
  • caught. */

In the case where a php E_WARNING error was triggered.  We intercept the error from PHP and record it then pass execution back to PHP.

But, for instance, in the particular case that deals with a filehandling which toggles EH_THROW on here: https://github.com/php/php-src/blob/master/ext/fileinfo/fileinfo.c#L176
PHP then handles the error but for a certain number of error codes, PHP triggers an exception if EH_THROW is toggled on: https://github.com/php/php-src/blob/master/main/main.c#L1254C1-L1259C24
and then immediately returns after throwing the exception.

This leads to the case in the issue where the exception that was triggered is caught, but we recorded the error....

  /*
   * For a the following error codes:
   * E_WARNING || E_CORE_WARNING || E_COMPILE_WARNING || E_USER_WARNING
   * PHP triggers an exception if EH_THROW is toggled on and then immediately
   * returns after throwing the exception. See for more info:
   * https://github.com/php/php-src/blob/master/main/main.c In that case, we
   * should not handle it, but we should exist and let the exception handler
   * deal with it; otherwise, we could record an error even if an exception is
   * caught.
   */
@newrelic-php-agent-bot
Copy link

newrelic-php-agent-bot commented Apr 11, 2024

Test Suite Status Result
Multiverse 9/9 passing
SOAK 51/51 passing

  /*
   * For a the following error codes:
   * E_WARNING || E_CORE_WARNING || E_COMPILE_WARNING || E_USER_WARNING
   * PHP triggers an exception if EH_THROW is toggled on and then immediately
   * returns after throwing the exception. See for more info:
   * https://github.com/php/php-src/blob/master/main/main.c In that case, we
   * should not handle it, but we should exist and let the exception handler
   * deal with it; otherwise, we could record an error even if an exception is
   * caught.
   */
@codecov-commenter
Copy link

codecov-commenter commented Apr 11, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 78.76%. Comparing base (e0d2d12) to head (49ee178).

Additional details and impacted files
@@           Coverage Diff           @@
##              dev     #876   +/-   ##
=======================================
  Coverage   78.76%   78.76%           
=======================================
  Files         193      193           
  Lines       27125    27127    +2     
=======================================
+ Hits        21365    21367    +2     
  Misses       5760     5760           
Flag Coverage Δ
agent-for-php-7.0 77.82% <100.00%> (+<0.01%) ⬆️
agent-for-php-7.1 77.58% <100.00%> (+<0.01%) ⬆️
agent-for-php-7.2 78.15% <100.00%> (+<0.01%) ⬆️
agent-for-php-7.3 78.17% <100.00%> (+<0.01%) ⬆️
agent-for-php-7.4 77.87% <100.00%> (+<0.01%) ⬆️
agent-for-php-8.0 77.90% <100.00%> (+<0.01%) ⬆️
agent-for-php-8.1 77.88% <100.00%> (+<0.01%) ⬆️
agent-for-php-8.2 77.48% <100.00%> (+<0.01%) ⬆️
agent-for-php-8.3 77.48% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

agent/php_error.c Outdated Show resolved Hide resolved
agent/php_error.c Outdated Show resolved Hide resolved
agent/php_error.c Outdated Show resolved Hide resolved
Copy link
Member

@lavarou lavarou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to know the answer to this question before I approve.

agent/php_error.c Outdated Show resolved Hide resolved
Co-authored-by: Michal Nowacki <[email protected]>
Copy link
Member

@lavarou lavarou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice find!

@zsistla zsistla merged commit 8f0ae2c into dev Apr 18, 2024
62 checks passed
@lavarou lavarou added this to the next-release milestone Apr 19, 2024
@lavarou lavarou linked an issue Apr 25, 2024 that may be closed by this pull request
@zsistla zsistla deleted the handle_EH_THROW branch September 13, 2024 19:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

non-existent E_WARNING reported to NewRelic when exception is caught
5 participants