1
0
Fork 0
mirror of https://github.com/syncthing/syncthing-android.git synced 2024-12-02 01:01:17 +00:00

Fix shutdown during pending startup of the runtime (#34)

This commit is contained in:
Catfriend1 2018-08-25 23:09:09 +02:00 committed by GitHub
parent 8591f4245f
commit c44996012a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 6 deletions

View file

@ -266,7 +266,7 @@ public class SyncthingRunnable implements Runnable {
* Look for running libsyncthing.so processes and return an array * Look for running libsyncthing.so processes and return an array
* containing the PIDs of found instances. * containing the PIDs of found instances.
*/ */
private List<String> getSyncthingPIDs() { private List<String> getSyncthingPIDs(Boolean enableLog) {
List<String> syncthingPIDs = new ArrayList<String>(); List<String> syncthingPIDs = new ArrayList<String>();
Process ps = null; Process ps = null;
DataOutputStream psOut = null; DataOutputStream psOut = null;
@ -283,7 +283,9 @@ public class SyncthingRunnable implements Runnable {
while ((line = br.readLine()) != null) { while ((line = br.readLine()) != null) {
if (line.contains(Constants.FILENAME_SYNCTHING_BINARY)) { if (line.contains(Constants.FILENAME_SYNCTHING_BINARY)) {
String syncthingPID = line.trim().split("\\s+")[1]; String syncthingPID = line.trim().split("\\s+")[1];
if (enableLog) {
Log.v(TAG, "getSyncthingPIDs: Found process PID [" + syncthingPID + "]"); Log.v(TAG, "getSyncthingPIDs: Found process PID [" + syncthingPID + "]");
}
syncthingPIDs.add(syncthingPID); syncthingPIDs.add(syncthingPID);
} }
} }
@ -330,7 +332,7 @@ public class SyncthingRunnable implements Runnable {
return; return;
} }
List<String> syncthingPIDs = getSyncthingPIDs(); List<String> syncthingPIDs = getSyncthingPIDs(false);
if (syncthingPIDs.isEmpty()) { if (syncthingPIDs.isEmpty()) {
Log.i(TAG_NICE, "Found no running instances of " + Constants.FILENAME_SYNCTHING_BINARY); Log.i(TAG_NICE, "Found no running instances of " + Constants.FILENAME_SYNCTHING_BINARY);
return; return;
@ -350,7 +352,7 @@ public class SyncthingRunnable implements Runnable {
*/ */
public void killSyncthing() { public void killSyncthing() {
int exitCode; int exitCode;
List<String> syncthingPIDs = getSyncthingPIDs(); List<String> syncthingPIDs = getSyncthingPIDs(true);
if (syncthingPIDs.isEmpty()) { if (syncthingPIDs.isEmpty()) {
Log.d(TAG, "killSyncthing: Found no running instances of " + Constants.FILENAME_SYNCTHING_BINARY); Log.d(TAG, "killSyncthing: Found no running instances of " + Constants.FILENAME_SYNCTHING_BINARY);
return; return;
@ -369,7 +371,7 @@ public class SyncthingRunnable implements Runnable {
* Wait for the syncthing instance to end. * Wait for the syncthing instance to end.
*/ */
Log.v(TAG, "Waiting for all syncthing instances to end ..."); Log.v(TAG, "Waiting for all syncthing instances to end ...");
while (!getSyncthingPIDs().isEmpty()) { while (!getSyncthingPIDs(false).isEmpty()) {
SystemClock.sleep(50); SystemClock.sleep(50);
} }
Log.v(TAG, "killSyncthing: Complete."); Log.v(TAG, "killSyncthing: Complete.");

View file

@ -7,6 +7,7 @@ import android.content.SharedPreferences;
import android.Manifest; import android.Manifest;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Handler; import android.os.Handler;
import android.os.SystemClock;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat; import android.support.v4.content.ContextCompat;
import android.util.Log; 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. * Sets {@link #mCurrentState} to newState, and calls onKilledListener once Syncthing is killed.
*/ */
private void shutdown(State newState, OnSyncthingKilled onKilledListener) { 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) { synchronized(mStateLock) {
onServiceStateChange(newState); onServiceStateChange(newState);
} }