voelin-core: load contacts before handling the next command

The contacts read from the database replaced the list in memory when the
read finished on the writer thread. A SetContact handled meanwhile was
lost (its write was queued behind the read), which made
contacts_are_kept_in_the_database fail under load. The engine now awaits
the read at start and after AttachHistory before its next command.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NJvYyRmTjKcb9i8eyYXK5G
This commit is contained in:
Claude committed 2026-09-29 01:41:47 +00:00
1 parent 628bc11a04
commit 2f35cd0057
2 files changed
+14 -16

No files matched your search

+11 -14
View File
@@ -110,19 +110,16 @@ impl Contacts {
} }
/// Read the contacts of the current database (after it was attached). /// Read the contacts of the current database (after it was attached).
pub fn load(&self) { /// The list read replaces the one in memory, so the engine awaits this
let this = self.clone(); /// before its next command: a contact set meanwhile would be lost.
self.history.current().run_then( pub async fn load(&self) {
false, match self.history.current().run(false, |s| s.contacts()).await {
|s| s.contacts(), Ok(list) => {
move |result| match result { self.lock().contacts = list.into_iter().map(|c| (c.uid.clone(), c)).collect();
Ok(list) => { self.changed();
this.lock().contacts = list.into_iter().map(|c| (c.uid.clone(), c)).collect(); }
this.changed(); Err(e) => warn!("cannot read contacts: {e}"),
} }
Err(e) => warn!("cannot read contacts: {e}"),
},
);
} }
/// Add or change a contact. /// Add or change a contact.
@@ -410,7 +407,7 @@ mod tests {
history.current().run(false, |s| s.put_contact(&friend("a="))).await.unwrap(); history.current().run(false, |s| s.put_contact(&friend("a="))).await.unwrap();
let contacts = Contacts::new(events, history.clone()); let contacts = Contacts::new(events, history.clone());
let mut revision = contacts.watch(); let mut revision = contacts.watch();
contacts.load(); contacts.load().await;
let loaded = loop { let loaded = loop {
if let Event::ContactsChanged { contacts } = rx.recv().await.unwrap() { if let Event::ContactsChanged { contacts } = rx.recv().await.unwrap() {
break contacts; break contacts;
+3 -2
View File
@@ -676,7 +676,6 @@ impl Engine {
history, history,
cache: SharedCache::new(Cache::new(Cache::default_dir())), cache: SharedCache::new(Cache::new(Cache::default_dir())),
}; };
shared.contacts.load();
tokio::spawn(run(rx, events.clone(), frames.clone(), shared.clone())); tokio::spawn(run(rx, events.clone(), frames.clone(), shared.clone()));
Self { commands, events, frames, runtime: tokio::runtime::Handle::current(), shared } Self { commands, events, frames, runtime: tokio::runtime::Handle::current(), shared }
} }
@@ -763,6 +762,8 @@ async fn run(
engine: Shared, engine: Shared,
) { ) {
let Shared { settings: shared, history, cache, contacts } = engine.clone(); let Shared { settings: shared, history, cache, contacts } = engine.clone();
// Before any command: a contact set meanwhile would be lost.
contacts.load().await;
let mut sessions: HashMap<SessionId, session::SessionHandle> = HashMap::new(); let mut sessions: HashMap<SessionId, session::SessionHandle> = HashMap::new();
let mut current = shared.current(); let mut current = shared.current();
// Pruning: at start, then hourly (and when the setting changes). // Pruning: at start, then hourly (and when the setting changes).
@@ -851,7 +852,7 @@ async fn run(
history.replace(new.clone()); history.replace(new.clone());
prune_history(&current, &new); prune_history(&current, &new);
// The contacts live in the same database. // The contacts live in the same database.
contacts.load(); contacts.load().await;
continue; continue;
} }
Command::AttachCache(dir) => { Command::AttachCache(dir) => {