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

[PLAT-2110] Use deamon threads to start flushing sessions #121

Merged
merged 6 commits into from
Nov 29, 2018

Conversation

Pezzah
Copy link
Contributor

@Pezzah Pezzah commented Nov 27, 2018

Goal

In some cases the Bugsnag notifier can prevent clean application shutdowns due to the session flushing threads that are running.

This changes the threads to be marked as "deamon" so that the application can terminate without waiting for them to finish

The deamon threads run every 60 seconds to flush sessions created during that time. New threads are used to actually perform the flushing which are not marked as deamon so that they won't get killed part way though sending data.

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

@Pezzah Pezzah requested a review from fractalwrench November 27, 2018 13:01
Copy link
Contributor

@fractalwrench fractalwrench left a comment

Choose a reason for hiding this comment

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

Left a few comments around the implementation, once resolved I'm happy to approve (after I perform manual testing again)

* This is to prevent applications from hanging waiting for the sessions scheduled task
* because deamon threads will be terminated on application shutdown
*/
public class DeamonThreadFactory implements ThreadFactory {
Copy link
Contributor

Choose a reason for hiding this comment

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

The implementation of this class could be simplified by using the decorator pattern. We could create a field for Executors.defaultThreadFactory(), then make the thread returned by threadFactory.newThread a daemon before returning. That might also make a good candidate for a unit test.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

changed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

also added a test

/**
* @see ThreadFactory#newThread(Runnable)
*/
public Thread newThread(Runnable runner) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This is missing an override annotation

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added

/**
* Constructor
*/
public DeamonThreadFactory() {
Copy link
Contributor

Choose a reason for hiding this comment

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

There's a typo in the class name (Deamon -> Daemon)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

// Create a new thread which is not a deamon thread to actually flush the sessions
// This will means that the thread will not be killed
// half way through if the application shuts down
Thread nonDeamonThread = new Thread(new Runnable() {
Copy link
Contributor

Choose a reason for hiding this comment

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

We could use a cached executor e.g. Executors.newSingleThreadExecutor() rather than creating a new thread every time this is invoked.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added new newSingleThreadExecutor

fractalwrench
fractalwrench previously approved these changes Nov 27, 2018
Copy link
Contributor

@fractalwrench fractalwrench left a comment

Choose a reason for hiding this comment

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

LGTM - just needs a changelog entry.

@kattrali
Copy link
Contributor

This issue came up before with a single thread executor in #50 (current logic on master) - should the session flushing logic use an executor service in the same way to ensure the service doesn't hang an app? I'd just like to check that we aren't introducing a different way for the application to hang by fixing this one.

@fractalwrench fractalwrench self-requested a review November 28, 2018 09:27
@fractalwrench fractalwrench merged commit 5e294cf into master Nov 29, 2018
@fractalwrench fractalwrench deleted the fix-application-hang branch November 29, 2018 09:45
tomlongridge added a commit that referenced this pull request Nov 6, 2020
The fix to prevent a resource leak in #143 removed the task scheduler running as
a daemon thread and so doesn't exit unless the application is explicitly exited
(via System.exit) or the thread is explicitly stopped (via bugsnag.close).

This fix should be the benefits of both #143 and #121 to gracefully shutdown in
both use cases.

fixes #151
tomlongridge added a commit that referenced this pull request Nov 6, 2020
The fix to prevent a resource leak in #143 removed the task scheduler running as
a daemon thread and so doesn't exit unless the application is explicitly exited
(via System.exit) or the thread is explicitly stopped (via bugsnag.close).

This fix should be the benefits of both #143 and #121 to gracefully shutdown in
both use cases.

fixes #151
tomlongridge added a commit that referenced this pull request Nov 7, 2020
The fix to prevent a resource leak in #143 removed the task scheduler running as
a daemon thread and so doesn't exit unless the application is explicitly exited
(via System.exit) or the thread is explicitly stopped (via bugsnag.close).

This fix should be the benefits of both #143 and #121 to gracefully shutdown in
both use cases.

fixes #151
tomlongridge added a commit that referenced this pull request Nov 9, 2020
The fix to prevent a resource leak in #143 removed the task scheduler running as
a daemon thread and so doesn't exit unless the application is explicitly exited
(via System.exit) or the thread is explicitly stopped (via bugsnag.close).

This fix should be the benefits of both #143 and #121 to gracefully shutdown in
both use cases.

fixes #151
tomlongridge added a commit that referenced this pull request Nov 9, 2020
The fix to prevent a resource leak in #143 removed the task scheduler running as
a daemon thread and so doesn't exit unless the application is explicitly exited
(via System.exit) or the thread is explicitly stopped (via bugsnag.close).

This fix should be the benefits of both #143 and #121 to gracefully shutdown in
both use cases.

Updated the Spring Boot app test and example to remove explict System.exit call
as this should not be required.

fixes #151
tomlongridge added a commit that referenced this pull request Nov 9, 2020
The fix to prevent a resource leak in #143 removed the task scheduler running as
a daemon thread and so doesn't exit unless the application is explicitly exited
(via System.exit) or the thread is explicitly stopped (via bugsnag.close).

This fix should be the benefits of both #143 and #121 to gracefully shutdown in
both use cases.

Updated the Spring Boot app test and example to remove explict System.exit call
as this should not be required.

fixes #151
tomlongridge added a commit that referenced this pull request Nov 9, 2020
The fix to prevent a resource leak in #143 removed the task scheduler running as
a daemon thread and so doesn't exit unless the application is explicitly exited
(via System.exit) or the thread is explicitly stopped (via bugsnag.close).

This fix should be the benefits of both #143 and #121 to gracefully shutdown in
both use cases.

Updated the Spring Boot app test and example to remove explict System.exit call
as this should not be required.

fixes #151
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.

3 participants