fix(android): close button now actually stops the node service
The close button called stopService() and then app.exit(0) immediately. stopService() is asynchronous — it posts to ActivityManager and onDestroy runs later on the main thread — so the process often died mid-flight. Android saw a foreground service whose process had died and, per onStartCommand returning START_STICKY, restarted it. Whether the clean path won was pure timing, which is why this worked when v0.7.3 shipped and stopped working under v0.8's heavier startup and shutdown. Nothing in gen/android had changed since v0.7.3 (verified: the diff is empty) — this was a latent race, not a regression. Fix, in two layers: - NodeService persists a user_quit flag to SharedPreferences (commit(), not apply(), since the process may die moments later). stopFromNative sets it BEFORE calling stopService. onStartCommand treats a null intent as "Android resurrected me" and, if the flag is set, calls stopForeground(STOP_FOREGROUND_REMOVE) + stopSelf() and returns START_NOT_STICKY. MainActivity clears the flag on genuine launch, so a system-initiated kill still resumes via START_STICKY — the case that behaviour exists for. - exit_app awaits 400ms between the stop and app.exit(0) so the clean path usually wins; the persisted flag is the backstop when it doesn't. Not rebuilt — batching for 0.8.1. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LGiPD2cF75mnvneSCjdDC5
This commit is contained in:
parent
3232796d4d
commit
bbc17f8855
3 changed files with 49 additions and 1 deletions
|
|
@ -21,6 +21,10 @@ class MainActivity : TauriActivity() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Genuine launch — clear any prior "user quit" flag so the service is
|
||||||
|
// allowed to run (and to resume via START_STICKY) again.
|
||||||
|
NodeService.setUserQuit(this, false)
|
||||||
|
|
||||||
// Start foreground service to keep connections alive in background
|
// Start foreground service to keep connections alive in background
|
||||||
val serviceIntent = Intent(this, NodeService::class.java)
|
val serviceIntent = Intent(this, NodeService::class.java)
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,28 @@ class NodeService : Service() {
|
||||||
const val CHANNEL_ID = "itsgoin_node"
|
const val CHANNEL_ID = "itsgoin_node"
|
||||||
const val NOTIFICATION_ID = 1
|
const val NOTIFICATION_ID = 1
|
||||||
|
|
||||||
|
// Survives process death, unlike any in-memory flag. START_STICKY
|
||||||
|
// means Android resurrects this service when its process dies —
|
||||||
|
// desirable when the SYSTEM kills us in the background, wrong when
|
||||||
|
// the USER explicitly quit. stopService() is asynchronous, so a
|
||||||
|
// fast app.exit(0) can kill the process before the stop is
|
||||||
|
// processed, and the resurrection races ahead of it. This flag is
|
||||||
|
// how a restarted service learns the quit already happened.
|
||||||
|
private const val PREFS = "itsgoin_service"
|
||||||
|
private const val KEY_USER_QUIT = "user_quit"
|
||||||
|
|
||||||
|
@JvmStatic
|
||||||
|
fun setUserQuit(context: Context, quit: Boolean) {
|
||||||
|
context.getSharedPreferences(PREFS, Context.MODE_PRIVATE)
|
||||||
|
.edit()
|
||||||
|
.putBoolean(KEY_USER_QUIT, quit)
|
||||||
|
.commit() // synchronous — the process may die moments from now
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun userQuit(context: Context): Boolean =
|
||||||
|
context.getSharedPreferences(PREFS, Context.MODE_PRIVATE)
|
||||||
|
.getBoolean(KEY_USER_QUIT, false)
|
||||||
|
|
||||||
// Called via JNI from Rust when the user taps the in-app close
|
// Called via JNI from Rust when the user taps the in-app close
|
||||||
// button. Foreground services survive Activity exit by design
|
// button. Foreground services survive Activity exit by design
|
||||||
// (keeps connections alive when backgrounded). When the user
|
// (keeps connections alive when backgrounded). When the user
|
||||||
|
|
@ -25,6 +47,9 @@ class NodeService : Service() {
|
||||||
// service in addition to ending the Activity.
|
// service in addition to ending the Activity.
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun stopFromNative(context: Context) {
|
fun stopFromNative(context: Context) {
|
||||||
|
// Record the intent to quit BEFORE asking the service to stop,
|
||||||
|
// so a sticky restart that beats us to it still sees the flag.
|
||||||
|
setUserQuit(context, true)
|
||||||
val intent = Intent(context, NodeService::class.java)
|
val intent = Intent(context, NodeService::class.java)
|
||||||
context.stopService(intent)
|
context.stopService(intent)
|
||||||
}
|
}
|
||||||
|
|
@ -54,6 +79,16 @@ class NodeService : Service() {
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||||
|
// A null intent means Android resurrected us under START_STICKY
|
||||||
|
// rather than MainActivity starting us. If the user had quit, honour
|
||||||
|
// that: stop immediately and do not come back. MainActivity clears
|
||||||
|
// the flag whenever the app is genuinely launched, so a system-kill
|
||||||
|
// restart (the case START_STICKY exists for) still resumes.
|
||||||
|
if (intent == null && userQuit(this)) {
|
||||||
|
stopForeground(STOP_FOREGROUND_REMOVE)
|
||||||
|
stopSelf()
|
||||||
|
return START_NOT_STICKY
|
||||||
|
}
|
||||||
return START_STICKY
|
return START_STICKY
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1161,7 +1161,16 @@ async fn exit_app(app: tauri::AppHandle) {
|
||||||
// explicitly hits the in-app close button, also stop the service
|
// explicitly hits the in-app close button, also stop the service
|
||||||
// so we actually free the device's network/wakelock.
|
// so we actually free the device's network/wakelock.
|
||||||
#[cfg(target_os = "android")]
|
#[cfg(target_os = "android")]
|
||||||
itsgoin_core::android_wifi::stop_node_service();
|
{
|
||||||
|
itsgoin_core::android_wifi::stop_node_service();
|
||||||
|
// stopService() is asynchronous: it posts to ActivityManager and the
|
||||||
|
// service's onDestroy runs afterwards on the main thread. Exiting
|
||||||
|
// immediately kills the process mid-flight, which Android treats as
|
||||||
|
// "foreground service died" and restarts under START_STICKY. Give the
|
||||||
|
// clean path a moment to win; NodeService's persisted user-quit flag
|
||||||
|
// is the backstop for when it doesn't.
|
||||||
|
tokio::time::sleep(std::time::Duration::from_millis(400)).await;
|
||||||
|
}
|
||||||
|
|
||||||
app.exit(0);
|
app.exit(0);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue