From c068e292a002e524753f8cb9259092a85c454f5c Mon Sep 17 00:00:00 2001 From: apk-dev Date: Sat, 12 Sep 2026 14:29:39 -0400 Subject: [PATCH] feat(android): stop the node when the UI goes away MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `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. --- .../siriusdevops/onionwire/WireViewModel.kt | 59 ++++++++++++++++++- 1 file changed, 57 insertions(+), 2 deletions(-) diff --git a/android/app/src/main/java/com/siriusdevops/onionwire/WireViewModel.kt b/android/app/src/main/java/com/siriusdevops/onionwire/WireViewModel.kt index 93bc1ef..140c494 100644 --- a/android/app/src/main/java/com/siriusdevops/onionwire/WireViewModel.kt +++ b/android/app/src/main/java/com/siriusdevops/onionwire/WireViewModel.kt @@ -7,7 +7,10 @@ import com.siriusdevops.onionwire.crypto.KeyBacking import com.siriusdevops.onionwire.crypto.KeystorePassphraseCipher import com.siriusdevops.onionwire.crypto.StoreSecret import com.siriusdevops.onionwire.crypto.StoredPassphraseStore +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 @@ -55,6 +58,14 @@ sealed interface Unlock { data class OneTap(val backing: KeyBacking) : Unlock } +/** + * Teardown work must outlive the ViewModel: `onCleared()` runs *after* + * `viewModelScope` is already cancelled, and stopping the node means draining + * the outbox and stopping Arti, 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. * @@ -87,6 +98,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 + init { // Probe on a background dispatcher: this touches the Keystore and does // a full GCM decrypt. Doing it here means a killed Keystore key is @@ -143,12 +161,45 @@ 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 refreshUnlockState() + 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 refreshUnlockState() { @@ -180,13 +231,17 @@ class WireViewModel(app: Application) : AndroidViewModel(app) { * follow-up SDK signature this card cannot make itself. */ private fun openWith(secret: CharArray, storeAfterwards: Boolean) { - if (secret.isEmpty() || _phase.value is Phase.Opening) { + if (secret.isEmpty() || _phase.value is Phase.Opening || wire != null) { StoreSecret.wipe(secret) return } _phase.value = Phase.Opening viewModelScope.launch { try { + // Any previous node must be fully stopped first: the old node + // holds the sqlite file and Arti's state lock until its teardown + // returns, and racing it makes this open fail. + stopping?.join() val w = openWire(home.absolutePath, secret.concatToString()) wire = w if (storeAfterwards) store(secret)