Remove duplicate shutdown hooks

This commit is contained in:
Andrea Cavalli 2021-12-08 11:51:18 +01:00
parent b27c736866
commit 39bbe60e50
4 changed files with 6 additions and 45 deletions

View File

@ -26,7 +26,7 @@
<dependency> <dependency>
<groupId>it.tdlight</groupId> <groupId>it.tdlight</groupId>
<artifactId>tdlight-java-bom</artifactId> <artifactId>tdlight-java-bom</artifactId>
<version>2.7.10.0</version> <version>2.7.10.1</version>
<type>pom</type> <type>pom</type>
<scope>import</scope> <scope>import</scope>
</dependency> </dependency>

View File

@ -205,18 +205,6 @@ public final class SimpleTelegramClient implements Authenticable {
this.authenticationData = authenticationData; this.authenticationData = authenticationData;
createDirectories(); createDirectories();
client.initialize(this::handleUpdate, this::handleUpdateException, this::handleDefaultException); client.initialize(this::handleUpdate, this::handleUpdateException, this::handleDefaultException);
// Handle unexpected shutdown
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
try {
// Send close function
this.client.send(new TdApi.Close(), ok -> {}, ex -> {});
// Wait until the client has been closed successfully
this.waitForExit();
} catch (Throwable ignored) {
// Ignore errors since we are shutting down everything
}
}));
} }
private void createDirectories() { private void createDirectories() {

View File

@ -60,15 +60,7 @@ public final class InternalClientManager implements AutoCloseable {
} }
return val; return val;
}); });
if (clientManager.startIfNeeded()) { clientManager.startIfNeeded();
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
try {
clientManager.onJVMShutdown();
} catch (InterruptedException ex) {
logger.error("Failed to close", ex);
}
}));
}
return clientManager; return clientManager;
} }
@ -159,10 +151,6 @@ public final class InternalClientManager implements AutoCloseable {
} }
} }
private void onJVMShutdown() throws InterruptedException {
responseReceiver.onJVMShutdown();
}
private static final class DroppedEvent { private static final class DroppedEvent {
private final long id; private final long id;

View File

@ -21,7 +21,6 @@ import java.util.concurrent.atomic.AtomicBoolean;
abstract class ResponseReceiver extends Thread implements AutoCloseable { abstract class ResponseReceiver extends Thread implements AutoCloseable {
private static final String FLAG_PAUSE_SHUTDOWN_UNTIL_ALL_CLOSED = "it.tdlight.pauseShutdownUntilAllClosed";
private static final String FLAG_USE_OPTIMIZED_DISPATCHER = "tdlight.dispatcher.use_optimized_dispatcher"; private static final String FLAG_USE_OPTIMIZED_DISPATCHER = "tdlight.dispatcher.use_optimized_dispatcher";
private static final boolean USE_OPTIMIZED_DISPATCHER private static final boolean USE_OPTIMIZED_DISPATCHER
= Boolean.parseBoolean(System.getProperty(FLAG_USE_OPTIMIZED_DISPATCHER, "true")); = Boolean.parseBoolean(System.getProperty(FLAG_USE_OPTIMIZED_DISPATCHER, "true"));
@ -36,7 +35,6 @@ abstract class ResponseReceiver extends Thread implements AutoCloseable {
private final AtomicBoolean startCalled = new AtomicBoolean(); private final AtomicBoolean startCalled = new AtomicBoolean();
private final AtomicBoolean closeCalled = new AtomicBoolean(); private final AtomicBoolean closeCalled = new AtomicBoolean();
private final AtomicBoolean jvmShutdown = new AtomicBoolean();
private final EventsHandler eventsHandler; private final EventsHandler eventsHandler;
private final int[] clientIds = new int[MAX_EVENTS]; private final int[] clientIds = new int[MAX_EVENTS];
@ -56,7 +54,7 @@ abstract class ResponseReceiver extends Thread implements AutoCloseable {
public ResponseReceiver(EventsHandler eventsHandler) { public ResponseReceiver(EventsHandler eventsHandler) {
super("TDLib thread"); super("TDLib thread");
this.eventsHandler = eventsHandler; this.eventsHandler = eventsHandler;
this.setDaemon(true); this.setDaemon(false);
} }
/** /**
@ -82,7 +80,6 @@ abstract class ResponseReceiver extends Thread implements AutoCloseable {
boolean interrupted; boolean interrupted;
while ( while (
!(interrupted = Thread.interrupted()) !(interrupted = Thread.interrupted())
&& !jvmShutdown.get()
&& (!closeCalled.get() || !registeredClients.isEmpty()) && (!closeCalled.get() || !registeredClients.isEmpty())
) { ) {
// Timeout is expressed in seconds // Timeout is expressed in seconds
@ -224,11 +221,9 @@ abstract class ResponseReceiver extends Thread implements AutoCloseable {
} }
} }
if (interrupted) { if (interrupted || closeCalled.get()) {
if (!jvmShutdown.get()) { for (Integer clientId : this.registeredClients) {
for (Integer clientId : this.registeredClients) { eventsHandler.handleClientEvents(clientId, true, clientEventIds, clientEvents, 0, 0);
eventsHandler.handleClientEvents(clientId, true, clientEventIds, clientEvents, 0, 0);
}
} }
} }
} finally { } finally {
@ -287,14 +282,4 @@ abstract class ResponseReceiver extends Thread implements AutoCloseable {
throw new IllegalStateException("Start not called"); throw new IllegalStateException("Start not called");
} }
} }
public void onJVMShutdown() throws InterruptedException {
if (startCalled.get()) {
if (this.jvmShutdown.compareAndSet(false, true)) {
if (Boolean.parseBoolean(System.getProperty(FLAG_PAUSE_SHUTDOWN_UNTIL_ALL_CLOSED, "true"))) {
this.closeWait.await();
}
}
}
}
} }