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
Merged
Show file tree
Hide file tree
Changes from 4 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

## 3.4.2 (TBD)

* Prevent application hangs due to session flushing
[#121](https://github.com/bugsnag/bugsnag-java/pull/121)

## 3.4.1

(Skipped, duplicate of 3.4.0)
Expand Down
24 changes: 22 additions & 2 deletions bugsnag/src/main/java/com/bugsnag/Bugsnag.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
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;
Expand All @@ -12,6 +13,9 @@
import java.util.Collections;
import java.util.Date;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadPoolExecutor;
Expand All @@ -23,8 +27,17 @@ public class Bugsnag {
private static final int SESSION_TRACKING_PERIOD_MS = 60000;
private static final int CORE_POOL_SIZE = 1;

// Create an executor service which keeps idle threads alive for a maximum of SHUTDOWN_TIMEOUT.
// This should avoid blocking an application that doesn't call shutdown from exiting.
private ExecutorService sessionFlusherService =
new ThreadPoolExecutor(0, 1,
SHUTDOWN_TIMEOUT_MS, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());

private ScheduledThreadPoolExecutor sessionExecutorService =
new ScheduledThreadPoolExecutor(CORE_POOL_SIZE, new RejectedExecutionHandler() {
new ScheduledThreadPoolExecutor(CORE_POOL_SIZE,
new DaemonThreadFactory(),
new RejectedExecutionHandler() {
@Override
public void rejectedExecution(Runnable runnable, ThreadPoolExecutor executor) {
LOGGER.error("Rejected execution for sessionExecutorService");
Expand Down Expand Up @@ -81,7 +94,14 @@ private void scheduleSessionFlushes() {
sessionExecutorService.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
sessionTracker.flushSessions(new Date());
// Use a different thread which is not a daemon thread
// to actually flush the sessions
sessionFlusherService.submit(new Runnable() {
@Override
public void run() {
sessionTracker.flushSessions(new Date());
}
});
}
}, SESSION_TRACKING_PERIOD_MS, SESSION_TRACKING_PERIOD_MS, TimeUnit.MILLISECONDS);
}
Expand Down
31 changes: 31 additions & 0 deletions bugsnag/src/main/java/com/bugsnag/util/DaemonThreadFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.bugsnag.util;

import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

/**
* Wraps {@link Executors#defaultThreadFactory()} to return daemon threads
* This is to prevent applications from hanging waiting for the sessions scheduled task
* because daemon threads will be terminated on application shutdown
*/
public class DaemonThreadFactory implements ThreadFactory {
private final ThreadFactory defaultThreadFactory;

/**
* Constructor
*/
public DaemonThreadFactory() {
defaultThreadFactory = Executors.defaultThreadFactory();
}

@Override
public Thread newThread(Runnable runner) {
Thread daemonThread = defaultThreadFactory.newThread(runner);

// Set the threads to daemon to allow the app to shutdown properly
if (!daemonThread.isDaemon()) {
daemonThread.setDaemon(true);
}
return daemonThread;
}
}
33 changes: 33 additions & 0 deletions bugsnag/src/test/java/com/bugsnag/DaemonThreadFactoryTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.bugsnag;

import static org.junit.Assert.assertTrue;

import com.bugsnag.util.DaemonThreadFactory;

import org.junit.Before;
import org.junit.Test;


/**
* Tests for the Daemon thread factory internal logic
*/
public class DaemonThreadFactoryTest {

private DaemonThreadFactory daemonThreadFactory;

/**
* Create the daemonThreadFactory before the tests
*/
@Before
public void createFactory() {
daemonThreadFactory = new DaemonThreadFactory();
}

@Test
public void testDaemonThreadFactory() {
Thread testThread = daemonThreadFactory.newThread(null);

// Check that the thread is as expected
assertTrue(testThread.isDaemon());
}
}