Skip to content

Commit 75fef46

Browse files
authored
fix: Increase threadpool count to avoid deadlocks (#1391)
To refresh a Cloud SQL Instance's certificates, the current algorithm uses 2 threads from the thread pool for each instance. Because the thread pool is fixed size, if a user configures 2 or more instances, they were at risk of a causing thread starvation and a deadlock. This increases the thread count to a safe level so that most users will never experience a deadlock. Fixes #1314
1 parent f7c9598 commit 75fef46

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

core/src/main/java/com/google/cloud/sql/core/CoreSocketFactory.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,14 @@ public static synchronized CoreSocketFactory getInstance() {
120120
@VisibleForTesting
121121
// Returns a listenable, scheduled executor that exits upon shutdown.
122122
static ListeningScheduledExecutorService getDefaultExecutor() {
123-
// TODO(kvg): Figure out correct way to determine number of threads
123+
124+
// During refresh, each instance consumes 2 threads from the thread pool. By using 8 threads,
125+
// there should be enough free threads so that there will not be a deadlock. Most users
126+
// configure 3 or fewer instances, requiring 6 threads during refresh. By setting
127+
// this to 8, it's enough threads for most users, plus a safety factor of 2.
124128
ScheduledThreadPoolExecutor executor =
125-
(ScheduledThreadPoolExecutor) Executors.newScheduledThreadPool(2);
129+
(ScheduledThreadPoolExecutor) Executors.newScheduledThreadPool(8);
130+
126131
executor.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
127132
return MoreExecutors.listeningDecorator(
128133
MoreExecutors.getExitingScheduledExecutorService(executor));

0 commit comments

Comments
 (0)