Revoke podcast cards promptly and report unrevoked ones

Turning sharing off or unsubscribing runs a reconciler pass right away,
and quitting waits up to 4 s for it. The Podcasts tab tells a private
podcast whose card from this device is still live, failed revocations
are reported without ids, the overlay peer count stays current, and the
reconciler loop is tested against a loopback overlay.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
sanapachandClaude Opus 5.5 committed 2026-11-20 15:13:07 +02:00
1 parent a51f5552e3
commit 1137670229
6 files changed
+423 -35

No files matched your search

+43 -11
View File
@@ -33,6 +33,9 @@ const SCHEDULER_FIRST_DELAY: Duration = Duration::from_secs(10);
const MAX_CONCURRENT_REFRESHES: usize = 4;
/// Feeds started per scheduler pass.
const MAX_DUE_PER_PASS: usize = 16;
/// How long quitting waits for a pending revocation (share turned off or
/// unsubscribed just before) to be signed and pushed.
const PUBLICATION_FLUSH_TIMEOUT: Duration = Duration::from_secs(4);
/// Podcast services available once the store opened.
pub struct PodcastRuntime {
@@ -154,6 +157,15 @@ fn wake_publication(runtime: &Runtime) {
}
}
/// Asks the publication reconciler for an immediate pass: a card must be
/// revoked (sharing turned off or unsubscribed here). Quitting waits for
/// it, bounded ([`PUBLICATION_FLUSH_TIMEOUT`]).
fn revoke_publication(runtime: &Runtime) {
if let Some(podcasts) = &runtime.podcasts {
podcasts.publication.revoke_now();
}
}
fn services(runtime: &Runtime) -> Option<Services> {
runtime
.podcasts
@@ -475,8 +487,10 @@ pub fn handle_event(
title,
result,
} => {
// Unsubscribing revokes this device's card.
wake_publication(runtime);
// Unsubscribing revokes this device's card, right away.
if result.is_ok() {
revoke_publication(runtime);
}
podcasts::unsubscribed(state, podcast_id, title, result)
.into_iter()
.collect()
@@ -494,7 +508,11 @@ pub fn handle_event(
shared,
result,
} => {
wake_publication(runtime);
if !shared && result.is_ok() {
revoke_publication(runtime);
} else {
wake_publication(runtime);
}
podcasts::share_set(state, podcast_id, title, shared, result)
.into_iter()
.collect()
@@ -1095,18 +1113,32 @@ fn progress_event(
}
}
/// Before exit: end the playing episode's session and give the progress
/// writer a moment to save it.
/// Before exit (before the federation node stops): end the playing
/// episode's session and give the progress writer a moment to save it, and
/// let a revocation requested just before (sharing turned off, unsubscribed)
/// reach the overlay.
pub(crate) async fn shutdown(runtime: &mut Runtime) {
end_playback(runtime);
let Some(mut progress) = runtime
.podcasts
.as_mut()
.and_then(|podcasts| podcasts.progress.take())
else {
let Some(podcasts) = runtime.podcasts.as_mut() else {
return;
};
let _ = tokio::task::spawn_blocking(move || progress.close(Duration::from_secs(3))).await;
let publication = podcasts.publication.clone();
let progress = podcasts.progress.take();
let flush = async {
if !publication.flush(PUBLICATION_FLUSH_TIMEOUT).await {
tracing::warn!(
"podcast publication: a public card may not be revoked yet; \
it is revoked on the next start with federation on"
);
}
};
let close = async {
if let Some(mut progress) = progress {
let _ =
tokio::task::spawn_blocking(move || progress.close(Duration::from_secs(3))).await;
}
};
tokio::join!(flush, close);
}
#[cfg(test)]
+2 -1
View File
@@ -806,7 +806,7 @@ pub fn share_set(
Some(Effect::PodcastsReload)
}
Ok(()) => {
state.podcasts.publication.podcasts.remove(&podcast_id);
state.podcasts.publication.note_revocation(&podcast_id);
state.status_message = Some(format!(
"podcasts: {title} is private again; the public card is being revoked"
));
@@ -1153,6 +1153,7 @@ pub fn unsubscribed(
) -> Option<Effect> {
match result {
Ok(()) => {
state.podcasts.publication.note_revocation(&podcast_id);
state.status_message = Some(format!(
"podcasts: unsubscribed from {}; progress and history are kept",
display_title(&title)
+149 -18
View File
@@ -34,6 +34,7 @@
use std::collections::{BTreeMap, BTreeSet};
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Mutex, PoisonError};
use std::time::Duration;
@@ -41,7 +42,7 @@ use anyhow::{Context, Result};
use furumi_library::podcasts::{PodcastListFilter, PodcastStore, PodcastSummary};
use music_dht::podcast::{FeedUrl, PodcastId, SubscriptionRecord, effective_share};
use music_dht::podcast_dht::{
FeedUrlPrivacyWarning, PodcastCardSpec, PodcastDht, PodcastShareState,
FeedUrlPrivacyWarning, PodcastCardSpec, PodcastDht, PodcastDhtSyncStats, PodcastShareState,
};
use serde::{Deserialize, Serialize};
use tokio::sync::{Notify, watch};
@@ -57,6 +58,9 @@ pub const RECONCILE_INTERVAL: Duration = Duration::from_secs(5 * 60);
/// Triggers within this window are handled by one pass (a "check all"
/// refresh finishes many feeds in a row).
pub const RECONCILE_DEBOUNCE: Duration = Duration::from_secs(1);
/// How often the overlay's peer count is checked between passes, so the
/// Podcasts tab does not show a stale "no discovery peers yet".
pub const PEER_REFRESH_INTERVAL: Duration = Duration::from_secs(10);
/// The feed URLs a privacy confirmation covers: exactly the URLs of the card
/// the user was shown.
@@ -369,10 +373,29 @@ pub struct PublicationReport {
pub discovery: Discovery,
/// Shared podcasts only.
pub podcasts: BTreeMap<PodcastId, Publication>,
/// The pass as a whole failed (store or overlay error).
/// Podcasts that are no longer shared but whose card from this device is
/// still live: the revocation is pending, failed (retried every pass),
/// or waits for the overlay (federation off). In memory only; never
/// logged.
pub unrevoked: BTreeSet<PodcastId>,
/// The pass as a whole failed (store or overlay error), or cards could
/// not be revoked.
pub error: Option<String>,
}
impl PublicationReport {
/// Sharing of `podcast_id` was turned off (or it was unsubscribed) on
/// this device: until the next pass says otherwise, a card it may have
/// published counts as not revoked yet.
pub fn note_revocation(&mut self, podcast_id: &PodcastId) {
if let Some(publication) = self.podcasts.remove(podcast_id)
&& !matches!(publication, Publication::HeldBack(_))
{
self.unrevoked.insert(podcast_id.clone());
}
}
}
/// Reconciles this device's publications with the share registers of
/// `store` once. Without a store (it could not be opened) every own
/// publication is revoked: this device can no longer tell what the user
@@ -466,6 +489,42 @@ pub async fn reconcile(
BTreeSet::new()
}
};
summarize(&mut report, shared, stats, &active);
report
}
/// Folds one `sync_shares` result and the set of this device's active
/// publications into `report`: the state of each shared podcast, the cards
/// of private podcasts that are still live (`unrevoked`), and an error when
/// revocations failed (counts only, no ids).
pub(super) fn summarize(
report: &mut PublicationReport,
shared: Vec<PodcastId>,
stats: PodcastDhtSyncStats,
active: &BTreeSet<PodcastId>,
) {
let shared_set: BTreeSet<&PodcastId> = shared.iter().collect();
let revoke_failed: BTreeSet<PodcastId> = stats
.failed
.iter()
.filter(|failure| !shared_set.contains(&failure.podcast_id))
.map(|failure| failure.podcast_id.clone())
.collect();
// Cards of podcasts that are private now but still live from here.
report.unrevoked = active
.iter()
.filter(|podcast_id| !shared_set.contains(podcast_id))
.cloned()
.chain(revoke_failed.iter().cloned())
.collect();
if !revoke_failed.is_empty() && report.error.is_none() {
let count = revoke_failed.len();
tracing::warn!(count, "podcast publication: public cards not revoked yet");
report.error = Some(format!(
"{count} public card{} could not be revoked yet; retrying",
if count == 1 { "" } else { "s" }
));
}
let held: BTreeMap<PodcastId, Vec<FeedUrlPrivacyWarning>> = stats
.held_back
.into_iter()
@@ -488,13 +547,20 @@ pub async fn reconcile(
};
report.podcasts.insert(podcast_id, publication);
}
report
}
/// Wakes the running reconciler.
#[derive(Debug, Clone)]
pub struct Reconciler {
/// Debounced passes (feed refreshes, received operations).
wake: Arc<Notify>,
/// Immediate passes (revocations).
urgent: Arc<Notify>,
/// Number of revocations requested so far.
revocations: Arc<AtomicU64>,
/// Revocations covered by a finished pass (or by a pass that found the
/// overlay off, when nothing can be sent).
covered: watch::Receiver<u64>,
}
impl Reconciler {
@@ -503,11 +569,39 @@ impl Reconciler {
pub fn wake(&self) {
self.wake.notify_one();
}
/// Requests a reconciliation now, without the debounce: sharing was
/// turned off or a podcast unsubscribed, and its card must be revoked
/// before the user may quit. See [`Reconciler::flush`].
pub fn revoke_now(&self) {
self.revocations.fetch_add(1, Ordering::SeqCst);
self.urgent.notify_one();
}
/// Waits, at most `timeout`, until a pass started after every
/// [`Reconciler::revoke_now`] so far finished (the tombstones were
/// signed and pushed, or the overlay is off and nothing can be sent).
/// Returns at once when nothing is pending. `false` on timeout or when
/// the reconciler stopped.
pub async fn flush(&self, timeout: Duration) -> bool {
let target = self.revocations.load(Ordering::SeqCst);
let mut covered = self.covered.clone();
if *covered.borrow() >= target {
return true;
}
// `revoke_now` already asked for the pass.
matches!(
tokio::time::timeout(timeout, covered.wait_for(|covered| *covered >= target)).await,
Ok(Ok(_))
)
}
}
/// Runs the reconciler until the overlay watch closes. `dht` follows the
/// federation node; while it is `None` nothing is published from this
/// device and `report` says so.
/// device and `report` says so (keeping the cards known not to be revoked
/// yet: they are revoked once the overlay runs again). Between passes the
/// overlay's peer count is refreshed every [`PEER_REFRESH_INTERVAL`].
pub fn spawn(
mut dht: watch::Receiver<Option<Arc<PodcastDht>>>,
store: Option<Arc<PodcastStore>>,
@@ -515,32 +609,69 @@ pub fn spawn(
report: impl Fn(PublicationReport) + Send + 'static,
) -> Reconciler {
let wake = Arc::new(Notify::new());
let urgent = Arc::new(Notify::new());
let revocations = Arc::new(AtomicU64::new(0));
let (covered_tx, covered) = watch::channel(0);
let reconciler = Reconciler {
wake: Arc::clone(&wake),
urgent: Arc::clone(&urgent),
revocations: Arc::clone(&revocations),
covered,
};
tokio::spawn(async move {
let mut last = PublicationReport::default();
let mut peers = tokio::time::interval(PEER_REFRESH_INTERVAL);
peers.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay);
peers.reset();
loop {
let requested = revocations.load(Ordering::SeqCst);
let current = dht.borrow_and_update().clone();
match current {
Some(dht) => {
let pass = reconcile(&dht, store.clone(), Arc::clone(&confirmations)).await;
report(pass);
}
None => report(PublicationReport {
last = match current {
Some(dht) => reconcile(&dht, store.clone(), Arc::clone(&confirmations)).await,
None => PublicationReport {
discovery: Discovery::Off(
"federation is off or podcast discovery is unavailable".into(),
),
unrevoked: std::mem::take(&mut last.unrevoked),
..PublicationReport::default()
}),
}
tokio::select! {
_ = wake.notified() => tokio::time::sleep(RECONCILE_DEBOUNCE).await,
changed = dht.changed() => {
if changed.is_err() {
return;
},
};
report(last.clone());
covered_tx.send_replace(requested);
let periodic = tokio::time::sleep(RECONCILE_INTERVAL);
tokio::pin!(periodic);
loop {
tokio::select! {
_ = urgent.notified() => break,
_ = wake.notified() => {
tokio::time::sleep(RECONCILE_DEBOUNCE).await;
// Wakes during the debounce are covered by this
// pass: drop the permit they left.
let _ = futures_util::FutureExt::now_or_never(wake.notified());
break;
}
changed = dht.changed() => {
if changed.is_err() {
return;
}
break;
}
_ = &mut periodic => break,
_ = peers.tick() => {
// Keep the peer count live (the first bootstrap
// usually finishes after the first pass).
let Discovery::Running { peers: shown } = &mut last.discovery else {
continue;
};
let Some(known) = dht.borrow().as_ref().map(|dht| dht.known_peers()) else {
continue;
};
if *shown != known {
*shown = known;
report(last.clone());
}
}
}
_ = tokio::time::sleep(RECONCILE_INTERVAL) => {}
}
}
});
+184 -2
View File
@@ -9,12 +9,14 @@ use anyhow::Result;
use furumi_library::podcasts::{OpStamp, PodcastLocalOp, PodcastStore, StampSource};
use music_dht::device_sync::SyncOpWire;
use music_dht::podcast::{FeedUrl, PodcastId, PodcastMeta};
use music_dht::podcast_dht::{FeedUrlPrivacyWarning, PODCAST_DHT_ALPN, PodcastDht};
use music_dht::podcast_dht::{
FeedUrlPrivacyWarning, PODCAST_DHT_ALPN, PodcastDht, PodcastDhtSyncStats, PodcastSyncFailure,
};
use music_dht::{MusicDhtConfig, MusicDhtService, NetworkId};
use super::publication::{
Discovery, Publication, PublicationReport, ShareConfirmations, check_share, reconcile,
share_card, share_states, warning_ru,
share_card, share_states, summarize, warning_ru,
};
const SHOW_URL: &str = "https://feeds.example.test/rust-radio.xml";
@@ -256,6 +258,67 @@ fn privacy_confirmations_are_local_and_bound_to_the_urls() {
assert!(!ShareConfirmations::open(path).is_confirmed(&card));
}
#[test]
fn revocations_that_fail_are_reported_without_ids() {
let show = PodcastId::from_feed_url(&FeedUrl::parse(SHOW_URL).unwrap());
let diary = PodcastId::from_feed_url(&FeedUrl::parse(PRIVATE_URL).unwrap());
let secret = PodcastId::from_feed_url(&FeedUrl::parse(SECRET_URL).unwrap());
// The tombstone of the private diary could not be pushed: its card is
// still live and the user is told, without the podcast's id or URL.
let mut report = PublicationReport::default();
let stats = PodcastDhtSyncStats {
failed: vec![PodcastSyncFailure {
podcast_id: diary.clone(),
error: "no replica reached".into(),
}],
..PodcastDhtSyncStats::default()
};
let active = [show.clone(), diary.clone()].into_iter().collect();
summarize(&mut report, vec![show.clone()], stats, &active);
assert_eq!(report.podcasts.get(&show), Some(&Publication::Published));
assert_eq!(
report.podcasts.get(&diary),
None,
"private: not listed as shared"
);
assert_eq!(report.unrevoked, [diary.clone()].into_iter().collect());
let error = report.error.clone().unwrap();
assert_eq!(error, "1 public card could not be revoked yet; retrying");
assert!(!error.contains(diary.as_str()));
// A private podcast whose card is still active (not revoked yet) is
// unrevoked too, without an error; a failed shared one is Failed.
let mut report = PublicationReport::default();
let stats = PodcastDhtSyncStats {
failed: vec![PodcastSyncFailure {
podcast_id: show.clone(),
error: "signing failed".into(),
}],
..PodcastDhtSyncStats::default()
};
let active = [secret.clone()].into_iter().collect();
summarize(&mut report, vec![show.clone()], stats, &active);
assert_eq!(
report.podcasts.get(&show),
Some(&Publication::Failed("signing failed".into()))
);
assert_eq!(report.unrevoked, [secret.clone()].into_iter().collect());
assert_eq!(report.error, None);
// Turning sharing off in the UI counts a published card as not revoked
// until the next pass; a held-back one was never published here.
let mut report = PublicationReport::default();
report.podcasts.insert(show.clone(), Publication::Published);
report
.podcasts
.insert(secret.clone(), Publication::HeldBack(Vec::new()));
report.note_revocation(&show);
report.note_revocation(&secret);
assert!(report.podcasts.is_empty());
assert_eq!(report.unrevoked, [show].into_iter().collect());
}
// ---------------------------------------------------------------------------
// Two loopback nodes
@@ -474,3 +537,122 @@ async fn two_nodes() {
a.stop().await;
c.stop().await;
}
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn the_reconciler_follows_the_overlay_and_revokes_without_the_debounce() {
tokio::time::timeout(Duration::from_secs(180), reconciler_lifecycle())
.await
.unwrap();
}
/// `publication::spawn` as the runtime drives it: the federation watch,
/// debounced wakes, immediate revocations with the quit-time flush, the live
/// peer count, and the end of the task with the watch.
async fn reconciler_lifecycle() {
use super::publication::{PEER_REFRESH_INTERVAL, RECONCILE_DEBOUNCE, spawn};
let temp = tempfile::tempdir().unwrap();
let network = NetworkId::from_name("furumi-tui-podcast-reconciler-test");
let store = Arc::new(PodcastStore::open_in_memory().unwrap());
let confirmations = Arc::new(ShareConfirmations::in_memory());
let mut device = Device::new("device-a");
let show = device.subscribe(&store, SHOW_URL, "Rust Radio Weekly");
device.set_shared(&store, &show, true);
let (overlay, watched) = tokio::sync::watch::channel(None);
let (tx, mut reports) = tokio::sync::mpsc::unbounded_channel();
let reconciler = spawn(
watched,
Some(Arc::clone(&store)),
confirmations,
move |report| {
let _ = tx.send(report);
},
);
let mut next = async || reports.recv().await.expect("a report");
// Federation off: nothing is published, nothing to wait for on quit.
let report = next().await;
assert!(matches!(report.discovery, Discovery::Off(_)));
assert!(report.podcasts.is_empty());
assert!(reconciler.flush(Duration::from_millis(10)).await);
// The overlay starts (two nodes, the overlay not bootstrapped yet): a
// pass publishes right away.
let a = Node::start(&temp.path().join("a"), network).await;
let c = Node::start(&temp.path().join("c"), network).await;
c.service
.connect(a.service.ticket().await.unwrap())
.await
.unwrap();
wait_for("music DHT contacts", async || {
(!a.service.known_peers().is_empty()).then_some(())
})
.await;
overlay.send_replace(Some(Arc::clone(&a.dht)));
let report = next().await;
assert!(matches!(report.discovery, Discovery::Running { .. }));
assert_eq!(publication(&report, &show), Some(Publication::Published));
// The peer count follows the overlay without another pass.
a.dht.bootstrap().await.unwrap();
c.dht.bootstrap().await.unwrap();
let mut report = report;
let started = std::time::Instant::now();
while !matches!(report.discovery, Discovery::Running { peers } if peers >= 1) {
assert!(
started.elapsed() < PEER_REFRESH_INTERVAL * 3,
"the peer count is refreshed"
);
report = next().await;
}
assert_eq!(publication(&report, &show), Some(Publication::Published));
// Wakes within the debounce are handled by one pass.
reconciler.wake();
reconciler.wake();
reconciler.wake();
let report = next().await;
assert_eq!(publication(&report, &show), Some(Publication::Published));
assert!(
tokio::time::timeout(RECONCILE_DEBOUNCE * 2, reports.recv())
.await
.is_err(),
"one pass for several wakes"
);
// Sharing off: the revocation does not wait for the debounce, and the
// quit-time flush returns once the tombstone was sent.
device.set_shared(&store, &show, false);
reconciler.revoke_now();
assert!(
reconciler.flush(RECONCILE_DEBOUNCE * 9 / 10).await,
"revoked before the debounce would have ended"
);
let report = reports.recv().await.unwrap();
assert!(report.podcasts.is_empty());
assert!(report.unrevoked.is_empty());
assert!(
a.dht
.published()
.await
.unwrap()
.iter()
.all(|publication| !publication.active)
);
assert!(!found(&c, &show).await);
// The overlay stops: off again; the task ends with the watch.
overlay.send_replace(None);
let mut report = reports.recv().await.unwrap();
while !matches!(report.discovery, Discovery::Off(_)) {
// A peer count refresh may still come in between.
assert!(report.podcasts.is_empty());
report = reports.recv().await.unwrap();
}
drop(overlay);
assert!(reports.recv().await.is_none(), "the reconciler stopped");
a.stop().await;
c.stop().await;
}
+27
View File
@@ -216,6 +216,33 @@ mod tests {
state.podcasts.publication.discovery = Discovery::Off("federation is off".into());
let screen = render(&state, 110, 30);
assert!(screen.contains("not published:"), "{screen}");
// Sharing turned off here: until a pass revoked the card, the panel
// does not claim the podcast is simply private.
state.podcasts.publication.discovery = Discovery::Running { peers: 2 };
state
.podcasts
.publication
.podcasts
.insert(podcast_fixtures::podcast_id(), Publication::Published);
state
.podcasts
.publication
.note_revocation(&podcast_fixtures::podcast_id());
let screen = render(&state, 110, 30);
assert!(screen.contains("private · revoking the"), "{screen}");
if let Some(crate::app::state::Loadable::Ready(rows)) = &mut state.podcasts.list {
rows[0].summary.shared = false;
}
let screen = render(&state, 110, 30);
assert!(screen.contains("private · revoking the"), "{screen}");
assert!(!screen.contains("private (not shared"), "{screen}");
state.podcasts.publication.discovery = Discovery::Off("federation is off".into());
let screen = render(&state, 110, 30);
assert!(screen.contains("not revoked yet:"), "{screen}");
state.podcasts.publication.unrevoked.clear();
let screen = render(&state, 110, 30);
assert!(screen.contains("private (not shared"), "{screen}");
}
/// Search view with a federated artist and two public podcast cards;
+18 -3
View File
@@ -313,10 +313,25 @@ pub(crate) fn share_status_line(state: &AppState, podcast: &PodcastRow) -> Line<
if tab.share_busy.contains(podcast.id()) {
return loading_line(state, "saving the share setting…");
}
if !podcast.summary.shared {
return Line::styled("private (not shared in the federation)", theme::dim());
}
let report = &tab.publication;
// A card from this device that is still live although the podcast is
// private now (revocation pending, failed, or federation off).
let unrevoked = report.unrevoked.contains(podcast.id());
if !podcast.summary.shared || (unrevoked && !report.podcasts.contains_key(podcast.id())) {
if !unrevoked {
return Line::styled("private (not shared in the federation)", theme::dim());
}
if let Discovery::Off(_) = report.discovery {
return Line::styled(
"private · public card not revoked yet: federation is off",
Style::new().fg(Color::Yellow),
);
}
return loading_line(
state,
"private · revoking the public card from this device…",
);
}
let (text, style) = match (&report.discovery, report.podcasts.get(podcast.id())) {
(Discovery::Off(reason), _) => (format!("shared · not published: {reason}"), theme::dim()),
(_, Some(Publication::HeldBack(_))) => (