1
0
Fork 0
mirror of https://github.com/M66B/FairEmail.git synced 2024-12-26 09:47:13 +00:00

Fixed race condition

This commit is contained in:
M66B 2018-08-14 14:25:21 +00:00
parent badbd134f5
commit afb7668d6e

View file

@ -72,6 +72,7 @@ import java.util.Properties;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.Semaphore;
import javax.mail.Address;
import javax.mail.FetchProfile;
@ -1557,31 +1558,25 @@ public class ServiceSynchronize extends LifecycleService {
public static void stopSynchronous(Context context, String reason) {
Log.i(Helper.TAG, "Stop because of '" + reason + "'");
final Object lock = new Object();
final Semaphore semaphore = new Semaphore(0, true);
ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder binder) {
Log.i(Helper.TAG, "Service connected");
((LocalBinder) binder).getService().quit();
synchronized (lock) {
lock.notify();
}
semaphore.release();
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
Log.i(Helper.TAG, "Service disconnected");
synchronized (lock) {
lock.notify();
}
semaphore.release();
}
@Override
public void onBindingDied(ComponentName name) {
Log.i(Helper.TAG, "Service died");
synchronized (lock) {
lock.notify();
}
semaphore.release();
}
};
@ -1592,9 +1587,7 @@ public class ServiceSynchronize extends LifecycleService {
if (exists) {
Log.i(Helper.TAG, "Service stopping");
try {
synchronized (lock) {
lock.wait();
}
semaphore.acquire();
} catch (InterruptedException ex) {
Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
}