Allow Bugsnag\Handler to handle OOMs #621
Merged
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.
Goal
Currently we are able to report OOMs that are caused by a single, large allocation as PHP doesn't actually allocate the memory so there's plenty left for us to use. However, when an OOM happens due to lots of small allocations, we crash as there's not enough memory left to send the report
Design
Bugsnag\Handler
will now increase the memory limit on an OOM by the number of bytes specified in the newmemoryLimitIncrease
option (5MiB default)To do this successfully, we need to reserve a bit of memory so that we can:
error_get_last
We don't reserve all of the memory that we need upfront, as this would be a big overhead on every request when it shouldn't be necessary most of the time. 32K was chosen because
error_get_last
can cause PHP to re-allocate chunks of memory that it has already allocated. This can lead to a second OOM if the chunks it attempts to re-allocate are larger than the available memory. 32K seems to be a happy medium between being small enough to not matter and large enough to allowerror_get_last
to be called, but this can be tweaked in futureChangeset
memoryLimitIncrease
(5MiB default), with getters & setters inClient
&Configuration
Handler
reserves 32K of memory when the shutdown function is registeredHandler
increases the current memory limit bymemoryLimitIncrease
if an OOM is detectedTesting
New PHPT tests cause OOMs and check delivery