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

Add shutdown strategy #547

Merged
merged 13 commits into from
Nov 5, 2019
Merged

Add shutdown strategy #547

merged 13 commits into from
Nov 5, 2019

Conversation

rjharrison
Copy link
Contributor

@rjharrison rjharrison commented Oct 28, 2019

Goal

We currently rely on register_shutdown_function() to defer calls to flush(). This potentially causes a memory leak as references to the Client are held until the script exits. (This is only an issue when testing with many instances of Client objects).

The call to register_shutdown_function is hardcoded in the constructor. This PR introduces a ShutdownStrategyInterface which replaces this hardcoding and allows for the injection of different shutdown strategies.

There is no functionality change in this PR. The current register_shutdown_function() behaviour is now wrapped inside an object. In a future PR on the bugsnag-symfony project I will introduce a Symfony-specific shutdown strategy that will fix issue-77.

Changeset

Added

  • New ShutdownStrategyInterface parameter to Client constructor (defaults to null)
  • PhpShutdownStrategy (replicates current behaviour)

Removed

  • hardcoded call to register_shutdown_function

Tests

Added unit tests. The "shutdownSpy" is a pretty cool way to test native functions (in this case the call to register_shutdown_function)

Discussion

Alternative Approaches

None.

Outstanding Questions

I think we should also make the call to registerShutdownStrategy conditional on whether we are in batch mode or not (there is no need to defer calls to flush() if we are not "batching"). However at this stage I want to change one thing at a time, so will potentially do this as a separate piece of work.

Review

For the submitter, initial self-review:

  • Commented on code changes inline explain the reasoning behind the approach
  • Reviewed the test cases added for completeness and possible points for discussion
  • A changelog entry was added for the goal of this pull request
  • Check the scope of the changeset - is everything in the diff required for the pull request?
  • This pull request is ready for:
    • Initial review of the intended approach, not yet feature complete
    • Structural review of the classes, functions, and properties modified
    • Final review

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

  • Consistency across platforms for structures or concepts added or modified
  • Consistency between the changeset and the goal stated above
  • Internal consistency with the rest of the library - is there any overlap between existing interfaces and any which have been added?
  • Usage friction - is the proposed change in usage cumbersome or complicated?
  • Performance and complexity - are there any cases of unexpected O(n^3) when iterating, recursing, flat mapping, etc?
  • Concurrency concerns - if components are accessed asynchronously, what issues will arise
  • Thoroughness of added tests and any missing edge cases
  • Idiomatic use of the language

src/Client.php Outdated Show resolved Hide resolved
src/Shutdown/ShutdownStrategyInterface.php Show resolved Hide resolved
rjharrison and others added 4 commits October 29, 2019 09:27
…p into add-shutdown-strategy

(I merged master into remote add-shutdown-strategy by mistake - should have pulled locally)
use Bugsnag\Client;

/**
* Interface ShutdownStrategyInterface.
Copy link
Contributor

Choose a reason for hiding this comment

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

Worth expanding the docs here to describe the purpose of the interface and expected reasons for implementing it?

Copy link
Contributor

@Cawllec Cawllec left a comment

Choose a reason for hiding this comment

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

It'd be worth adding a couple more tests, one to verify that the PhpShutdownStrategy is used by default when no shutdownStrategy is declared, and one to verify that the make method of the client calls the constructor correctly and that the PhpShutdownStrategy is used and the shutdown handler registered.

Copy link
Contributor

@tomlongridge tomlongridge left a comment

Choose a reason for hiding this comment

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

LGTM 👍

Copy link
Contributor

@Cawllec Cawllec left a comment

Choose a reason for hiding this comment

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

LGTM

@rjharrison rjharrison merged commit 215a4df into master Nov 5, 2019
@rjharrison rjharrison deleted the add-shutdown-strategy branch November 5, 2019 08:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

register_shutdown_function() causes major memory leak during testing
5 participants