From 8e1371acf0d4101abb0b1983520200aeac0df9f4 Mon Sep 17 00:00:00 2001 From: akwizgran Date: Mon, 28 Apr 2025 18:34:54 +0100 Subject: [PATCH] Keep foreground service until lifecycle shutdown completes. This ensures our background threads keep running. --- .../briar/android/BriarService.java | 39 +++++++++++-------- .../controller/BriarControllerImpl.java | 2 +- 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/briar-android/src/main/java/org/briarproject/briar/android/BriarService.java b/briar-android/src/main/java/org/briarproject/briar/android/BriarService.java index 6fe6df44c..3639a02f1 100644 --- a/briar-android/src/main/java/org/briarproject/briar/android/BriarService.java +++ b/briar-android/src/main/java/org/briarproject/briar/android/BriarService.java @@ -217,19 +217,14 @@ public class BriarService extends Service { @Override public void onDestroy() { - // Hold a wake lock during shutdown - wakeLockManager.runWakefully(() -> { - super.onDestroy(); - LOG.info("Destroyed"); - stopForeground(true); - if (receiver != null) { - getApplicationContext().unregisterReceiver(receiver); - } - // Stop the services in a background thread - wakeLockManager.executeWakefully(() -> { - if (started) lifecycleManager.stopServices(); - }, "LifecycleShutdown"); - }, "LifecycleShutdown"); + super.onDestroy(); + LOG.info("Destroyed"); + // Stop the lifecycle, if not already stopped + shutdown(false); + stopForeground(true); + if (receiver != null) { + getApplicationContext().unregisterReceiver(receiver); + } } @Override @@ -299,8 +294,8 @@ public class BriarService extends Service { private void shutdownFromBackground() { // Hold a wake lock during shutdown wakeLockManager.runWakefully(() -> { - // Stop the service - stopSelf(); + // Begin lifecycle shutdown + shutdown(true); // Hide the UI hideUi(); // Wait for shutdown to complete, then exit @@ -335,8 +330,18 @@ public class BriarService extends Service { /** * Starts the shutdown process. */ - public void shutdown() { - stopSelf(); // This will call onDestroy() + public void shutdown(boolean stopAndroidService) { + // Hold a wake lock during shutdown + wakeLockManager.runWakefully(() -> { + // Stop the lifecycle services in a background thread, + // then stop this Android service if needed + wakeLockManager.executeWakefully(() -> { + if (started) lifecycleManager.stopServices(); + if (stopAndroidService) { + androidExecutor.runOnUiThread(() -> stopSelf()); + } + }, "LifecycleShutdown"); + }, "LifecycleShutdown"); } public class BriarBinder extends Binder { diff --git a/briar-android/src/main/java/org/briarproject/briar/android/controller/BriarControllerImpl.java b/briar-android/src/main/java/org/briarproject/briar/android/controller/BriarControllerImpl.java index 34f4edcee..0993bbe75 100644 --- a/briar-android/src/main/java/org/briarproject/briar/android/controller/BriarControllerImpl.java +++ b/briar-android/src/main/java/org/briarproject/briar/android/controller/BriarControllerImpl.java @@ -147,7 +147,7 @@ public class BriarControllerImpl implements BriarController { service.waitForStartup(); // Shut down the service and wait for it to shut down LOG.info("Shutting down service"); - service.shutdown(); + service.shutdown(true); service.waitForShutdown(); } catch (InterruptedException e) { LOG.warning("Interrupted while waiting for service");