Check if main thread alive

This commit is contained in:
M66B 2019-09-23 14:42:13 +02:00
parent 4ef10cd7d0
commit cbb81e6c31
1 changed files with 18 additions and 14 deletions

View File

@ -84,10 +84,9 @@ import static android.os.Process.THREAD_PRIORITY_BACKGROUND;
public class ServiceSynchronize extends ServiceBase { public class ServiceSynchronize extends ServiceBase {
private ConnectionHelper.NetworkState networkState = new ConnectionHelper.NetworkState(); private ConnectionHelper.NetworkState networkState = new ConnectionHelper.NetworkState();
private Core.State state; private Core.State state = null;
private int lastStartId = -1; private int lastStartId = -1;
private boolean started = false; private boolean started = false;
private boolean stopped = false;
private int queued = 0; private int queued = 0;
private long lastLost = 0; private long lastLost = 0;
private TupleAccountStats lastStats = new TupleAccountStats(); private TupleAccountStats lastStats = new TupleAccountStats();
@ -104,6 +103,7 @@ public class ServiceSynchronize extends ServiceBase {
private static final int BACKOFF_ERROR_AFTER = 16; // seconds private static final int BACKOFF_ERROR_AFTER = 16; // seconds
private static final long ONESHOT_DURATION = 90 * 1000L; // milliseconds private static final long ONESHOT_DURATION = 90 * 1000L; // milliseconds
private static final long STOP_DELAY = 5000L; // milliseconds private static final long STOP_DELAY = 5000L; // milliseconds
private static final long CHECK_ALIVE_INTERVAL = 19 * 60 * 1000L; // milliseconds
static final int PI_ALARM = 1; static final int PI_ALARM = 1;
static final int PI_ONESHOT = 2; static final int PI_ONESHOT = 2;
@ -281,9 +281,6 @@ public class ServiceSynchronize extends ServiceBase {
public void onDestroy() { public void onDestroy() {
EntityLog.log(this, "Service destroy"); EntityLog.log(this, "Service destroy");
if (!stopped)
Log.e("Service destroy without stop");
unregisterReceiver(onScreenOff); unregisterReceiver(onScreenOff);
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
@ -291,6 +288,11 @@ public class ServiceSynchronize extends ServiceBase {
setUnseen(null); setUnseen(null);
if (state != null && state.isRunning()) {
Log.e("Destroy while running");
state.stop();
}
try { try {
stopForeground(true); stopForeground(true);
} catch (Throwable ex) { } catch (Throwable ex) {
@ -327,7 +329,6 @@ public class ServiceSynchronize extends ServiceBase {
@Override @Override
public int onStartCommand(Intent intent, int flags, int startId) { public int onStartCommand(Intent intent, int flags, int startId) {
lastStartId = startId; lastStartId = startId;
stopped = false;
String action = (intent == null ? null : intent.getAction()); String action = (intent == null ? null : intent.getAction());
Log.i("Service command intent=" + intent + " action=" + action); Log.i("Service command intent=" + intent + " action=" + action);
Log.logExtras(intent); Log.logExtras(intent);
@ -545,10 +546,8 @@ public class ServiceSynchronize extends ServiceBase {
Thread.sleep(STOP_DELAY); Thread.sleep(STOP_DELAY);
} catch (InterruptedException ignored) { } catch (InterruptedException ignored) {
} }
if (queued == 0 && !isEnabled()) { if (queued == 0 && !isEnabled())
stopped = true;
stopService(lastStartId); stopService(lastStartId);
}
} }
wl.release(); wl.release();
@ -570,6 +569,8 @@ public class ServiceSynchronize extends ServiceBase {
private void start(final boolean sync) { private void start(final boolean sync) {
EntityLog.log(this, "Main start"); EntityLog.log(this, "Main start");
final Thread main = Thread.currentThread();
state = new Core.State(networkState); state = new Core.State(networkState);
state.runnable(new Runnable() { state.runnable(new Runnable() {
private PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); private PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
@ -624,7 +625,12 @@ public class ServiceSynchronize extends ServiceBase {
try { try {
wl.release(); wl.release();
state.acquire(); while (!state.acquire(CHECK_ALIVE_INTERVAL)) {
if (!main.isAlive()) {
Log.e("Main thread died");
state.stop();
}
}
} catch (InterruptedException ex) { } catch (InterruptedException ex) {
Log.w("main wait " + ex.toString()); Log.w("main wait " + ex.toString());
} finally { } finally {
@ -1170,8 +1176,7 @@ public class ServiceSynchronize extends ServiceBase {
try { try {
unregisterReceiver(alarm); unregisterReceiver(alarm);
} catch (IllegalArgumentException ex) { } catch (IllegalArgumentException ex) {
Log.w(account.name, new IllegalStateException("Killed", ex)); Log.e(ex);
reload(ServiceSynchronize.this, "killed");
} }
} }
@ -1270,8 +1275,7 @@ public class ServiceSynchronize extends ServiceBase {
try { try {
unregisterReceiver(alarm); unregisterReceiver(alarm);
} catch (IllegalArgumentException ex) { } catch (IllegalArgumentException ex) {
Log.w(account.name, new IllegalStateException("Killed", ex)); Log.e(ex);
reload(ServiceSynchronize.this, "killed");
} }
} }
} }