phpunit icon indicating copy to clipboard operation
phpunit copied to clipboard

Fix memory leak in ExceptionWrapper

Open mvorisek opened this issue 3 years ago • 2 comments

Currently ExceptionWrapper stores the original exception forever and prevents it to be GCed.

This is causing not only memory to grow/leak, but also if the original exception has a reference to an object, the object is not released as well. Such scenario is not uncommon, as exception stack trace/frame can contain object arguments.

In consequence when the unreleased object is a some limited resource like DB connection, with a lot of wrapped/unreleased exceptions, the resource can be exhausted by a few failing tests making the other tests fail because of them.

This fix maintains the shallow backtrace as WeakReference property is not expanded during backtrace dump.

No BC break introduced.

mvorisek avatar Jul 08 '22 01:07 mvorisek

Codecov Report

Merging #5012 (bd1a437) into 9.5 (8885568) will decrease coverage by 0.02%. The diff coverage is 42.85%.

@@             Coverage Diff              @@
##                9.5    #5012      +/-   ##
============================================
- Coverage     83.82%   83.79%   -0.03%     
- Complexity     4615     4618       +3     
============================================
  Files           272      272              
  Lines         11470    11474       +4     
============================================
  Hits           9615     9615              
- Misses         1855     1859       +4     
Impacted Files Coverage Δ
src/Framework/ExceptionWrapper.php 89.18% <42.85%> (-10.82%) :arrow_down:

:mega: We’re building smart automated test selection to slash your CI/CD build times. Learn more

codecov[bot] avatar Jul 08 '22 01:07 codecov[bot]

I have updated the PR description to understand the real issue better. Please merge this fix into at least phpunit 9.5.

For next major release, is there any reason to not drop ExceptionWrapper completely?

mvorisek avatar Jul 28 '22 14:07 mvorisek

For next major release, is there any reason to not drop ExceptionWrapper completely?

The ExceptionWrapper has been removed in the main branch.

sebastianbergmann avatar Aug 23 '22 13:08 sebastianbergmann

The ExceptionWrapper has been removed in the main branch.

Thank you ❤️

To fix the memory leaks in phpunit 9.5, can this PR be merged into 9.5.x branch? No BC break should be involved.

mvorisek avatar Aug 23 '22 14:08 mvorisek