Skip to content

Commit 8b48712

Browse files
committed
Don't retry commands submitted by the onConnect handler.
Also fixes a deadlock when reconnecting after a disconnect during the onConnect handler.
1 parent f265bc4 commit 8b48712

File tree

1 file changed

+22
-19
lines changed

1 file changed

+22
-19
lines changed

src/main/java/com/github/theholywaffle/teamspeak3/TS3Query.java

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -227,14 +227,11 @@ synchronized void doCommandAsync(Command c) {
227227
}
228228

229229
void submitUserTask(final String name, final Runnable task) {
230-
userThreadPool.submit(new Runnable() {
231-
@Override
232-
public void run() {
233-
try {
234-
task.run();
235-
} catch (Throwable throwable) {
236-
log.error(name + " threw an exception", throwable);
237-
}
230+
userThreadPool.submit(() -> {
231+
try {
232+
task.run();
233+
} catch (Throwable throwable) {
234+
log.error(name + " threw an exception", throwable);
238235
}
239236
});
240237
}
@@ -250,21 +247,18 @@ FileTransferHelper getFileTransferHelper() {
250247
void fireDisconnect() {
251248
connected.set(false);
252249

253-
submitUserTask("ConnectionHandler disconnect task", new Runnable() {
254-
@Override
255-
public void run() {
256-
handleDisconnect();
257-
}
258-
});
250+
submitUserTask("ConnectionHandler disconnect task", this::handleDisconnect);
259251
}
260252

261-
private synchronized void handleDisconnect() {
253+
private void handleDisconnect() {
262254
try {
263255
connectionHandler.onDisconnect(this);
264256
} finally {
265-
if (!connected.get()) {
266-
shuttingDown.set(true); // Try to prevent extraneous exit commands
267-
shutDown();
257+
synchronized (this) {
258+
if (!connected.get()) {
259+
shuttingDown.set(true); // Try to prevent extraneous exit commands
260+
shutDown();
261+
}
268262
}
269263
}
270264
}
@@ -281,12 +275,21 @@ private ReconnectQuery(TS3Query query, QueryIO io) {
281275

282276
@Override
283277
public void connect() {
284-
throw new UnsupportedOperationException("Can't call connect from onConnect");
278+
throw new UnsupportedOperationException("Can't call connect from onConnect handler");
285279
}
286280

287281
@Override
288282
public void exit() {
289283
parent.exit();
290284
}
285+
286+
@Override
287+
synchronized void fireDisconnect() {
288+
// If a reconnect query fails, we do not want this to affect the parent query.
289+
// Instead, simply fail all remaining onConnect commands.
290+
QueryIO io = super.io;
291+
io.disconnect();
292+
io.failRemainingCommands();
293+
}
291294
}
292295
}

0 commit comments

Comments
 (0)