feat(android): stop the node when the UI goes away
All checks were successful
ci / test (pull_request) Successful in 3m38s
All checks were successful
ci / test (pull_request) Successful in 3m38s
`WireViewModel.lock()` set `wire = null` and `onCleared()` did not exist at all, so neither the user pressing Lock nor the Activity finishing stopped anything: Arti kept its client and the hosted onion service, the outbox worker kept dialing, and the sqlite connection stayed open behind the JNI handle. Both paths now go through `stopNode(Wire)`: 1. `flush()` — one last bounded drain, so a queued message gets its chance before the process may lose its socket. 2. `shutdown()` — drain again with a shorter budget, stop the onion service, abort the retry worker, checkpoint the WAL, and take the node handle out of the `Wire` (which is what releases the last Arc to the Tor client and the store). Both run on a process-scoped `TEARDOWN` scope (`SupervisorJob` + `Dispatchers.IO`) because `onCleared()` runs *after* `viewModelScope` is already cancelled. Errors are deliberately swallowed there: the UI is gone, and the SDK reports a second teardown as the typed `WireException.Stopped` rather than a panic. `unlock()` joins any in-flight teardown before opening again. The old node holds the sqlite file and Arti's state lock until its teardown returns, and racing it makes the next `openWire` fail. A process killed outright (Doze, background-process killer, force-stop) still never runs any of this — that is what the sqlite outbox and the new `flush()` are for. The foreground-service card (`t_5bf32cd3`) moves this ownership from the ViewModel to the service.
This commit is contained in:
parent
201aa48344
commit
3d41ffceb3
1 changed files with 56 additions and 2 deletions
|
|
@ -3,6 +3,10 @@ package com.siriusdevops.onionwire
|
|||
import android.app.Application
|
||||
import androidx.lifecycle.AndroidViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.SupervisorJob
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
|
|
@ -32,6 +36,14 @@ sealed interface Phase {
|
|||
data class Failed(val message: String) : Phase
|
||||
}
|
||||
|
||||
/**
|
||||
* Teardown work must outlive the ViewModel: `onCleared()` runs *after*
|
||||
* `viewModelScope` is already cancelled, and stopping the node means draining
|
||||
* the outbox and shutting Arti down, not just dropping a reference. One
|
||||
* process-wide scope is enough — there is one node per process.
|
||||
*/
|
||||
private val TEARDOWN = CoroutineScope(SupervisorJob() + Dispatchers.IO)
|
||||
|
||||
/**
|
||||
* Owns the single Wire node for the process.
|
||||
*
|
||||
|
|
@ -51,6 +63,13 @@ class WireViewModel(app: Application) : AndroidViewModel(app) {
|
|||
|
||||
private var wire: Wire? = null
|
||||
|
||||
/**
|
||||
* The in-flight teardown, if any. Unlocking again has to wait for it: the
|
||||
* old node holds the sqlite file and Arti's state lock until `shutdown()`
|
||||
* returns, and racing it makes the new node fail to open.
|
||||
*/
|
||||
private var stopping: Job? = null
|
||||
|
||||
/**
|
||||
* Opens (or on first run, creates) the identity and starts hosting.
|
||||
*
|
||||
|
|
@ -58,10 +77,12 @@ class WireViewModel(app: Application) : AndroidViewModel(app) {
|
|||
* it is not a "skip" path.
|
||||
*/
|
||||
fun unlock(passphrase: String) {
|
||||
if (_phase.value is Phase.Opening) return
|
||||
if (_phase.value is Phase.Opening || wire != null) return
|
||||
_phase.value = Phase.Opening
|
||||
viewModelScope.launch {
|
||||
try {
|
||||
// Any previous node must be fully stopped first.
|
||||
stopping?.join()
|
||||
val w = openWire(home.absolutePath, passphrase)
|
||||
wire = w
|
||||
_phase.value = Phase.Ready(
|
||||
|
|
@ -76,11 +97,44 @@ class WireViewModel(app: Application) : AndroidViewModel(app) {
|
|||
}
|
||||
}
|
||||
|
||||
/** Drops our handle. The onion service stops; nothing is sent after this. */
|
||||
/**
|
||||
* Stops the node and drops our handle. Nothing is sent after this.
|
||||
*
|
||||
* Flush first — the outbox is the whole point of losing the process — then
|
||||
* `shutdown()`, which stops the onion service, aborts the retry worker and
|
||||
* checkpoints the sqlite WAL. Both calls are on [TEARDOWN] because this
|
||||
* runs from an `onClick`.
|
||||
*/
|
||||
fun lock() {
|
||||
val w = wire ?: return
|
||||
wire = null
|
||||
_friends.value = emptyList()
|
||||
_phase.value = Phase.Locked
|
||||
stopping = TEARDOWN.launch { stopNode(w) }
|
||||
}
|
||||
|
||||
/**
|
||||
* The node must not outlive the UI. Doze and the background-process killer
|
||||
* skip this entirely (that is what the outbox is for), but a normal finish
|
||||
* tears Tor down here instead of leaving Arti running behind a dead screen.
|
||||
*/
|
||||
override fun onCleared() {
|
||||
val w = wire
|
||||
wire = null
|
||||
if (w != null) stopping = TEARDOWN.launch { stopNode(w) }
|
||||
super.onCleared()
|
||||
}
|
||||
|
||||
private suspend fun stopNode(w: Wire) {
|
||||
// Best effort, both steps: teardown runs on a process-scope job after
|
||||
// the UI is gone, so there is nobody to show an error to and nothing
|
||||
// useful to retry. `shutdown()` drains again with a shorter budget, so
|
||||
// a flush that times out is not fatal.
|
||||
runCatching { w.flush() }
|
||||
// A second shutdown — `lock()` and then `onCleared()`, say — lands here
|
||||
// too: the SDK reports it as a typed WireException.Stopped, never a
|
||||
// panic, so swallowing it is correct.
|
||||
runCatching { w.shutdown() }
|
||||
}
|
||||
|
||||
private fun requireWire(): Wire = wire ?: error("locked")
|
||||
|
|
|
|||
Loading…
Reference in a new issue