Skip to content

Commit 13378cb

Browse files
committed
stop holding keepalive lock while waiting for shutdown
the keepalive thread may be in the middle of reconnecting, which means it tries to get the keepalive lock, which can cause deadlock.
1 parent bcf7a43 commit 13378cb

File tree

1 file changed

+13
-20
lines changed

1 file changed

+13
-20
lines changed

src/main/java/com/github/shyiko/mysql/binlog/BinaryLogClient.java

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -794,17 +794,13 @@ public void run() {
794794
}
795795
}
796796
if (connectionLost) {
797-
if (logger.isLoggable(Level.INFO)) {
798-
logger.info("Trying to restore lost connection to " + hostname + ":" + port);
799-
}
797+
logger.info("Keepalive: Trying to restore lost connection to " + hostname + ":" + port);
800798
try {
801799
terminateConnect();
802800
connect(connectTimeout);
803801
} catch (Exception ce) {
804-
if (logger.isLoggable(Level.WARNING)) {
805-
logger.warning("Failed to restore connection to " + hostname + ":" + port +
806-
". Next attempt in " + keepAliveInterval + "ms");
807-
}
802+
logger.warning("keepalive: Failed to restore connection to " + hostname + ":" + port +
803+
". Next attempt in " + keepAliveInterval + "ms");
808804
}
809805
}
810806
}
@@ -1179,19 +1175,16 @@ public void disconnect() throws IOException {
11791175
}
11801176

11811177
private void terminateKeepAliveThread() {
1182-
try {
1183-
keepAliveThreadExecutorLock.lock();
1184-
ExecutorService keepAliveThreadExecutor = this.keepAliveThreadExecutor;
1185-
if (keepAliveThreadExecutor == null) {
1186-
return;
1187-
}
1188-
keepAliveThreadExecutor.shutdownNow();
1189-
while (!awaitTerminationInterruptibly(keepAliveThreadExecutor,
1190-
Long.MAX_VALUE, TimeUnit.NANOSECONDS)) {
1191-
// ignore
1192-
}
1193-
} finally {
1194-
keepAliveThreadExecutorLock.unlock();
1178+
keepAliveThreadExecutorLock.lock();
1179+
ExecutorService keepAliveThreadExecutor = this.keepAliveThreadExecutor;
1180+
if (keepAliveThreadExecutor == null) {
1181+
return;
1182+
}
1183+
keepAliveThreadExecutor.shutdownNow();
1184+
keepAliveThreadExecutorLock.unlock();
1185+
while (!awaitTerminationInterruptibly(keepAliveThreadExecutor,
1186+
Long.MAX_VALUE, TimeUnit.NANOSECONDS)) {
1187+
// ignore
11951188
}
11961189
}
11971190

0 commit comments

Comments
 (0)