Fix race condition

This commit is contained in:
Andrea Cavalli 2020-11-15 20:39:45 +01:00
parent 83561e1d1b
commit c156313fee
2 changed files with 11 additions and 10 deletions

View File

@ -1,7 +1,5 @@
package it.tdlight.common; package it.tdlight.common;
import it.tdlight.jni.TdApi;
public abstract class CommonClientManager { public abstract class CommonClientManager {
private static InternalClientManager getClientManager(String implementationName) { private static InternalClientManager getClientManager(String implementationName) {
@ -34,8 +32,6 @@ public abstract class CommonClientManager {
} }
private static TelegramClient create(InternalClient internalClient) { private static TelegramClient create(InternalClient internalClient) {
// Send a dummy request because @levlam is too lazy to fix race conditions in a better way
internalClient.send(new TdApi.GetAuthorizationState(), null, null);
return internalClient; return internalClient;
} }
} }

View File

@ -44,6 +44,8 @@ public class InternalClient implements ClientEventsHandler, TelegramClient {
UpdatesHandler updatesHandler, UpdatesHandler updatesHandler,
ExceptionHandler updateExceptionHandler, ExceptionHandler updateExceptionHandler,
ExceptionHandler defaultExceptionHandler) { ExceptionHandler defaultExceptionHandler) {
clientInitializationLock.writeLock().lock();
try {
this.updateHandler = null; this.updateHandler = null;
this.updatesHandler = new MultiHandler(updatesHandler, updateExceptionHandler); this.updatesHandler = new MultiHandler(updatesHandler, updateExceptionHandler);
this.clientManager = clientManager; this.clientManager = clientManager;
@ -51,6 +53,9 @@ public class InternalClient implements ClientEventsHandler, TelegramClient {
this.clientId = NativeClientAccess.create(); this.clientId = NativeClientAccess.create();
clientManager.registerClient(clientId, this); clientManager.registerClient(clientId, this);
} finally {
clientInitializationLock.writeLock().unlock();
}
} }
@Override @Override