Wait for in-flight revocations and track them with federation off

Share-off and unsubscribe now take a revocation ticket on the UI thread
before the write starts and ask for the immediate pass from the blocking
task once the write succeeded, so quitting right after Shift+F (before the
write's event was handled) still waits for the tombstone. With the
federation off or not started yet, a podcast that was shared counts as
possibly not revoked: the reconciler keeps it in the Off reports until the
overlay runs, the side panel says the card may not be revoked yet, and the
quit-time flush logs a warning.

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 d6962eba47
commit adfe809a40
7 files changed
+312 -66

No files matched your search

+42 -16
View File
@@ -157,13 +157,22 @@ 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();
}
/// A card may have to be revoked (sharing turned off or unsubscribed here):
/// the write about to start ends with an immediate reconciler pass
/// ([`publication::RevocationTicket::done`]), and quitting waits for both,
/// bounded ([`PUBLICATION_FLUSH_TIMEOUT`]), even when it is handled before
/// the write finished.
fn begin_revocation(runtime: &Runtime) -> Option<publication::RevocationTicket> {
runtime
.podcasts
.as_ref()
.map(|podcasts| podcasts.publication.begin_revocation())
}
/// Whether `podcast_id` is shared now (a card from this device may be
/// live), read before a share-off or unsubscribe is written. Blocking.
fn shared_now(services: &Services, podcast_id: &music_dht::podcast::PodcastId) -> bool {
matches!(services.store.podcast(podcast_id), Ok(Some(summary)) if summary.shared)
}
fn services(runtime: &Runtime) -> Option<Services> {
@@ -283,9 +292,11 @@ pub fn perform(state: &mut AppState, runtime: &mut Runtime, effect: Effect) -> O
}
}
Effect::PodcastUnsubscribe { podcast_id, title } => {
let revocation = begin_revocation(runtime);
tokio::task::spawn_blocking(move || {
let store = Arc::clone(&services.store);
let id = podcast_id.clone();
let was_shared = shared_now(&services, &podcast_id);
let result = services
.log
.record(&mut |stamps| store.unsubscribe(stamps, &id))
@@ -297,6 +308,12 @@ pub fn perform(state: &mut AppState, runtime: &mut Runtime, effect: Effect) -> O
{
tracing::warn!("podcast share confirmation not removed: {err:#}");
}
// Unsubscribing revokes this device's card, right away.
if let Some(revocation) = revocation
&& result.is_ok()
{
revocation.done(was_shared.then_some(&podcast_id));
}
let _ = tx.send(AppEvent::PodcastUnsubscribed {
podcast_id,
title,
@@ -337,9 +354,20 @@ pub fn perform(state: &mut AppState, runtime: &mut Runtime, effect: Effect) -> O
shared,
confirm,
} => {
let revocation = if shared {
None
} else {
begin_revocation(runtime)
};
tokio::task::spawn_blocking(move || {
let was_shared = !shared && shared_now(&services, &podcast_id);
let result =
set_shared(&services, &podcast_id, shared, confirm).map_err(err_string);
if let Some(revocation) = revocation
&& result.is_ok()
{
revocation.done(was_shared.then_some(&podcast_id));
}
let _ = tx.send(AppEvent::PodcastShareSet {
podcast_id,
title,
@@ -488,10 +516,7 @@ pub fn handle_event(
title,
result,
} => {
// Unsubscribing revokes this device's card, right away.
if result.is_ok() {
revoke_publication(runtime);
}
// The blocking write already asked for the revocation pass.
podcasts::unsubscribed(state, podcast_id, title, result)
.into_iter()
.collect()
@@ -509,9 +534,9 @@ pub fn handle_event(
shared,
result,
} => {
if !shared && result.is_ok() {
revoke_publication(runtime);
} else {
// A share-off already asked for its revocation pass when it
// was written.
if shared || result.is_err() {
wake_publication(runtime);
}
podcasts::share_set(state, podcast_id, title, shared, result)
@@ -1116,8 +1141,9 @@ fn progress_event(
/// 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.
/// let a revocation requested just before (sharing turned off, unsubscribed;
/// also one still being written) reach the overlay. Logs a warning when a
/// card of a private podcast may still be live.
pub(crate) async fn shutdown(runtime: &mut Runtime) {
end_playback(runtime);
let Some(podcasts) = runtime.podcasts.as_mut() else {
+17 -2
View File
@@ -806,7 +806,7 @@ pub fn share_set(
Some(Effect::PodcastsReload)
}
Ok(()) => {
state.podcasts.publication.note_revocation(&podcast_id);
note_revocation(state, &podcast_id);
state.status_message = Some(format!(
"podcasts: {title} is private again; the public card is being revoked"
));
@@ -819,6 +819,21 @@ pub fn share_set(
}
}
/// Sharing of `podcast_id` was turned off or it was unsubscribed here: its
/// card counts as not revoked until a pass says otherwise
/// (`PublicationReport::note_revocation`; the row is not reloaded yet, so it
/// still says whether the podcast was shared).
fn note_revocation(state: &mut AppState, podcast_id: &PodcastId) {
let was_shared = state
.podcasts
.podcast(podcast_id)
.is_some_and(|row| row.summary.shared);
state
.podcasts
.publication
.note_revocation(podcast_id, was_shared);
}
/// A publication reconciler pass finished. Errors go to the status bar
/// when they are new (the log has them too).
pub fn publication_reported(state: &mut AppState, report: PublicationReport) {
@@ -1153,7 +1168,7 @@ pub fn unsubscribed(
) -> Option<Effect> {
match result {
Ok(()) => {
state.podcasts.publication.note_revocation(&podcast_id);
note_revocation(state, &podcast_id);
state.status_message = Some(format!(
"podcasts: unsubscribed from {}; progress and history are kept",
display_title(&title)
+24
View File
@@ -2008,6 +2008,7 @@ mod podcast_tab {
let effect = podcasts::share_set(&mut state, podcast_id(), "Show".into(), false, Ok(()));
assert_eq!(effect, Some(Effect::PodcastsReload));
assert!(state.podcasts.publication.podcasts.is_empty());
assert!(state.podcasts.publication.unrevoked.contains(&podcast_id()));
assert!(
state
.status_message
@@ -2015,6 +2016,29 @@ mod podcast_tab {
.is_some_and(|m| m.contains("private again"))
);
// The federation is off (since the start, or the app was restarted
// since the card was published): no pass knows whether a card from
// an earlier session is live, so one that was shared counts as not
// revoked yet, for share-off and unsubscribe alike.
for discovery in [
Discovery::Off("federation is off".into()),
Discovery::Starting,
] {
let mut state = ready(vec![shared_row()]);
state.podcasts.publication.discovery = discovery.clone();
podcasts::share_set(&mut state, podcast_id(), "Show".into(), false, Ok(()));
assert!(state.podcasts.publication.unrevoked.contains(&podcast_id()));
let mut state = ready(vec![shared_row()]);
state.podcasts.publication.discovery = discovery.clone();
podcasts::unsubscribed(&mut state, podcast_id(), "Show".into(), Ok(()));
assert!(state.podcasts.publication.unrevoked.contains(&podcast_id()));
// A podcast that was not shared has no card to revoke.
let mut state = ready(vec![fx::row("Show", 0, 1, 0)]);
state.podcasts.publication.discovery = discovery;
podcasts::unsubscribed(&mut state, podcast_id(), "Show".into(), Ok(()));
assert!(state.podcasts.publication.unrevoked.is_empty());
}
// Shared (for example from another device) but held back here: the
// key asks for this device's confirmation instead of unsharing.
let mut state = ready(vec![shared_row()]);
+168 -41
View File
@@ -18,10 +18,11 @@
//! every own publication that is not (share turned off or unsubscribed on
//! any trusted device). A stale device cannot republish, because its
//! registers lose to the newer share decision once they converge.
//! * [`spawn`] runs the reconciler off the UI thread: after local share and
//! unsubscribe actions, after received podcast operations, after feed
//! refreshes (card metadata), when the overlay (re)starts, and
//! periodically.
//! * [`spawn`] runs the reconciler off the UI thread: right after local
//! share-off and unsubscribe actions (quitting waits for those,
//! [`Reconciler::flush`]), after other local share actions, received
//! podcast operations and feed refreshes (card metadata), when the overlay
//! (re)starts, and periodically.
//! * [`ShareConfirmations`] remembers, on this device only, that the user
//! confirmed publishing feed URLs that look private
//! (`PodcastCardSpec::privacy_warnings`). FRID leaves this confirmation to
@@ -665,12 +666,24 @@ pub struct PublicationReport {
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(_))
{
/// this device; `was_shared`: it was shared just before. Until the next
/// pass says otherwise, a card this device may have published counts as
/// not revoked yet.
///
/// While the overlay runs, the last pass says whether a card was
/// published (a held-back one never was). Without a pass of the running
/// overlay (federation off, or not started yet) this device cannot tell
/// whether a card from an earlier session is still live, so a podcast
/// that was shared counts as not revoked.
pub fn note_revocation(&mut self, podcast_id: &PodcastId, was_shared: bool) {
let publication = self.podcasts.remove(podcast_id);
let maybe_live = match self.discovery {
Discovery::Running { .. } => publication.map_or(was_shared, |publication| {
!matches!(publication, Publication::HeldBack(_))
}),
Discovery::Off(_) | Discovery::Starting => was_shared,
};
if maybe_live {
self.unrevoked.insert(podcast_id.clone());
}
}
@@ -850,9 +863,46 @@ pub struct Reconciler {
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>,
/// Revocations whose decision is still being written
/// ([`Reconciler::begin_revocation`]).
in_flight: Arc<watch::Sender<usize>>,
/// Podcasts revoked here that were shared just before, not yet seen by
/// a pass. A pass without the overlay keeps them as not revoked.
revoked: Arc<Mutex<BTreeSet<PodcastId>>>,
/// The latest finished pass.
covered: watch::Receiver<Covered>,
}
/// What the latest finished pass covered.
#[derive(Debug, Clone, Copy, Default)]
struct Covered {
/// Revocations requested before it started.
revocations: u64,
/// Cards of private podcasts it found (or kept as) not revoked.
unrevoked: usize,
}
/// A share-off or unsubscribe being written: quitting waits for it
/// ([`Reconciler::flush`]). Dropping it without [`RevocationTicket::done`]
/// means the decision was not written.
#[derive(Debug)]
pub struct RevocationTicket {
reconciler: Reconciler,
}
impl RevocationTicket {
/// The decision was written: revoke now ([`Reconciler::revoke_now`]).
pub fn done(self, was_shared: Option<&PodcastId>) {
self.reconciler.revoke_now(was_shared);
}
}
impl Drop for RevocationTicket {
fn drop(&mut self) {
self.reconciler
.in_flight
.send_modify(|in_flight| *in_flight = in_flight.saturating_sub(1));
}
}
impl Reconciler {
@@ -862,38 +912,99 @@ impl Reconciler {
self.wake.notify_one();
}
/// A share-off or unsubscribe is about to be written (off the UI
/// thread): from now on, quitting waits for it to be written and
/// revoked. Call before starting the write, so a quit that is handled
/// before the write finishes still waits.
pub fn begin_revocation(&self) -> RevocationTicket {
self.in_flight.send_modify(|in_flight| *in_flight += 1);
RevocationTicket {
reconciler: self.clone(),
}
}
/// 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) {
/// before the user may quit. `was_shared`: the podcast, when it was
/// shared just before (a card from this device may be live). See
/// [`Reconciler::flush`].
pub fn revoke_now(&self, was_shared: Option<&PodcastId>) {
if let Some(podcast_id) = was_shared {
self.revoked
.lock()
.unwrap_or_else(PoisonError::into_inner)
.insert(podcast_id.clone());
}
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
/// Waits, at most `timeout`, until the revocations being written
/// ([`Reconciler::begin_revocation`]) are written and 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. `true` when
/// that pass left no card of a private podcast live from this device;
/// `false` when one may be (revocation failed, or the federation is off
/// and cards of an earlier session may be live), 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(_))
)
let wait = async {
let mut in_flight = self.in_flight.subscribe();
if in_flight
.wait_for(|in_flight| *in_flight == 0)
.await
.is_err()
{
return false;
}
let target = self.revocations.load(Ordering::SeqCst);
let mut covered = self.covered.clone();
// `revoke_now` already asked for the pass.
match covered
.wait_for(|covered| covered.revocations >= target)
.await
{
Ok(covered) => covered.unrevoked == 0,
Err(_) => false,
}
};
tokio::time::timeout(timeout, wait).await.unwrap_or(false)
}
}
/// The podcasts of `unrevoked` that are not shared now (a podcast shared
/// again is no longer "private · not revoked"). Without a store, all.
async fn still_private(
store: Option<&Arc<PodcastStore>>,
unrevoked: BTreeSet<PodcastId>,
) -> BTreeSet<PodcastId> {
let Some(store) = store.cloned() else {
return unrevoked;
};
if unrevoked.is_empty() {
return unrevoked;
}
let fallback = unrevoked.clone();
tokio::task::spawn_blocking(move || {
unrevoked
.into_iter()
.filter(|podcast_id| {
!matches!(store.podcast(podcast_id), Ok(Some(summary)) if summary.shared)
})
.collect()
})
.await
.unwrap_or(fallback)
}
/// 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 (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`].
/// device and `report` says so, keeping as not revoked the cards known not
/// to be revoked yet and those of podcasts that were shared and revoked
/// here meanwhile (`Reconciler::revoke_now`): 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>>,
@@ -903,11 +1014,14 @@ pub fn spawn(
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 revoked = Arc::new(Mutex::new(BTreeSet::new()));
let (covered_tx, covered) = watch::channel(Covered::default());
let reconciler = Reconciler {
wake: Arc::clone(&wake),
urgent: Arc::clone(&urgent),
revocations: Arc::clone(&revocations),
in_flight: Arc::new(watch::channel(0).0),
revoked: Arc::clone(&revoked),
covered,
};
tokio::spawn(async move {
@@ -916,20 +1030,33 @@ pub fn spawn(
peers.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay);
peers.reset();
loop {
// Read before the revoked podcasts: `revoke_now` records the
// podcast before it counts the revocation.
let requested = revocations.load(Ordering::SeqCst);
let revoked_here =
std::mem::take(&mut *revoked.lock().unwrap_or_else(PoisonError::into_inner));
let current = dht.borrow_and_update().clone();
last = match current {
// The pass sees this device's own publications: it revokes
// and reports exactly what is still live.
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()
},
None => {
let mut unrevoked = std::mem::take(&mut last.unrevoked);
unrevoked.extend(revoked_here);
PublicationReport {
discovery: Discovery::Off(
"federation is off or podcast discovery is unavailable".into(),
),
unrevoked: still_private(store.as_ref(), unrevoked).await,
..PublicationReport::default()
}
}
};
report(last.clone());
covered_tx.send_replace(requested);
covered_tx.send_replace(Covered {
revocations: requested,
unrevoked: last.unrevoked.len(),
});
let periodic = tokio::time::sleep(RECONCILE_INTERVAL);
tokio::pin!(periodic);
loop {
+57 -4
View File
@@ -616,10 +616,26 @@ fn revocations_that_fail_are_reported_without_ids() {
report
.podcasts
.insert(secret.clone(), Publication::HeldBack(Vec::new()));
report.note_revocation(&show);
report.note_revocation(&secret);
report.discovery = Discovery::Running { peers: 1 };
report.note_revocation(&show, true);
report.note_revocation(&secret, true);
assert!(report.podcasts.is_empty());
assert_eq!(report.unrevoked, [show].into_iter().collect());
assert_eq!(report.unrevoked, [show.clone()].into_iter().collect());
// Without a pass of the running overlay nothing tells whether a card
// of an earlier session is live: a podcast that was shared counts.
for discovery in [
Discovery::Off("federation is off".into()),
Discovery::Starting,
] {
let mut report = PublicationReport {
discovery,
..PublicationReport::default()
};
report.note_revocation(&show, true);
report.note_revocation(&diary, false);
assert_eq!(report.unrevoked, [show.clone()].into_iter().collect());
}
}
// ---------------------------------------------------------------------------
@@ -930,6 +946,43 @@ async fn reconciler_lifecycle() {
assert!(report.podcasts.is_empty());
assert!(reconciler.flush(Duration::from_millis(10)).await);
// Sharing off while the federation is off (a card of an earlier
// session may be live). Quitting while the decision is still being
// written waits for the write and the pass, then says the card may not
// be revoked; the Off reports keep it as not revoked.
let ticket = reconciler.begin_revocation();
assert!(
!reconciler.flush(Duration::from_millis(50)).await,
"the write is still running"
);
let flush = tokio::spawn({
let reconciler = reconciler.clone();
async move { reconciler.flush(Duration::from_secs(5)).await }
});
tokio::time::sleep(Duration::from_millis(100)).await;
assert!(!flush.is_finished(), "quitting waits for the write");
device.set_shared(&store, &show, false);
ticket.done(Some(&show));
assert!(!flush.await.unwrap(), "the card may not be revoked yet");
let report = next().await;
assert!(matches!(report.discovery, Discovery::Off(_)));
assert_eq!(report.unrevoked, [show.clone()].into_iter().collect());
reconciler.wake();
let report = next().await;
assert_eq!(
report.unrevoked,
[show.clone()].into_iter().collect(),
"kept while the federation is off"
);
// A write that failed (the ticket is dropped) is not waited for.
drop(reconciler.begin_revocation());
// Shared again: no longer "private · not revoked".
device.set_shared(&store, &show, true);
reconciler.wake();
let report = next().await;
assert!(report.unrevoked.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;
@@ -977,7 +1030,7 @@ async fn reconciler_lifecycle() {
// 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();
reconciler.revoke_now(Some(&show));
assert!(
reconciler.flush(RECONCILE_DEBOUNCE * 9 / 10).await,
"revoked before the debounce would have ended"
+3 -2
View File
@@ -228,7 +228,7 @@ mod tests {
state
.podcasts
.publication
.note_revocation(&podcast_fixtures::podcast_id());
.note_revocation(&podcast_fixtures::podcast_id(), true);
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 {
@@ -239,7 +239,8 @@ mod tests {
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}");
assert!(screen.contains("may not be revoked yet:"), "{screen}");
assert!(screen.contains("federation is off"), "{screen}");
state.podcasts.publication.unrevoked.clear();
let screen = render(&state, 110, 30);
assert!(screen.contains("private (not shared"), "{screen}");
+1 -1
View File
@@ -323,7 +323,7 @@ pub(crate) fn share_status_line(state: &AppState, podcast: &PodcastRow) -> Line<
}
if let Discovery::Off(_) = report.discovery {
return Line::styled(
"private · public card not revoked yet: federation is off",
"private · public card may not be revoked yet: federation is off",
Style::new().fg(Color::Yellow),
);
}