Drop share confirmations whose decision was not written

A confirmation is recorded as pending before the share decision is
written and covers the next newer decision. When the write failed (the
podcast was unsubscribed by a sync meanwhile, a database error), the
pending entry stayed and later covered a decision taken on another
device that this user never saw. Put the previous entry back on failure,
and drop pending entries left in the file by a call that never finished.

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 405f8ae6d3
commit 43e0696531
2 files changed
+115 -2

No files matched your search

+43 -1
View File
@@ -189,6 +189,13 @@ impl ShareConfirmations {
BTreeMap::new()
}
};
// A pending confirmation only lives between `confirm` and `bind`
// within one `set_shared` call; one found at start belongs to a
// call that never finished (the decision may not have been written)
// and must not cover a later decision taken elsewhere.
let mut confirmed = confirmed;
confirmed
.retain(|_, confirmation| !matches!(confirmation.decision, Decision::Pending { .. }));
Self {
path: Some(path),
confirmed: Mutex::new(confirmed),
@@ -276,6 +283,29 @@ impl ShareConfirmations {
self.update(podcast_id, |entry| *entry = None)
}
/// The confirmation of a podcast as it is now, to put back with
/// [`ShareConfirmations::restore`].
fn entry(&self, podcast_id: &PodcastId) -> Option<Confirmation> {
self.lock().get(podcast_id).cloned()
}
/// Puts back the confirmation `previous` of a podcast (the decision it
/// was given for could not be written). Takes effect in memory even
/// when the file cannot be saved: a pending confirmation left in the
/// file is dropped when it is read again ([`ShareConfirmations::open`]).
/// Blocking.
fn restore(&self, podcast_id: &PodcastId, previous: Option<Confirmation>) -> Result<()> {
let mut confirmed = self.lock();
if confirmed.get(podcast_id) == previous.as_ref() {
return Ok(());
}
match previous {
Some(confirmation) => confirmed.insert(podcast_id.clone(), confirmation),
None => confirmed.remove(podcast_id),
};
self.save(&confirmed)
}
/// Applies `change` to the entry of `podcast_id` and saves the file when
/// the entry changed.
fn update(
@@ -459,11 +489,23 @@ pub fn set_shared(
confirm: Option<ConfirmedUrls>,
record: impl FnOnce() -> Result<()>,
) -> Result<()> {
// What to put back if the decision cannot be written: a pending
// confirmation must not outlive this call, or it would cover the next
// share decision taken on any device, which this user never saw.
let mut previous = None;
if let Some(urls) = confirm {
let share = store.share(podcast_id)?;
previous = Some(confirmations.entry(podcast_id));
confirmations.confirm(podcast_id, urls, share.as_ref())?;
}
record()?;
if let Err(err) = record() {
if let Some(previous) = previous
&& let Err(restore) = confirmations.restore(podcast_id, previous)
{
tracing::warn!("podcast share confirmation not restored: {restore:#}");
}
return Err(err);
}
if !shared {
return confirmations.forget(podcast_id);
}
+72 -1
View File
@@ -16,7 +16,7 @@ use music_dht::{MusicDhtConfig, MusicDhtService, NetworkId};
use super::publication::{
Discovery, Publication, PublicationReport, ShareConfirmations, check_share, reconcile,
set_shared, share_card, share_states, summarize, warning_ru,
set_shared, share_card, share_plan, share_states, summarize, warning_ru,
};
const SHOW_URL: &str = "https://feeds.example.test/rust-radio.xml";
@@ -393,6 +393,77 @@ fn a_confirmation_does_not_outlive_a_revocation_on_another_device() {
);
}
#[test]
fn a_confirmation_for_a_decision_that_was_not_written_is_dropped() {
let temp = tempfile::tempdir().unwrap();
let path = temp.path().join("confirmations.json");
let store = PodcastStore::open_in_memory().unwrap();
let confirmations = ShareConfirmations::open(path.clone());
let mut device = Device::new("device-a");
let secret = device.subscribe(&store, SECRET_URL, "Members Only");
let other = PodcastStore::open_in_memory().unwrap();
let mut device_b = Device::new("device-b");
// The user confirms, but the decision cannot be written (for example
// the podcast was unsubscribed by a sync meanwhile, or the database
// failed): nothing of the confirmation is left.
let check = check_share(&store, &confirmations, &secret).unwrap();
assert!(check.needs_confirmation());
let result = set_shared(
&store,
&confirmations,
&secret,
true,
Some(check.urls.clone()),
|| anyhow::bail!("not subscribed"),
);
assert!(result.is_err());
assert!(shared_ids(&store, &confirmations).is_empty());
// Days later sharing is turned on on device B: A was never asked for
// that decision, so it holds the podcast back.
for op in &device.ops {
other.apply_remote_op(op).unwrap();
}
for op in &device_b.set_shared(&other, &secret, true) {
store.apply_remote_op(op).unwrap();
}
assert!(!confirmed_here(&store, &confirmations, &secret));
assert!(
share_plan(&store, &confirmations)
.unwrap()
.held
.contains_key(&secret),
"held back on A"
);
assert!(
check_share(&store, &confirmations, &secret)
.unwrap()
.needs_confirmation()
);
assert!(!ShareConfirmations::open(path.clone()).is_confirmed(
&share_card(&store, &secret).unwrap().unwrap(),
store.share(&secret).unwrap().as_ref()
));
// A pending confirmation left in the file by a call that never
// finished (the app was killed before the decision was written) is
// dropped when the file is read again.
let fresh = device.subscribe(
&store,
"https://feeds.example.test/vip.xml?key=0123456789abcdef",
"VIP",
);
let check = check_share(&store, &confirmations, &fresh).unwrap();
confirmations
.confirm(&fresh, check.urls, store.share(&fresh).unwrap().as_ref())
.unwrap();
let reopened = ShareConfirmations::open(path);
device.set_shared(&store, &fresh, true);
assert!(confirmed_here(&store, &confirmations, &fresh));
assert!(!confirmed_here(&store, &reopened, &fresh));
}
#[test]
fn cards_leave_out_artwork_and_links_that_look_private() {
let store = PodcastStore::open_in_memory().unwrap();