fix(android): keep Tor bootstrap alive after the unlock activity dies
Some checks failed
android-ci / android (pull_request) Has been cancelled

Open set Phase.Opening then ran openWire on viewModelScope, and the
keep-alive refused to start until Wire existed. Closing the activity
cancelled bootstrap, so the spinner vanished and the lock screen came
back idle. Run openWire on NodeHost's process scope, start the FGS on
the Open tap, and treat in-flight bootstrap as keep-alive work. IME Go
also submits unlock the same way as the Open button.
This commit is contained in:
agent 2026-09-18 21:46:34 -04:00
commit 396e468f1a
No known key found for this signature in database
9 changed files with 226 additions and 30 deletions

View file

@ -15,6 +15,7 @@ import com.siriusdevops.onionwire.wallet.ChainStorage
import com.siriusdevops.onionwire.wallet.NO_WALLET_RPC_MESSAGE
import com.siriusdevops.onionwire.wallet.WalletChildBinaries
import com.siriusdevops.onionwire.wallet.WalletChildBinaryPaths
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.currentCoroutineContext
@ -151,10 +152,13 @@ class WireViewModel(app: Application) : AndroidViewModel(app) {
// is already open would be theatre, and unlocking again would race the
// live node for the sqlite file.
val existing = NodeHost.current()
if (existing != null) {
viewModelScope.launch { adopt(existing) }
} else {
refreshUnlockState()
when {
existing != null -> viewModelScope.launch { adopt(existing) }
NodeHost.isOpening() -> {
_phase.value = Phase.Opening
viewModelScope.launch { resumeOpening() }
}
else -> refreshUnlockState()
}
viewModelScope.launch {
@ -308,17 +312,23 @@ class WireViewModel(app: Application) : AndroidViewModel(app) {
return
}
_phase.value = Phase.Opening
NodeHost.noteOpening()
// Hold the process from this tap, not after Tor publishes: otherwise
// closing the activity cancels viewModelScope and kills bootstrap.
KeepAliveService.start(getApplication(), "")
viewModelScope.launch {
try {
// The SDK open is dispatched to Dispatchers.IO inside NodeHost:
// cold start is minutes of Tor bootstrap, which the main thread
// does not survive.
val w = NodeHost.open(home, secret.concatToString())
if (storeAfterwards) store(secret)
adopt(w)
startKeepAlive(w)
} catch (t: CancellationException) {
throw t
} catch (t: Throwable) {
_phase.value = Phase.Failed(t.message ?: t.toString())
if (!NodeHost.hasWorkForKeepAlive()) {
KeepAliveService.stop(getApplication())
}
} finally {
StoreSecret.wipe(secret)
}
@ -338,6 +348,19 @@ class WireViewModel(app: Application) : AndroidViewModel(app) {
runCatching { refreshFriends() }
}
/** Re-attach after the activity died mid-bootstrap. */
private suspend fun resumeOpening() {
try {
val w = NodeHost.awaitOpen()
adopt(w)
startKeepAlive(w)
} catch (t: CancellationException) {
throw t
} catch (t: Throwable) {
_phase.value = Phase.Failed(t.message ?: t.toString())
}
}
/**
* Starts the foreground keep-alive. Always reached from a user-visible
* unlock, so the API 31+ background-start restriction does not apply; if