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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## TBD

* Remove use of daemon threads, fixing potential resource leak
[#143](https://github.com/bugsnag/bugsnag-java/pull/143)

## 3.5.0 (2019-05-07)

* Migrate version information to device.runtimeVersions
Expand Down
31 changes: 13 additions & 18 deletions bugsnag/src/main/java/com/bugsnag/Bugsnag.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import com.bugsnag.callbacks.Callback;
import com.bugsnag.delivery.Delivery;
import com.bugsnag.delivery.HttpDelivery;
import com.bugsnag.util.DaemonThreadFactory;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Closeable;
import java.lang.Thread.UncaughtExceptionHandler;
import java.net.Proxy;
import java.util.Collections;
Expand All @@ -22,7 +22,7 @@
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class Bugsnag {
public class Bugsnag implements Closeable {
private static final Logger LOGGER = LoggerFactory.getLogger(Bugsnag.class);
private static final int SHUTDOWN_TIMEOUT_MS = 5000;
private static final int SESSION_TRACKING_PERIOD_MS = 60000;
Expand All @@ -46,7 +46,7 @@ public Thread newThread(Runnable runnable) {

private ScheduledThreadPoolExecutor sessionExecutorService =
new ScheduledThreadPoolExecutor(CORE_POOL_SIZE,
new DaemonThreadFactory(),
Executors.defaultThreadFactory(),
new RejectedExecutionHandler() {
@Override
public void rejectedExecution(Runnable runnable, ThreadPoolExecutor executor) {
Expand Down Expand Up @@ -120,20 +120,7 @@ private void addSessionTrackingShutdownHook() {
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
sessionTracker.shutdown();
sessionExecutorService.shutdown();
try {
if (!sessionExecutorService
.awaitTermination(SHUTDOWN_TIMEOUT_MS, TimeUnit.MILLISECONDS)) {
LOGGER.warn("Shutdown of 'session tracking' threads"
+ " took too long - forcing a shutdown");
sessionExecutorService.shutdownNow();
}
} catch (InterruptedException ex) {
LOGGER.warn("Shutdown of 'session tracking' thread "
+ "was interrupted - forcing a shutdown");
sessionExecutorService.shutdownNow();
}
close();
}
});
}
Expand Down Expand Up @@ -596,12 +583,20 @@ public void setEndpoints(String notify, String sessions) throws IllegalArgumentE
/**
* Close the connection to Bugsnag and unlink the exception handler.
*/
@Override
public void close() {
LOGGER.debug("Closing connection to Bugsnag");
ExceptionHandler.disable(this);

// runs periodic checks, should shut down immediately as don't need to send any sessions
sessionExecutorService.shutdownNow();

// flush remaining sessions
sessionTracker.shutdown();

if (config.delivery != null) {
config.delivery.close();
}
ExceptionHandler.disable(this);
}

// Thread metadata
Expand Down
32 changes: 0 additions & 32 deletions bugsnag/src/main/java/com/bugsnag/util/DaemonThreadFactory.java

This file was deleted.

33 changes: 0 additions & 33 deletions bugsnag/src/test/java/com/bugsnag/DaemonThreadFactoryTest.java

This file was deleted.