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

[fix] ScheduledExecutors use daemon threads, to avoid blocking shutdowns #989

Merged
merged 2 commits into from
Mar 26, 2019
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,14 @@ public final class OkHttpClients {
+ "which requires debugging.",
uncaughtException))
.setNameFormat("remoting-okhttp-dispatcher-%d")
// The Dispatcher must *not* use daemon threads otherwise it would become impossible to make
// outgoing network requests in a shutdown hook.
.setDaemon(false)
.build();
/**
* The {@link ExecutorService} used for the {@link Dispatcher}s of all OkHttp clients created through this class.
* Same as OkHttp's default, but with a logging uncaught exception handler.
* Same as OkHttp's default, but with a logging uncaught exception handler. In a cachedThreadPool, threads that
* have not been used for sixty seconds are terminated, so this won't block shutdown indefinitely.
*/
private static final ExecutorService executionExecutor =
Tracers.wrap(Executors.newCachedThreadPool(executionThreads));
Expand Down Expand Up @@ -103,18 +107,18 @@ public final class OkHttpClients {
*/
private static final ScheduledExecutorService limitReviver = Tracers.wrap(
Executors.newSingleThreadScheduledExecutor(
Util.threadFactory("conjure-java-runtime/leaked limit reviver", false)));
Util.threadFactory("conjure-java-runtime/leaked limit reviver", true)));

/**
* The {@link ScheduledExecutorService} used for scheduling call retries. This thread pool is distinct from OkHttp's
* internal thread pool and from the thread pool used by {@link #executionExecutor}.
* <p>
* Note: In contrast to the {@link java.util.concurrent.ThreadPoolExecutor} used by OkHttp's {@link
* #executionExecutor}, {@code corePoolSize} must not be zero for a {@link ScheduledThreadPoolExecutor}, see its
* Javadoc.
* Javadoc. Since this executor will never hit zero threads, it must use daemon threads.
*/
private static final ScheduledExecutorService schedulingExecutor = Tracers.wrap(Executors.newScheduledThreadPool(
NUM_SCHEDULING_THREADS, Util.threadFactory("conjure-java-runtime/OkHttp Scheduler", false)));
NUM_SCHEDULING_THREADS, Util.threadFactory("conjure-java-runtime/OkHttp Scheduler", true)));
Copy link
Contributor

Choose a reason for hiding this comment

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

there are other executors in this class; you probably want to daemonize all of them!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

maybe, but this is the one that is causing me troubles

Copy link
Contributor

Choose a reason for hiding this comment

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

Please either update all executors, or add comments describing why they need to use non-daemon threads

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok, better?

Copy link
Contributor

Choose a reason for hiding this comment

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

Why doesn't this apply to the cached executor which also uses non-daemon threads? If anything has made a remote call I imagine that would block shutdown for a full minute as well even if actions are not running.

If we haven't made calls using conjure-java-runtiem clients, it's odd that we would create this executor. Should creation of these executors be deferred until a client is created using memoized suppliers?


private OkHttpClients() {}

Expand Down