diff --git a/app/src/main/java/com/nutomic/syncthingandroid/service/SyncthingRunnable.java b/app/src/main/java/com/nutomic/syncthingandroid/service/SyncthingRunnable.java index da830fac..6c873e06 100644 --- a/app/src/main/java/com/nutomic/syncthingandroid/service/SyncthingRunnable.java +++ b/app/src/main/java/com/nutomic/syncthingandroid/service/SyncthingRunnable.java @@ -266,7 +266,7 @@ public class SyncthingRunnable implements Runnable { * Look for running libsyncthing.so processes and return an array * containing the PIDs of found instances. */ - private List getSyncthingPIDs() { + private List getSyncthingPIDs(Boolean enableLog) { List syncthingPIDs = new ArrayList(); Process ps = null; DataOutputStream psOut = null; @@ -283,7 +283,9 @@ public class SyncthingRunnable implements Runnable { while ((line = br.readLine()) != null) { if (line.contains(Constants.FILENAME_SYNCTHING_BINARY)) { String syncthingPID = line.trim().split("\\s+")[1]; - Log.v(TAG, "getSyncthingPIDs: Found process PID [" + syncthingPID + "]"); + if (enableLog) { + Log.v(TAG, "getSyncthingPIDs: Found process PID [" + syncthingPID + "]"); + } syncthingPIDs.add(syncthingPID); } } @@ -330,7 +332,7 @@ public class SyncthingRunnable implements Runnable { return; } - List syncthingPIDs = getSyncthingPIDs(); + List syncthingPIDs = getSyncthingPIDs(false); if (syncthingPIDs.isEmpty()) { Log.i(TAG_NICE, "Found no running instances of " + Constants.FILENAME_SYNCTHING_BINARY); return; @@ -350,7 +352,7 @@ public class SyncthingRunnable implements Runnable { */ public void killSyncthing() { int exitCode; - List syncthingPIDs = getSyncthingPIDs(); + List syncthingPIDs = getSyncthingPIDs(true); if (syncthingPIDs.isEmpty()) { Log.d(TAG, "killSyncthing: Found no running instances of " + Constants.FILENAME_SYNCTHING_BINARY); return; @@ -369,7 +371,7 @@ public class SyncthingRunnable implements Runnable { * Wait for the syncthing instance to end. */ Log.v(TAG, "Waiting for all syncthing instances to end ..."); - while (!getSyncthingPIDs().isEmpty()) { + while (!getSyncthingPIDs(false).isEmpty()) { SystemClock.sleep(50); } Log.v(TAG, "killSyncthing: Complete."); diff --git a/app/src/main/java/com/nutomic/syncthingandroid/service/SyncthingService.java b/app/src/main/java/com/nutomic/syncthingandroid/service/SyncthingService.java index 4fa34aa9..89c4ec45 100644 --- a/app/src/main/java/com/nutomic/syncthingandroid/service/SyncthingService.java +++ b/app/src/main/java/com/nutomic/syncthingandroid/service/SyncthingService.java @@ -7,6 +7,7 @@ import android.content.SharedPreferences; import android.Manifest; import android.os.AsyncTask; import android.os.Handler; +import android.os.SystemClock; import android.support.annotation.Nullable; import android.support.v4.content.ContextCompat; import android.util.Log; @@ -494,7 +495,15 @@ public class SyncthingService extends Service { * Sets {@link #mCurrentState} to newState, and calls onKilledListener once Syncthing is killed. */ private void shutdown(State newState, OnSyncthingKilled onKilledListener) { - Log.i(TAG, "Shutting down background service"); + if (mCurrentState == State.STARTING) { + Log.w(TAG, "Deferring shutdown until State.STARTING was left"); + mHandler.postDelayed(() -> { + shutdown(newState, onKilledListener); + }, 1000); + return; + } + + Log.i(TAG, "Shutting down"); synchronized(mStateLock) { onServiceStateChange(newState); }