bugsnag / bugsnag-symfony

BugSnag notifier for the Symfony PHP framework. Monitor and report errors in your Symfony apps.
https://docs.bugsnag.com/platforms/php/symfony
MIT License
43 stars 21 forks source link

Add symfony shutdown strategy #87

Closed rjharrison closed 4 years ago

rjharrison commented 4 years ago

Goal

Adds a Symfony-specific shutdown strategy. This replaces the default PhpShutdownStrategy (register_shutdown_function) with a Symfony event-subscriber that reacts to the Kernel::TERMINATE event.

This is motivated by two benefits:

  1. No memory leaks when running tests: memory used by the Bugsnag\Client can be garbage collected (this is not possible when register_shutdown_function is used).

  2. Better performance: Symfony uses fastcgi_finish_request(), so if PHP-FPM is being used, the flush() call takes place after the HTTP connection has closed.

Design

Avoiding the memory leak when testing was the primary motivation. Potentially we could have done this by implementing conditional registration of the shutdown strategy (i.e. only register it in batch mode – current behaviour is to always register it within the Client constructor). This would have allowed users to test without the memory leak by setting batch_sending = false for the test environment.

Whilst conditional registration of the shutdown strategy is still desirable (why register one if we are not batch sending?), a better solution is to take advantage of Symfony's KernelEvents::TERMINATE event. When using WebTest to perform functional tests, Symfony's kernel is rebooted between tests (i.e. KernelEvents::TERMINATE is fired after the test). This destroys the DI container and allows memory to be garbage collected by PHP.

Changeset

Added

Changed

Tests

Discussion

Alternative Approaches

As above, I considered @ryancastle's suggestion of removing the call to register_shutdown_function from the constructor (and calling it only when batch_sending = true).

Outstanding Questions

I think we should consider conditionally registering (or deregistering) the shutdown strategy depending on whether we are batching or sending synchronously.

Linked issues

Fixes #77

Review

For the submitter, initial self-review:

For the pull request reviewer(s), this changeset has been reviewed for:

rjharrison commented 4 years ago

Builds are failing as this code requires the unmerged new stuff in https://github.com/bugsnag/bugsnag-php/pull/547