Skip to content

Commit 2ee8e51

Browse files
committed
fix(android): Stop the NDK crash service when its daemon exits
The service was started with `startService` but never stopped, so its process kept running after `Sentry.close()` or after the app process died. Call `stopSelf` with the start ID that was current when the worker started, so a newer start from a relaunched app is not stopped.
1 parent e34fbfe commit 2ee8e51

1 file changed

Lines changed: 6 additions & 2 deletions

File tree

‎sentry-android-ndk-outproc/src/main/java/io/sentry/android/ndk/SentryNdkCrashService.java‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public final class SentryNdkCrashService extends Service {
7272
private final @NotNull Object lock = new Object();
7373

7474
private @Nullable Thread daemonWorker;
75+
private int lastStartId = 0;
7576
private volatile boolean running = false;
7677
private @Nullable File nativeDatabaseDir;
7778
private @Nullable String nativeLibraryDir;
@@ -248,6 +249,7 @@ public int onStartCommand(final @Nullable Intent intent, final int flags, final
248249
}
249250

250251
synchronized (lock) {
252+
lastStartId = startId;
251253
startWorkersLocked();
252254
}
253255
return START_NOT_STICKY;
@@ -459,12 +461,13 @@ private void startWorkersLocked() {
459461
running = true;
460462
}
461463
if (daemonWorker == null && isDaemonConfigured()) {
462-
daemonWorker = new Thread(this::runDaemonWorker, "SentryNdkCrashService");
464+
final int startId = lastStartId;
465+
daemonWorker = new Thread(() -> runDaemonWorker(startId), "SentryNdkCrashService");
463466
daemonWorker.start();
464467
}
465468
}
466469

467-
private void runDaemonWorker() {
470+
private void runDaemonWorker(final int startId) {
468471
final SentryAndroidOptions options = new SentryAndroidOptions();
469472
options.setLogger(new AndroidLogger("SentryNdkCrash"));
470473
options.setDebug(debug);
@@ -522,6 +525,7 @@ private void runDaemonWorker() {
522525
appTid = 0;
523526
shmPath = null;
524527
}
528+
stopSelf(startId);
525529
}
526530
}
527531

0 commit comments

Comments
 (0)