commit 12ebab67c3c5927bb19ca204706fe60fd9d97283 parent ac314b506b831f844589d79363bc742cc0a93b36 Author: triesap <tyson@radroots.org> Date: Sat, 17 Oct 2026 05:56:20 +0000 sdk: retain issued Nostr calls across profile replacement - Separate new-call admission from disposal of busy transport pools. - Bound current and retained pools to sixteen relay reservations. - Disconnect registered relays on completion and retain retryable cleanup. - Prove unchanged late ACK behavior, retained suites, APIs and coverage. Diffstat:
15 files changed, 696 insertions(+), 53 deletions(-)
diff --git a/contracts/api_baselines/radroots_transport_nostr.txt b/contracts/api_baselines/radroots_transport_nostr.txt @@ -83,7 +83,9 @@ pub fn radroots_transport_nostr::Config::write_relays(&self) -> impl core::iter: pub struct radroots_transport_nostr::NostrTransport impl radroots_transport_nostr::NostrTransport pub fn radroots_transport_nostr::NostrTransport::admission_is_closed(&self) -> bool +pub fn radroots_transport_nostr::NostrTransport::close_admission(&self) pub const fn radroots_transport_nostr::NostrTransport::config(&self) -> &radroots_transport_nostr::Config +pub fn radroots_transport_nostr::NostrTransport::has_in_flight_operations(&self) -> bool pub fn radroots_transport_nostr::NostrTransport::new(radroots_transport_nostr::Config) -> Self pub fn radroots_transport_nostr::NostrTransport::relay_status(&self) -> radroots_transport_nostr::RelayStatusReport pub async fn radroots_transport_nostr::NostrTransport::shutdown(&self) diff --git a/contracts/architecture/deviations.toml b/contracts/architecture/deviations.toml @@ -2,6 +2,32 @@ schema_version = 1 architecture_id = "radroots.crates.release.v1" [[deviation]] +id = "RCRV1-DEV-047" +date = "2026-10-17" +status = "closed" +approval = "The user authorizes the complete shared SDK refactor, preservation of retained behavior, verified source checkpoints and master publication." +affected_steps = ["040", "070", "110", "130"] +spec_anchors = ["contracts/crates/release_v1/radroots_crates_release_v1.toml#package.radroots_sdk"] +source_evidence = [ + "The actual Tera retained late-ACK configuration test fails against published Lib ac314b50: replacement closes an already-issued EVENT socket, causing BrokenPipe before its accepted observation can be retained. The same unfiltered run passes both ten-thousand-item cases, 488 core cases, and retains three existing child-only ignores.", + "The native slot owns private Arc-backed adapter selections that remain borrowed throughout its admitted source/sink futures. New configuration must close admission while preserving already-started attempt observations, and all current and retained pools must remain owned and bounded until explicit cleanup.", +] +replacement_action = "Separate concrete admission closure from pool shutdown. Count source/delivery/subscription-setup/AUTH calls, register actual relay handles before connecting, and disconnect synchronously after the last closed-admission call completes or is cancelled. Retain old private adapter selections for already-started calls. Reclaim idle retired pools before installation, enforce sixteen relay reservations across current and retained pools, refuse over-budget transitions explicitly, and preserve owned state across cleanup cancellation. Full explicit close disposes every owned pool before storage. Add no detached worker, synchronous alias, unbounded retirement list or post-observation await." +verification = [ + "Bind exact clean provider source, all601 unchanged lock records and the failed actual consumer source/test result before implementation; validate this prospective record first.", + "Prove native retirement, admission refusal, aggregate resource bounds, idle reclamation, interrupted cleanup and full disposal. Keep obtained observations and every original test assertion intact.", + "Pass the unchanged real Tera late-ACK case against the primary candidate with any temporary verification patch explicit and removed, then qualify the exact published successor source through its actual consumer pins and owning artifacts. Require native/Linux retained tests, strict lint/docs, owner-produced API freshness, unchanged coverage policy and current contracts before source publication.", +] +unresolved_risk = "Retained pools are bounded session-owned resources, not proof of complete worker joining or durable operation-result supervision. Those later obligations and final browser/binding/device qualification remain open. Published ac314b50 history is preserved; its consumer candidate is unqualified until this repair passes." +normative_architecture_change = false +adr_required = false +closure_evidence = [ + "Closed admission retains already-started source/delivery/subscription-setup/AUTH calls. Actual relay handles are registered before connection and disconnect synchronously on last completion or cancellation. The slot keeps strong ownership of busy old pools and enforces sixteen relay reservations across replacement and retained selections; explicit close disposes all pools before storage. No worker, synchronous alias or result-losing cleanup await is introduced.", + "The unchanged actual Tera late-ACK regression passes all six memory/SQLite configuration-removal variants against this primary local candidate through an explicit temporary provider patch that is removed afterwards. Four new connection and four new SDK retirement cases prove admission, bounded/poisoned registration, resource refusal, idle reclamation, interrupted cleanup, socket disposal and retry. The retained native and Linux suites include every prior case and doctest.", + "Strict lint/docs, current facade/native binding compilation, all four generated public API baselines, freshly measured unchanged Nostr/SDK coverage gates, architecture, catalog, contracts and local preflight pass. Generic portable checks and other preflight payloads retain their exact earlier scope. Actual published Git consumer adoption and artifacts, complete worker joining, durable operation supervision, browser/bindings and final qualification remain open.", +] + +[[deviation]] id = "RCRV1-DEV-046" date = "2026-10-17" status = "closed" diff --git a/crates/sdk/README.md b/crates/sdk/README.md @@ -125,11 +125,18 @@ Native mobile hosts can retain one client while changing host-owned identity and relay selection. The host injects one opaque implementation of the canonical `radroots_signing::Signer` SPI; the SDK has no mutable secret slot and accepts no secret string. Await `transport::NostrSlot::configure` or -`Client::configure_nostr` to validate a relay set, shut down its predecessor, +`Client::configure_nostr` to validate a relay set, close prior admission, and install the replacement without opening a new connection. Transitions -are serialized. During cleanup, capability observations report unavailable; -interrupted cleanup retains the old selection for an explicit retry. -`NostrSlot::clear` also awaits pool disposal. `NostrSlot::shutdown` permanently +are serialized. Already-started calls retain their original pool and exact +observations; their last completion or cancellation disconnects its registered +relays synchronously. The slot owns retired pools until explicit cleanup. +The new and retained pools together reserve at most sixteen relays, counting +the same endpoint in separate pools twice. A larger new profile is invalid; +a busy aggregate budget is temporarily unavailable and requires an explicit +retry. Idle retired pools are disposed before the next installation. +During cleanup, capability observations report unavailable; interrupted +cleanup retains its owned resources for an explicit retry. +`NostrSlot::clear` also awaits current and retired pool disposal. `NostrSlot::shutdown` permanently closes admission across clones, so a retained handle cannot install another pool after client close. `Client::close` requests that shutdown before closing storage; failed storage close remains unavailable and retryable. Already diff --git a/crates/sdk/src/transport.rs b/crates/sdk/src/transport.rs @@ -1875,18 +1875,28 @@ impl Default for Profile { /// Host-configured, client-shareable Nostr transport slot. /// -/// Reconfiguration validates the complete relay set before atomically -/// replacing the active adapter. Construction, clearing, and target -/// inspection perform no network I/O. +/// Reconfiguration closes prior admission and retains already-started calls +/// within a sixteen-relay aggregate budget. Construction and target inspection +/// perform no network I/O; explicit cleanup awaits owned pool disposal. #[cfg(feature = "nostr")] #[derive(Clone)] pub struct NostrSlot { - state: Arc<RwLock<Option<NostrState>>>, + state: Arc<RwLock<NostrSlotState>>, transition: Arc<tokio::sync::Mutex<()>>, admission_closed: Arc<AtomicBool>, } #[cfg(feature = "nostr")] +const NOSTR_SESSION_RELAY_LIMIT: usize = 16; + +#[cfg(feature = "nostr")] +#[derive(Default)] +struct NostrSlotState { + current: Option<NostrState>, + retired: Vec<NostrState>, +} + +#[cfg(feature = "nostr")] #[derive(Clone)] struct NostrState { transport: Arc<radroots_transport_nostr::NostrTransport>, @@ -1926,22 +1936,28 @@ impl NostrSlot { #[must_use] pub fn new() -> Self { Self { - state: Arc::new(RwLock::new(None)), + state: Arc::new(RwLock::new(NostrSlotState::default())), transition: Arc::new(tokio::sync::Mutex::new(())), admission_closed: Arc::new(AtomicBool::new(false)), } } - /// Validates a profile, shuts down its predecessor and installs the new inert adapter. + /// Validates a profile and installs it while preserving already-started prior calls. /// - /// Cancellation during predecessor cleanup retains that selection for a - /// later retry, with its admission closed. No replacement is installed - /// after permanent shutdown begins. + /// Idle prior pools are disposed before installation. Busy prior pools have + /// closed admission and remain owned until later cleanup. Their last call + /// disconnects registered relays without an extra result-losing await. + /// The new and retained pools together may reserve at most sixteen relays. + /// Interrupted or over-budget cleanup leaves prior admission closed for an + /// explicit retry. Permanent shutdown always prevents a new installation. pub async fn configure(&self, profile: RelayProfile) -> crate::Result<()> { if self.admission_closed.load(Ordering::Acquire) { return Err(crate::Error::client_closed()); } let config = radroots_transport_nostr::Config::from_profile(profile); + if config.endpoints().len() > NOSTR_SESSION_RELAY_LIMIT { + return Err(crate::Error::invalid_host_configuration_without_source()); + } let read_targets = TargetSet::new( config .read_relays() @@ -1969,7 +1985,38 @@ impl NostrSlot { if self.admission_closed.load(Ordering::Acquire) { return Err(crate::Error::client_closed()); } - if let Some(previous) = self.cleanup_snapshot() { + self.cleanup_idle_retired().await?; + let previous = { + let mut state = self + .state + .write() + .map_err(|_| crate::Error::shared_operation_unavailable())?; + if let Some(previous) = &state.current { + previous.transport.close_admission(); + } + let retained = state + .retired + .iter() + .map(|previous| previous.transport.config().endpoints().len()) + .sum::<usize>(); + let busy = state + .current + .as_ref() + .filter(|previous| previous.transport.has_in_flight_operations()) + .map_or(0, |previous| previous.transport.config().endpoints().len()); + if retained + busy + config.endpoints().len() > NOSTR_SESSION_RELAY_LIMIT { + return Err(crate::Error::shared_operation_unavailable()); + } + if busy != 0 { + if let Some(previous) = state.current.take() { + state.retired.push(previous); + } + None + } else { + state.current.clone() + } + }; + if let Some(previous) = previous { previous.transport.shutdown().await; } let mut current = self @@ -1979,7 +2026,7 @@ impl NostrSlot { if self.admission_closed.load(Ordering::Acquire) { return Err(crate::Error::client_closed()); } - *current = Some(NostrState { + current.current = Some(NostrState { transport: Arc::new(radroots_transport_nostr::NostrTransport::new(config)), read_targets, write_targets, @@ -1987,20 +2034,61 @@ impl NostrSlot { Ok(()) } + async fn cleanup_idle_retired(&self) -> crate::Result<()> { + loop { + let idle = self + .state + .read() + .map_err(|_| crate::Error::shared_operation_unavailable())? + .retired + .iter() + .find(|previous| !previous.transport.has_in_flight_operations()) + .map(|previous| Arc::clone(&previous.transport)); + let Some(idle) = idle else { return Ok(()) }; + idle.shutdown().await; + let mut state = self + .state + .write() + .map_err(|_| crate::Error::shared_operation_unavailable())?; + if let Some(index) = state + .retired + .iter() + .position(|previous| Arc::ptr_eq(&previous.transport, &idle)) + { + state.retired.remove(index); + } + } + } + /// Shuts down the selected pool before removing its profile. /// /// Cancellation retains the selected pool so cleanup can be awaited again. pub async fn clear(&self) { let _transition = self.transition.lock().await; - if let Some(previous) = self.cleanup_snapshot() { - previous.transport.shutdown().await; + let pools = { + let state = self + .state + .read() + .unwrap_or_else(std::sync::PoisonError::into_inner); + state + .current + .iter() + .chain(state.retired.iter()) + .map(|previous| Arc::clone(&previous.transport)) + .collect::<Vec<_>>() + }; + for pool in &pools { + pool.close_admission(); + } + for pool in pools { + pool.shutdown().await; } // Poisoned state cannot authorize work, but its owned resources still // require disposal. Never reuse the recovered selection for admission. *self .state .write() - .unwrap_or_else(std::sync::PoisonError::into_inner) = None; + .unwrap_or_else(std::sync::PoisonError::into_inner) = NostrSlotState::default(); } /// Permanently closes admission and shuts down the selected pool across all clones. @@ -2035,10 +2123,12 @@ impl NostrSlot { self.operation_snapshot().ok().flatten() } + #[cfg(test)] fn cleanup_snapshot(&self) -> Option<NostrState> { self.state .read() .unwrap_or_else(std::sync::PoisonError::into_inner) + .current .clone() } @@ -2053,11 +2143,14 @@ impl NostrSlot { if self.admission_closed.load(Ordering::Acquire) { return Err(NostrSlotAccessError::Closed); } - let selected = self + let state = self .state .read() - .map_err(|_| NostrSlotAccessError::Unavailable)? - .clone(); + .map_err(|_| NostrSlotAccessError::Unavailable)?; + if state.current.is_none() && !state.retired.is_empty() { + return Err(NostrSlotAccessError::Unavailable); + } + let selected = state.current.clone(); if selected .as_ref() .is_some_and(|state| state.transport.admission_is_closed()) @@ -2405,6 +2498,10 @@ impl DaemonDelivery { } } +#[cfg(all(test, feature = "nostr"))] +#[path = "transport/nostr_retirement_tests.rs"] +mod nostr_retirement_tests; + #[cfg(test)] mod tests { use radroots_transport::{ diff --git a/crates/sdk/src/transport/nostr_retirement_tests.rs b/crates/sdk/src/transport/nostr_retirement_tests.rs @@ -0,0 +1,204 @@ +use super::*; +use radroots_transport::{EventSource, FetchPage, FetchRequest, source::FetchBounds}; +use std::{ + future::Future, + task::{Context, Poll, Waker}, + time::Duration, +}; +use tokio::{io::AsyncReadExt, net::TcpListener, sync::oneshot, task::JoinHandle}; + +fn profile(base: &str, count: usize) -> RelayProfile { + RelayProfile::explicit( + RelayProfileKind::Simulator, + (0..count).map(|index| { + RelayEndpoint::new( + format!("{base}/{index}"), + RelayUrlPolicy::Local, + RelayAccess::ReadWrite, + ) + .unwrap() + }), + ) + .unwrap() +} + +struct HeldFetch { + slot: NostrSlot, + previous: NostrState, + base: String, + fetch: JoinHandle<Result<FetchPage, radroots_transport::Error>>, + server: JoinHandle<()>, +} + +async fn held_fetch(relays: usize) -> HeldFetch { + let listener = TcpListener::bind("127.0.0.1:0").await.unwrap(); + let base = format!("ws://{}", listener.local_addr().unwrap()); + let (connected, ready) = oneshot::channel(); + let server = tokio::spawn(async move { + let (mut stream, _) = listener.accept().await.unwrap(); + let mut bytes = [0; 4096]; + assert!(stream.read(&mut bytes).await.unwrap() > 0); + connected.send(()).unwrap(); + // An admitted connection is held before upgrade. Only the exact /0 + // target is requested; the remaining profile reservations do no I/O. + loop { + match stream.read(&mut bytes).await { + Ok(0) | Err(_) => break, + Ok(_) => {} + } + } + }); + let slot = NostrSlot::new(); + slot.configure(profile(&base, relays)).await.unwrap(); + let previous = slot.cleanup_snapshot().unwrap(); + let target = RelayEndpoint::new( + format!("{base}/0"), + RelayUrlPolicy::Local, + RelayAccess::ReadWrite, + ) + .unwrap() + .url() + .to_target() + .unwrap(); + let request = FetchRequest::new( + "retirement-resource-check", + TargetSet::new(vec![target]).unwrap(), + FetchBounds::new(1, u64::MAX).unwrap(), + ) + .unwrap(); + let copy = slot.clone(); + let fetch = tokio::spawn(async move { copy.fetch(request).await }); + tokio::time::timeout(Duration::from_secs(2), ready) + .await + .unwrap() + .unwrap(); + assert!(previous.transport.has_in_flight_operations()); + HeldFetch { + slot, + previous, + base, + fetch, + server, + } +} + +#[tokio::test] +async fn an_oversized_new_profile_is_refused_before_changing_the_selection() { + let slot = NostrSlot::new(); + let base = "ws://127.0.0.1:19991"; + slot.configure(profile(base, NOSTR_SESSION_RELAY_LIMIT)) + .await + .unwrap(); + assert_eq!( + slot.read_targets().unwrap().len(), + NOSTR_SESSION_RELAY_LIMIT + ); + assert_eq!( + slot.configure(profile(base, NOSTR_SESSION_RELAY_LIMIT + 1)) + .await + .unwrap_err() + .kind(), + crate::error::ErrorKind::InvalidHostConfiguration + ); + assert_eq!( + slot.read_targets().unwrap().len(), + NOSTR_SESSION_RELAY_LIMIT + ); + slot.shutdown().await; +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn the_aggregate_budget_retains_a_busy_pool_and_refuses_an_extra_reservation() { + let HeldFetch { + slot, + previous, + base, + fetch, + server, + } = held_fetch(16).await; + assert_eq!( + slot.configure(profile(&base, 1)).await.unwrap_err().kind(), + crate::error::ErrorKind::SharedOperationUnavailable + ); + assert!(previous.transport.admission_is_closed()); + assert!(previous.transport.has_in_flight_operations()); + assert!(slot.read_targets().is_none()); + assert!(slot.state.read().unwrap().retired.is_empty()); + slot.shutdown().await; + tokio::time::timeout(Duration::from_secs(2), server) + .await + .unwrap() + .unwrap(); + let _obtained = tokio::time::timeout(Duration::from_secs(5), fetch) + .await + .unwrap() + .unwrap(); +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn cancelled_shutdown_retains_current_and_retired_pools_for_explicit_retry() { + let HeldFetch { + slot, + previous, + base, + fetch, + server, + } = held_fetch(1).await; + slot.configure(profile(&base, 15)).await.unwrap(); + assert_eq!(slot.read_targets().unwrap().len(), 15); + assert_eq!(slot.state.read().unwrap().retired.len(), 1); + assert!(previous.transport.has_in_flight_operations()); + assert_eq!( + slot.configure(profile(&base, 16)).await.unwrap_err().kind(), + crate::error::ErrorKind::SharedOperationUnavailable + ); + let held = slot.transition.lock().await; + let mut close = Box::pin(slot.shutdown()); + assert_eq!( + close.as_mut().poll(&mut Context::from_waker(Waker::noop())), + Poll::Pending + ); + drop(close); + assert_eq!(slot.state.read().unwrap().retired.len(), 1); + assert_eq!( + slot.configure(profile(&base, 1)).await.unwrap_err().kind(), + crate::error::ErrorKind::ClientClosed + ); + drop(held); + slot.shutdown().await; + assert!(slot.state.read().unwrap().retired.is_empty()); + assert!(slot.read_targets().is_none()); + tokio::time::timeout(Duration::from_secs(2), server) + .await + .unwrap() + .unwrap(); + let _obtained = tokio::time::timeout(Duration::from_secs(5), fetch) + .await + .unwrap() + .unwrap(); + slot.shutdown().await; +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn a_cancelled_call_disconnects_before_idle_retirement_is_reclaimed() { + let HeldFetch { + slot, + previous, + base, + fetch, + server, + } = held_fetch(1).await; + slot.configure(profile(&base, 15)).await.unwrap(); + assert_eq!(slot.state.read().unwrap().retired.len(), 1); + fetch.abort(); + assert!(fetch.await.unwrap_err().is_cancelled()); + tokio::time::timeout(Duration::from_secs(2), server) + .await + .unwrap() + .unwrap(); + assert!(!previous.transport.has_in_flight_operations()); + slot.configure(profile(&base, 1)).await.unwrap(); + assert!(slot.state.read().unwrap().retired.is_empty()); + assert_eq!(slot.read_targets().unwrap().len(), 1); + slot.shutdown().await; +} diff --git a/crates/sdk/tests/package_boundary.rs b/crates/sdk/tests/package_boundary.rs @@ -178,6 +178,7 @@ fn package_contains_only_reachable_sources_and_registered_targets() { "sync/tests/selected.rs".to_owned(), "trade.rs".to_owned(), "transport.rs".to_owned(), + "transport/nostr_retirement_tests.rs".to_owned(), ]) ); assert_eq!( diff --git a/crates/transport_nostr/README.md b/crates/transport_nostr/README.md @@ -67,6 +67,15 @@ results. Upstream pool shutdown signals its relay tasks; it does not itself establish that every internal task joined. [`NostrTransport::admission_is_closed`] reports admission, rather than complete session cleanup or business rollback. +Owners replacing a profile may call [`NostrTransport::close_admission`] while +retaining the pool for already-started calls. Their registered relay handles +disconnect synchronously when the last call completes or is cancelled, so +idle reconnect work does not survive retirement. The owner must still await +explicit pool shutdown. [`NostrTransport::has_in_flight_operations`] reports +source, delivery, subscription-setup and AUTH calls; it does not count passive +status inspection or represent live subscription handles or complete session +worker joining. No cleanup worker or post-observation await is introduced. + ## Prepared delivery boundary [`NostrTransport::prepare_delivery`] validates the exact request, writable diff --git a/crates/transport_nostr/src/auth.rs b/crates/transport_nostr/src/auth.rs @@ -218,9 +218,10 @@ impl NostrTransport { now_unix_ms: u64, ) -> BoxFuture<'a, Result<(), Error>> { Box::pin(async move { - if self.admission_is_closed() { - return Err(Error::TransportClosed); - } + let _operation = self.connections.begin().map_err(|error| match error { + radroots_transport::Error::TransportClosed => Error::TransportClosed, + _ => Error::AuthStateUnavailable, + })?; let pending = self.auth.pending(relay, challenge)?; if now_unix_ms > pending.expires_at_unix_ms { self.auth.remove(relay, challenge, false)?; diff --git a/crates/transport_nostr/src/client.rs b/crates/transport_nostr/src/client.rs @@ -4,7 +4,7 @@ use crate::{Error, RelayEndpoint, RelayProfile, RelayProfileKind, RelayStatusRep use core::fmt; use std::sync::{ Arc, - atomic::{AtomicBool, AtomicU64, Ordering}, + atomic::{AtomicU64, Ordering}, }; /// Maximum relay targets accepted by one transport instance. @@ -228,6 +228,22 @@ pub(crate) async fn connect_relay( Ok(relay) } +pub(crate) async fn connect_tracked( + client: &nostr_sdk::client::Client, + connections: &crate::connections::Connections, + url: &str, + timeout: std::time::Duration, +) -> Result<nostr_sdk::relay::Relay, nostr_sdk::error::Error> { + let relay = client.relay(url).await?.ok_or_else(|| { + nostr_sdk::error::Error::with_static_message( + nostr_sdk::error::ErrorKind::NotFound, + "configured relay is unavailable", + ) + })?; + connections.register(relay)?; + connect_relay(client, url, timeout).await +} + #[derive(Debug)] struct VerifiedEventAdmission; @@ -257,7 +273,7 @@ impl nostr_sdk::policy::AdmitPolicy for VerifiedEventAdmission { pub struct NostrTransport { config: Config, session_client: nostr_sdk::client::Client, - admission_closed: Arc<AtomicBool>, + pub(crate) connections: Arc<crate::connections::Connections>, pub(crate) client: Arc<dyn crate::sink::RelayClient>, pub(crate) source_client: Arc<dyn crate::source::RelaySourceClient>, pub(crate) subscription_client: Arc<dyn crate::subscription::RelaySubscriptionClient>, @@ -284,17 +300,26 @@ impl NostrTransport { )) .build(); let status = Arc::new(crate::status::StatusTracker::new(&config)); + let connections = Arc::new(crate::connections::Connections::new( + config.endpoints().len(), + )); Self { config, session_client: client.clone(), - admission_closed: Arc::new(AtomicBool::new(false)), - client: Arc::new(crate::sink::LiveRelayClient::new(client.clone(), writers)), + connections: Arc::clone(&connections), + client: Arc::new(crate::sink::LiveRelayClient::new( + client.clone(), + writers, + Arc::clone(&connections), + )), source_client: Arc::new(crate::source::LiveRelaySourceClient::new( client.clone(), ingress, + Arc::clone(&connections), )), subscription_client: Arc::new(crate::subscription::LiveRelaySubscriptionClient::new( client.clone(), + connections, )), auth: Arc::new(crate::auth::AuthFlow::new(Arc::new( crate::auth::LiveAuthClient::new(client), @@ -323,14 +348,28 @@ impl NostrTransport { /// Upstream pool shutdown signals its relay tasks; this method does not /// claim to join every upstream internal task. pub async fn shutdown(&self) { - self.admission_closed.store(true, Ordering::Release); - self.auth.close(); + self.close_admission(); self.session_client.shutdown().await; } + /// Refuses new calls while already-started calls retain their exact observations. + /// + /// When the last active call completes or is cancelled, its registered + /// relays disconnect synchronously. The owner must still await pool + /// shutdown; this method claims neither task joining nor durable completion. + pub fn close_admission(&self) { + self.connections.close(); + self.auth.close(); + } + + /// Reports active transport calls, excluding passive status and live subscription handles. + pub fn has_in_flight_operations(&self) -> bool { + self.connections.has_active_calls() + } + /// Reports closed admission, including interrupted shutdown, without claiming task joining. pub fn admission_is_closed(&self) -> bool { - self.admission_closed.load(Ordering::Acquire) + self.connections.is_closed() } pub(crate) fn require_open(&self) -> Result<(), radroots_transport::Error> { @@ -347,7 +386,7 @@ impl NostrTransport { Self { config, session_client: nostr_sdk::client::Client::default(), - admission_closed: Arc::new(AtomicBool::new(false)), + connections: Arc::new(crate::connections::Connections::new(MAX_RELAYS)), client, source_client: Arc::new(crate::source::LiveRelaySourceClient::isolated()), subscription_client: Arc::new( @@ -368,7 +407,7 @@ impl NostrTransport { Self { config, session_client: nostr_sdk::client::Client::default(), - admission_closed: Arc::new(AtomicBool::new(false)), + connections: Arc::new(crate::connections::Connections::new(MAX_RELAYS)), client: Arc::new(crate::sink::LiveRelayClient::isolated()), source_client, subscription_client: Arc::new( @@ -389,7 +428,7 @@ impl NostrTransport { Self { config, session_client: nostr_sdk::client::Client::default(), - admission_closed: Arc::new(AtomicBool::new(false)), + connections: Arc::new(crate::connections::Connections::new(MAX_RELAYS)), client: Arc::new(crate::sink::LiveRelayClient::isolated()), source_client: Arc::new(crate::source::LiveRelaySourceClient::isolated()), subscription_client, diff --git a/crates/transport_nostr/src/connections.rs b/crates/transport_nostr/src/connections.rs @@ -0,0 +1,203 @@ +//! Bounded connection registration and synchronous completion cleanup. + +use std::{ + collections::BTreeMap, + fmt, + sync::{ + Arc, Mutex, + atomic::{AtomicBool, AtomicUsize, Ordering}, + }, +}; + +pub(crate) struct Connections { + closed: AtomicBool, + active: AtomicUsize, + maximum: usize, + relays: Mutex<BTreeMap<String, nostr_sdk::relay::Relay>>, +} + +impl Connections { + pub(crate) fn new(maximum: usize) -> Self { + Self { + closed: AtomicBool::new(false), + active: AtomicUsize::new(0), + maximum, + relays: Mutex::new(BTreeMap::new()), + } + } + + pub(crate) fn is_closed(&self) -> bool { + self.closed.load(Ordering::Acquire) + } + + pub(crate) fn has_active_calls(&self) -> bool { + self.active.load(Ordering::Acquire) != 0 + } + + pub(crate) fn begin(self: &Arc<Self>) -> Result<Operation, radroots_transport::Error> { + if self.is_closed() { + return Err(radroots_transport::Error::TransportClosed); + } + self.active + .try_update(Ordering::AcqRel, Ordering::Acquire, |count| { + count.checked_add(1) + }) + .map_err(|_| radroots_transport::Error::TransportUnavailable)?; + let operation = Operation(Arc::clone(self)); + if self.is_closed() { + return Err(radroots_transport::Error::TransportClosed); + } + Ok(operation) + } + + pub(crate) fn close(&self) { + self.closed.store(true, Ordering::Release); + if !self.has_active_calls() { + self.disconnect(); + } + } + + pub(crate) fn register( + &self, + relay: nostr_sdk::relay::Relay, + ) -> Result<(), nostr_sdk::error::Error> { + let mut relays = self.relays.lock().map_err(|_| { + nostr_sdk::error::Error::with_static_message( + nostr_sdk::error::ErrorKind::State, + "connection registration is unavailable", + ) + })?; + let url = relay.url().to_string(); + if !relays.contains_key(&url) && relays.len() >= self.maximum { + return Err(nostr_sdk::error::Error::with_static_message( + nostr_sdk::error::ErrorKind::LimitExceeded, + "connection registration limit reached", + )); + } + relays.insert(url, relay); + Ok(()) + } + + fn disconnect(&self) { + // Disposal alone may recover poisoned state; it never authorizes work. + let relays = self + .relays + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner); + for relay in relays.values() { + relay.disconnect(); + } + } +} + +impl fmt::Debug for Connections { + fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + formatter + .debug_struct("Connections") + .field("admission_closed", &self.is_closed()) + .field("active_calls", &self.active.load(Ordering::Acquire)) + .finish_non_exhaustive() + } +} + +pub(crate) struct Operation(Arc<Connections>); + +impl Drop for Operation { + fn drop(&mut self) { + if self.0.active.fetch_sub(1, Ordering::AcqRel) == 1 && self.0.is_closed() { + self.0.disconnect(); + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn admission_closes_without_discarding_active_calls() { + let connections = Arc::new(Connections::new(2)); + assert!(!connections.has_active_calls()); + let first = connections.begin().unwrap(); + let second = connections.begin().unwrap(); + connections.close(); + assert!(connections.is_closed()); + assert!(matches!( + connections.begin(), + Err(radroots_transport::Error::TransportClosed) + )); + drop(first); + assert!(connections.has_active_calls()); + drop(second); + assert!(!connections.has_active_calls()); + connections.close(); + } + + #[test] + fn an_open_completed_call_does_not_close_admission_and_overflow_is_explicit() { + let connections = Arc::new(Connections::new(1)); + drop(connections.begin().unwrap()); + assert!(!connections.is_closed()); + assert!(!connections.has_active_calls()); + connections.active.store(usize::MAX, Ordering::Release); + assert!(matches!( + connections.begin(), + Err(radroots_transport::Error::TransportUnavailable) + )); + assert_eq!(connections.active.load(Ordering::Acquire), usize::MAX); + connections.active.store(0, Ordering::Release); + connections.close(); + } + + #[tokio::test] + async fn registration_is_deduplicated_bounded_and_safe_after_poison() { + let client = nostr_sdk::client::Client::default(); + client.add_relay("ws://127.0.0.1:19001").await.unwrap(); + client.add_relay("ws://127.0.0.1:19002").await.unwrap(); + let first = client.relay("ws://127.0.0.1:19001").await.unwrap().unwrap(); + let second = client.relay("ws://127.0.0.1:19002").await.unwrap().unwrap(); + let connections = Arc::new(Connections::new(1)); + connections.register(first.clone()).unwrap(); + connections.register(first.clone()).unwrap(); + assert_eq!(connections.relays.lock().unwrap().len(), 1); + assert_eq!( + connections.register(second).unwrap_err().kind(), + nostr_sdk::error::ErrorKind::LimitExceeded + ); + let debug = format!("{connections:?}"); + assert!(!debug.contains("19001")); + let _ = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { + let _held = connections.relays.lock().unwrap(); + panic!("poison connection registration"); + })); + assert_eq!( + connections.register(first).unwrap_err().kind(), + nostr_sdk::error::ErrorKind::State + ); + connections.close(); + assert!(connections.is_closed()); + client.shutdown().await; + } + + #[test] + fn dropping_a_pending_call_releases_its_registration_without_an_await() { + use std::{ + future::Future, + task::{Context, Poll, Waker}, + }; + let connections = Arc::new(Connections::new(1)); + let copy = Arc::clone(&connections); + let mut call = Box::pin(async move { + let _operation = copy.begin().unwrap(); + std::future::pending::<()>().await; + }); + assert_eq!( + call.as_mut().poll(&mut Context::from_waker(Waker::noop())), + Poll::Pending + ); + assert!(connections.has_active_calls()); + connections.close(); + drop(call); + assert!(!connections.has_active_calls()); + } +} diff --git a/crates/transport_nostr/src/lib.rs b/crates/transport_nostr/src/lib.rs @@ -4,6 +4,7 @@ mod auth; mod client; +mod connections; mod cursor; mod error; mod exact_delivery; diff --git a/crates/transport_nostr/src/sink.rs b/crates/transport_nostr/src/sink.rs @@ -62,14 +62,20 @@ pub(crate) trait RelayClient: Send + Sync { pub(crate) struct LiveRelayClient { client: nostr_sdk::client::Client, writers: crate::socket_write::WriterRegistry, + connections: std::sync::Arc<crate::connections::Connections>, } impl LiveRelayClient { pub(crate) const fn new( client: nostr_sdk::client::Client, writers: crate::socket_write::WriterRegistry, + connections: std::sync::Arc<crate::connections::Connections>, ) -> Self { - Self { client, writers } + Self { + client, + writers, + connections, + } } #[cfg(test)] @@ -78,6 +84,9 @@ impl LiveRelayClient { Self::new( client, crate::socket_write::WriterRegistry::new(std::iter::empty()), + std::sync::Arc::new(crate::connections::Connections::new( + crate::client::MAX_RELAYS, + )), ) } } @@ -111,9 +120,14 @@ impl RelayClient for LiveRelayClient { .connect_timeout(connect_timeout) .await .map_err(|error| error.to_string())?; - crate::client::connect_relay(&self.client, url.as_str(), connect_timeout) - .await - .map_err(|error| error.to_string())?; + crate::client::connect_tracked( + &self.client, + &self.connections, + url.as_str(), + connect_timeout, + ) + .await + .map_err(|error| error.to_string())?; event.publish(&self.client, &self.writers, &url).await }; let outcome = match tokio::time::timeout_at(deadline, attempt).await { @@ -219,9 +233,12 @@ impl NostrTransport { authorized, mut skipped, } = prepared; - if self.admission_is_closed() { - return Err(SinkFailure::transport_closed(&request)); - } + let _operation = self.connections.begin().map_err(|error| match error { + radroots_transport::Error::TransportClosed => { + SinkFailure::transport_closed(&request) + } + _ => SinkFailure::transport_unavailable(&request), + })?; if config != *self.config() { return Err(SinkFailure::invalid_contract(&request)); } diff --git a/crates/transport_nostr/src/source.rs b/crates/transport_nostr/src/source.rs @@ -65,20 +65,32 @@ pub(crate) trait RelaySourceClient: Send + Sync { pub(crate) struct LiveRelaySourceClient { client: nostr_sdk::client::Client, ingress: crate::source_ingress::IngressRegistry, + connections: Arc<crate::connections::Connections>, } impl LiveRelaySourceClient { pub(crate) const fn new( client: nostr_sdk::client::Client, ingress: crate::source_ingress::IngressRegistry, + connections: Arc<crate::connections::Connections>, ) -> Self { - Self { client, ingress } + Self { + client, + ingress, + connections, + } } #[cfg(test)] pub(crate) fn isolated() -> Self { let client = nostr_sdk::client::Client::default(); - Self::new(client, crate::source_ingress::IngressRegistry::default()) + Self::new( + client, + crate::source_ingress::IngressRegistry::default(), + Arc::new(crate::connections::Connections::new( + crate::client::MAX_RELAYS, + )), + ) } } @@ -183,8 +195,9 @@ impl RelaySourceClient for LiveRelaySourceClient { filter = filter.until(Timestamp::from_secs(until)); } let connected = tokio::time::timeout_at(deadline, async { - crate::client::connect_relay( + crate::client::connect_tracked( &self.client, + &self.connections, url.as_str(), connect_timeout.min( deadline.saturating_duration_since(tokio::time::Instant::now()), @@ -470,7 +483,7 @@ impl EventSource for NostrTransport { request: FetchRequest, ) -> BoxFuture<'_, Result<FetchPage, radroots_transport::Error>> { Box::pin(async move { - self.require_open()?; + let _operation = self.connections.begin()?; let cursor_scope = request_scope(&request); let cursor = request .cursor() @@ -1514,7 +1527,11 @@ mod tests { .await .unwrap(); let queued = client.relay(urls[1].as_str()).await.unwrap().unwrap(); - let source = LiveRelaySourceClient::new(client, ingress); + let source = LiveRelaySourceClient::new( + client, + ingress, + Arc::new(crate::connections::Connections::new(2)), + ); let query = SourceQuery { relays: urls .iter() diff --git a/crates/transport_nostr/src/subscription.rs b/crates/transport_nostr/src/subscription.rs @@ -62,17 +62,29 @@ pub(crate) trait RelaySubscriptionClient: Send + Sync { #[derive(Clone, Debug)] pub(crate) struct LiveRelaySubscriptionClient { client: nostr_sdk::client::Client, + connections: std::sync::Arc<crate::connections::Connections>, } impl LiveRelaySubscriptionClient { - pub(crate) const fn new(client: nostr_sdk::client::Client) -> Self { - Self { client } + pub(crate) const fn new( + client: nostr_sdk::client::Client, + connections: std::sync::Arc<crate::connections::Connections>, + ) -> Self { + Self { + client, + connections, + } } #[cfg(test)] pub(crate) fn isolated() -> Self { let client = nostr_sdk::client::Client::default(); - Self::new(client) + Self::new( + client, + std::sync::Arc::new(crate::connections::Connections::new( + crate::client::MAX_RELAYS, + )), + ) } } @@ -96,9 +108,14 @@ impl RelaySubscriptionClient for LiveRelaySubscriptionClient { .connect_timeout(query.connect_timeout) .await .map_err(|_| ())?; - crate::client::connect_relay(&self.client, url.as_str(), query.connect_timeout) - .await - .map_err(|_| ())?; + crate::client::connect_tracked( + &self.client, + &self.connections, + url.as_str(), + query.connect_timeout, + ) + .await + .map_err(|_| ())?; relay_lookup.insert(url.clone(), target.relay); targeted.push((url, vec![filter])); } @@ -462,7 +479,7 @@ impl EventSubscriber for NostrTransport { request: SubscriptionRequest, ) -> BoxFuture<'_, Result<BoxSubscription, radroots_transport::Error>> { Box::pin(async move { - self.require_open()?; + let _operation = self.connections.begin()?; let remaining = remaining_duration(request.bounds().deadline_unix_ms()); if remaining.is_zero() { return RelayEventSubscription::ended( diff --git a/crates/transport_nostr/tests/package_boundary.rs b/crates/transport_nostr/tests/package_boundary.rs @@ -41,6 +41,7 @@ fn manifest_and_root_match_the_governed_transport_boundary() { BTreeSet::from([ "auth", "client", + "connections", "cursor", "error", "exact_delivery", @@ -317,6 +318,7 @@ fn adapter_owns_no_storage_outbox_or_orchestration_surface() { BTreeSet::from([ "auth.rs".to_owned(), "client.rs".to_owned(), + "connections.rs".to_owned(), "cursor.rs".to_owned(), "error.rs".to_owned(), "exact_delivery.rs".to_owned(),