Reopen the contacts database after the first engine has closed it

voice_features::contacts_are_kept_in_the_database dropped its engine and opened
the same database again at once. Dropping an Engine does not close the history
database: the engine task ends later, the history writer is joined there, and
closing the connection checkpoints the write-ahead log under an exclusive lock
with fsyncs. The second History::open waits for that lock only for the 5 s busy
timeout, and fails with DatabaseBusy when a loaded disk makes the close take
longer (seen as 3 failures in 8 runs, 10 in 24 with eight runs at a time).

The test now waits until the -wal file is gone, which SQLite does when the last
connection closes, before it reopens: the restart of a process, as the test
means it. With every fsync of the test process delayed by 6 s (strace -e
inject=fsync,fdatasync:delay_enter=6000000) the old test failed every time at
the reopen with DatabaseBusy, and the new one passes.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
faumarayandClaude Sonnet 5.5 committed 2026-10-24 14:24:30 +03:00
1 parent 9254e08daa
commit 0d67dd7433
1 file changed
+25
@@ -3,6 +3,7 @@
//! in the client database). The round trips against real servers are in //! in the client database). The round trips against real servers are in
//! `scripts/it-smoke.sh` (`voelinctl engine`). //! `scripts/it-smoke.sh` (`voelinctl engine`).
use std::path::{Path, PathBuf};
use std::time::Duration; use std::time::Duration;
use tokio::sync::broadcast; use tokio::sync::broadcast;
@@ -22,6 +23,29 @@ async fn next<T>(rx: &mut broadcast::Receiver<Event>, mut f: impl FnMut(Event) -
.expect("event in time") .expect("event in time")
} }
/// Wait until nothing has the SQLite database at `path` open any more, as when
/// the process that had it has ended: SQLite deletes the write-ahead log
/// (`<path>-wal`) when the last connection closes.
///
/// A dropped `Engine` is not that yet. Its task ends later, the history writer
/// is joined there, and closing the connection checkpoints the log under an
/// exclusive lock, with fsyncs. A connection opened meanwhile waits for the
/// lock only as long as the busy timeout (5 s) and then fails with
/// `DatabaseBusy`, which a loaded disk makes happen (with every fsync taking
/// 6 s it happens every time).
async fn closed(path: &Path) {
let mut wal = path.as_os_str().to_owned();
wal.push("-wal");
let wal = PathBuf::from(wal);
tokio::time::timeout(Duration::from_secs(120), async {
while wal.exists() {
tokio::time::sleep(Duration::from_millis(10)).await;
}
})
.await
.expect("the engine's database connection closed");
}
#[tokio::test(flavor = "multi_thread")] #[tokio::test(flavor = "multi_thread")]
async fn requests_without_voice_fail_with_their_ids() { async fn requests_without_voice_fail_with_their_ids() {
let engine = Engine::start(); let engine = Engine::start();
@@ -165,6 +189,7 @@ async fn contacts_are_kept_in_the_database() {
engine.history().flush(); engine.history().flush();
} }
// A new engine attaches the database later: the contacts come back. // A new engine attaches the database later: the contacts come back.
closed(&path).await;
let engine = Engine::start(); let engine = Engine::start();
let mut rx = engine.subscribe(); let mut rx = engine.subscribe();
engine.send(Command::AttachHistory(History::open(&path).unwrap())); engine.send(Command::AttachHistory(History::open(&path).unwrap()));