Skip to content

Your Android phone and your Windows PC, working as one. Local-first, end-to-end encrypted, no account.

fix(core): cancelling stops a live send even if the queue can't be updated, and unpairing is all or nothing

Bee9e9d70db1c16
diff --git a/core/nectarlink-core/src/node.rs b/core/nectarlink-core/src/node.rs
index 891e791..0ed8d72 100644
--- a/core/nectarlink-core/src/node.rs
+++ b/core/nectarlink-core/src/node.rs
@@ -434,7 +434,8 @@ impl Shared {
pub(crate) fn cancel_queue_or_transfer(self: &Arc<Self>, id: &str) -> Result<bool> {
self.localsend.resolve_transfer_approval(id, false);
- let removed_queue = self.store.remove_queue_item(id)?;
+ // The live transfer is cancelled first, so a database error can't
+ // leave it running.
let target = {
let mut map = lock(&self.transfers);
if let Some(ctrl) = map.get_mut(id) {
@@ -451,6 +452,15 @@ impl Shared {
None
}
};
+ let removed_queue = match self.store.remove_queue_item(id) {
+ Ok(removed) => removed,
+ // Only an error when there was nothing live to cancel either.
+ Err(e) if target.is_none() => return Err(e),
+ Err(e) => {
+ tracing::warn!(error = %e, %id, "can't remove a cancelled send from the queue");
+ None
+ }
+ };
if let Some((ref q_item, ref payload)) = removed_queue {
transfer::cleanup_staging_id(self, id);
@@ -1015,12 +1025,11 @@ impl Shared {
for id in active_transfer_ids {
lock(&self.transfers).remove(&id);
}
- let dropped_queue = self.store.drop_all_queue_for_peer(peer).unwrap_or_default();
+ // One transaction: an error leaves both the device and its queue.
+ let (existed, dropped_queue) = self.store.remove_peer_with_queue(peer)?;
for (id, _) in &dropped_queue {
transfer::cleanup_staging_id(self, id);
}
-
- let existed = self.store.remove_peer(peer)?;
if let Some(sup) = lock(&self.supervisors).remove(peer) {
sup.cancel.cancel();
}
diff --git a/core/nectarlink-core/src/store.rs b/core/nectarlink-core/src/store.rs
index f7ffcbb..d307cb4 100644
--- a/core/nectarlink-core/src/store.rs
+++ b/core/nectarlink-core/src/store.rs
@@ -796,6 +796,8 @@ impl Store {
}
/// Removes a device. Returns whether it existed.
+ /// The device alone; the app unpairs with [`Store::remove_peer_with_queue`].
+ #[cfg(test)]
pub fn remove_peer(&self, id: &DeviceId) -> Result<bool> {
let peer_str = id.to_string();
self.with(|c| {
@@ -808,6 +810,39 @@ impl Store {
})
}
+ /// Removes a device and its send queue together, so unpairing never drops
+ /// the queue but keeps the device (or the reverse). Returns whether it
+ /// existed and the queue items that went with it.
+ pub fn remove_peer_with_queue(&self, id: &DeviceId) -> Result<(bool, Vec<(String, QueuePayload)>)> {
+ let peer_str = id.to_string();
+ let (existed, rows) = self.with(|c| {
+ let tx = c.unchecked_transaction()?;
+ let rows: Vec<(String, String)> = {
+ let mut stmt =
+ tx.prepare_cached("SELECT id, payload_json FROM send_queue WHERE peer_id = ?1")?;
+ stmt.query_map([&peer_str], |r| Ok((r.get(0)?, r.get(1)?)))?
+ .collect::<rusqlite::Result<Vec<_>>>()?
+ };
+ tx.execute("DELETE FROM send_queue WHERE peer_id = ?1", [&peer_str])?;
+ tx.execute("DELETE FROM chat_messages WHERE peer = ?1", [&peer_str])?;
+ tx.execute("DELETE FROM chat_threads WHERE peer = ?1", [&peer_str])?;
+ let existed = tx.execute("DELETE FROM peers WHERE id = ?1", [id.as_bytes().as_slice()])? > 0;
+ tx.commit()?;
+ Ok((existed, rows))
+ })?;
+ // Its toggles went with it.
+ self.toggle_cache().remove(id);
+ let dropped: Vec<(String, QueuePayload)> = rows
+ .into_iter()
+ .filter_map(|(id, json)| serde_json::from_str::<QueuePayload>(&json).ok().map(|p| (id, p)))
+ .collect();
+ // After the commit: files are only removed once nothing refers to them.
+ for (id, payload) in &dropped {
+ cleanup_staged_payload(self.data_dir.as_deref(), id, Some(payload));
+ }
+ Ok((existed, dropped))
+ }
+
pub fn get_peer(&self, id: &DeviceId) -> Result<Option<PeerRecord>> {
self.with(|c| {
c.query_row(&format!("{SELECT} WHERE id = ?1"), [id.as_bytes().as_slice()], row_to_peer)
@@ -1984,27 +2019,6 @@ impl Store {
}
Ok(removed)
}
-
- /// Deletes all queue items (`'waiting'`, `'sending'`, `'failed'`, `'expired'`) for `peer`,
- /// cleans up their staged files, and returns their `(id, payload)` pairs.
- pub fn drop_all_queue_for_peer(&self, peer: &DeviceId) -> Result<Vec<(String, QueuePayload)>> {
- let peer_str = peer.to_string();
- let dropped: Vec<(String, QueuePayload)> = self.with(|c| {
- let mut stmt = c.prepare_cached("SELECT id, payload_json FROM send_queue WHERE peer_id = ?1")?;
- let rows: Vec<(String, String)> = stmt
- .query_map([&peer_str], |r| Ok((r.get(0)?, r.get(1)?)))?
- .collect::<rusqlite::Result<Vec<_>>>()?;
- c.execute("DELETE FROM send_queue WHERE peer_id = ?1", [&peer_str])?;
- Ok(rows
- .into_iter()
- .filter_map(|(id, json)| serde_json::from_str::<QueuePayload>(&json).ok().map(|p| (id, p)))
- .collect())
- })?;
- for (id, payload) in &dropped {
- cleanup_staged_payload(self.data_dir.as_deref(), id, Some(payload));
- }
- Ok(dropped)
- }
}
const fn bytes_from_len(len: usize) -> u64 {
@@ -3683,10 +3697,13 @@ mod tests {
assert_eq!(store.expire_stale_queue_items(now_ms + 200).unwrap(), 1);
assert!(!dir.path().join("queue_staging").join("q-expire").exists());
- // Terminal path 5: Unpair (drop_all_queue_for_peer)
- store.drop_all_queue_for_peer(&peer).unwrap();
+ // Terminal path 5: Unpair, which removes the device and its queue together.
+ let (existed, dropped) = store.remove_peer_with_queue(&peer).unwrap();
+ assert!(existed);
+ assert!(dropped.iter().any(|(id, _)| id == "q-unpair"));
assert!(!dir.path().join("queue_staging").join("q-unpair").exists());
assert!(store.list_send_queue(Some(&peer)).unwrap().is_empty());
+ assert!(store.get_peer(&peer).unwrap().is_none());
}
#[test]