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.
This commit is contained in:
parent
c39259b919
commit
38fde75d40
5 changed files with 772 additions and 25 deletions
58
src/hs.rs
58
src/hs.rs
|
|
@ -96,6 +96,64 @@ pub fn hs_is_ready(state: State, probe_ok: bool) -> bool {
|
|||
}
|
||||
}
|
||||
|
||||
/// Observable lifecycle state of the node's onion service.
|
||||
///
|
||||
/// This is the four-state projection callers actually act on: is the onion
|
||||
/// service serving, is it serving but unhealthy, is it still coming up, or did
|
||||
/// we stop it. A process that gets killed by the Android background-process
|
||||
/// killer never observes `Stopped` — that is why `Wire.flush()` exists — but a
|
||||
/// clean teardown always does.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum TorState {
|
||||
/// Bootstrap or HsDir descriptor upload is still in flight: the service is
|
||||
/// not reliably reachable yet.
|
||||
Bootstrapping,
|
||||
/// The onion service is running and its descriptor is published.
|
||||
Published,
|
||||
/// Reachable through the service, but Arti reports the service degraded.
|
||||
/// Usable, worth surfacing, not identical to `Published`.
|
||||
DegradedReachable,
|
||||
/// The node was shut down: no onion service, no client dials, and the
|
||||
/// store is checkpointed.
|
||||
Stopped,
|
||||
}
|
||||
|
||||
/// Project an Arti onion-service status onto [`TorState`].
|
||||
///
|
||||
/// Deliberately mirrors `hs_is_ready`: `Running` and `DegradedReachable` are
|
||||
/// the states a probe could already reach, everything transient is
|
||||
/// `Bootstrapping`, and a shut-down service is `Stopped`.
|
||||
///
|
||||
/// `reachable` is the same evidence `wait_until_published` accepts from a
|
||||
/// probe: a successful connect to our own onion means some HsDir already has
|
||||
/// the descriptor even though Arti's combined status is still `Bootstrapping`
|
||||
/// (that mismatch is exactly what the 360s publish wait exists for). Without
|
||||
/// it, a node that is live and reachable would report `Bootstrapping` for
|
||||
/// another few minutes.
|
||||
///
|
||||
/// Two statuses look terminal but are not:
|
||||
/// * `Shutdown` on a *live* node is transient — a freshly launched service
|
||||
/// reports it before its publish reactor starts, which is why a live node
|
||||
/// reads `Bootstrapping` there and only the explicit stop flag produces
|
||||
/// `Stopped`. (Caught on device-real conditions by the live test: the
|
||||
/// first `tor_state()` after `start()` returned `Shutdown`.)
|
||||
/// * `Broken` maps to `Bootstrapping` rather than a state of its own: Arti
|
||||
/// reads `Broken` while the HsDir retry loop is still going, and
|
||||
/// `wait_until_published` is where a genuinely dead service surfaces as an
|
||||
/// error instead of a flag the UI would have to poll for.
|
||||
pub fn tor_state_of(state: State, stopped: bool, reachable: bool) -> TorState {
|
||||
if stopped {
|
||||
return TorState::Stopped;
|
||||
}
|
||||
match state {
|
||||
State::Running => TorState::Published,
|
||||
State::DegradedReachable => TorState::DegradedReachable,
|
||||
State::Broken | State::Shutdown => TorState::Bootstrapping,
|
||||
_ if reachable => TorState::Published,
|
||||
_ => TorState::Bootstrapping,
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn wait_until_published(
|
||||
client: &Client,
|
||||
svc: &RunningOnionService,
|
||||
|
|
|
|||
Loading…
Reference in a new issue