feat(sdk): explicit Tor lifecycle — Wire.shutdown / flush / torState + observable state #49
Merged
sirius
merged 3 commits from 2026-09-12 20:58:31 +00:00
feat/tor-lifecycle into main 2026-09-12
docs(sdk): Wire::shutdown doc said the second call returns Ok; it returns Stopped
All checks were successful
ci / test (pull_request) Successful in 4m22s
The handle-level call takes the node out on the first shutdown, so a second call reports the typed `WireError::Stopped` (covered by `calls_after_shutdown_are_typed_errors_not_panics`). Only the node-level `Node::shutdown()` is Ok-idempotent. The rustdoc claimed otherwise.
feat(android): stop the node when the UI goes away
`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.
feat(core,sdk): explicit Tor lifecycle — stop, flush, observable state
A node had no way to stop. `Wire` was a bare handle over a `Node`: dropping the
Kotlin reference left Arti's client, the hosted v3 onion service, the outbox
retry worker and the sqlite connection alive, and there was no way to ask where
the onion service was. On Android the process is killed without warning by Doze
and the background-process killer, so "drop everything when the process dies" is
not a lifecycle.
Core (`src/node.rs`, `src/hs.rs`):
* `Node::shutdown()` — bounded outbox drain (10s), stop the onion service (the
rendezvous task is aborted and the RunningOnionService dropped), abort the
retry worker, checkpoint the sqlite WAL. Idempotent at this level: the flag is
swapped first, so a racing second call is a no-op.
* `Node::flush() -> FlushOutcome { attempted, delivered, remaining }` — one
delivery attempt for every queued outbox row, ignoring backoff, then a WAL
checkpoint. Bounded by 30s because a dead peer's onion holds a dial for ~20s
and the caller is a UI thread. A timeout leaves rows in `sending`;
`Outbox::reset_stale` requeues them on the next unlock, same as a process that
died mid-attempt.
* `Node::tor_state() -> TorState` (`Bootstrapping` / `Published` /
`DegradedReachable` / `Stopped`) and `hs::tor_state_of(state, stopped,
reachable)`, which mirrors `hs_is_ready` exactly instead of re-deriving
readiness.
* Every network entry point (`send`, `send_once`, `connect_onion`, `rotate`,
`flush`) fails with a typed error after the node stops, rather than dialing a
dead client.
* `checkpoint_wal` reaches the database through a short-lived second connection:
`Store` owns the write connection and does not lend it out, and a checkpoint is
a property of the database, not of one connection. Best effort — WAL replay on
open recovers anything committed.
The `WorkerHandle` moved into a `Mutex<Option<...>>` so the teardown can *abort*
the retry worker instead of only dropping it when the whole `Node` dies.
SDK (`crates/onionwire-sdk/src/lib.rs`):
* `Wire.shutdown()`, `Wire.flush(): FlushOutcome`, `Wire.torState(): TorState`.
`Wire` now holds `Mutex<Option<Arc<Node>>>`, so teardown drops the last
reference to the Tor client and the store instead of keeping them alive behind
a JNI handle.
* `WireError::Stopped` — a second flat variant, so "you must unlock again" is a
typed `WireException.Stopped` in Kotlin, not a string match on `Failed`.
* No `unwrap()` on a poisoned mutex anywhere: a poisoned handle reports
`Stopped`, which is the correct recovery (unlock again).
* Fixes the crate not compiling on this base at all: `Node::send` has returned
`Result<OutboxStatus, String>` since the outbox landed, and the SDK still
mapped it to `Result<(), String>` (E0308). CI's path allowlist excludes
`crates/**` from the Rust job, which is why it stayed green.
The live Tor test found a real bug in the first version of `tor_state`: Arti's
combined status is still `Bootstrapping` after a *successful* probe of our own
onion (that mismatch is what the 360s publish wait exists for), and a freshly
launched service reports `Shutdown` before its publish reactor starts — so a
node that was live and reachable reported `Bootstrapping`, and briefly
`Stopped`, right after unlock. `Node` now tracks an `onion_reachable` flag set
when the publish bar is crossed and cleared when `rotate` hard-cuts the service,
and `Shutdown`/`Broken` on a live node read `Bootstrapping` (transient) rather
than terminal.