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

Remove use of daemon threads, fixing potential resource leak #143

Merged
merged 3 commits into from
May 30, 2019

Conversation

fractalwrench
Copy link
Contributor

Goal

Users have reported that daemon threads are not stopped when an application is stopped in a standalone Tomcat server. In an embedded server such as Spring Boot this does not matter as the JVM will always terminate when the application server is brought down, but in a standalone server, this can lead to threads leaking and performance problems over time.

We should fix this behaviour so that threads terminate properly when the application is shut down.

Changeset

  • Removed DaemonThreadFactory
  • Updated sessionExecutorService to use a regular thread factory, whose threads will be stopped on application shutdown
  • Refactored all shutdown logic to a close() method
  • Made Bugsnag implement Closable
  • Called shutdownNow on the sessionExecutorService, as the periodic check to flush sessions is no longer needed when the application is terminating.

Tests

Installed a standalone Tomcat server locally, and deployed a plain Spring application integrated with Bugsnag. Started then stopped the Tomcat server, and inspected the logs, verifying that there were no messages about thread leaks when this changeset was applied.

I also verified that the application shutdown when a crash occurred on startup, to verify that the scenario in #121 has not resurfaced.

@fractalwrench fractalwrench changed the title Remove use of daemon threads, fixing potential memory leak Remove use of daemon threads, fixing potential resource leak May 29, 2019
Copy link
Contributor

@simonbowring simonbowring left a comment

Choose a reason for hiding this comment

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

This looks good to me.

Incidentally, as a consequence of this PR and #121, in our example apps and test fixtures (I count 4 occurrences) we no longer need to do:

System.exit(SpringApplication.exit(...));

Instead we can just do this to exit an application without the application hanging:

SpringApplication.exit(...);

@fractalwrench fractalwrench merged commit 214f864 into master May 30, 2019
@fractalwrench fractalwrench deleted the remove-daemon-threads branch May 30, 2019 15:05
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.

2 participants