From 0d67dd7433211d89019bc4710821728d38e16bf9 Mon Sep 17 00:00:00 2001 From: faumaray Date: Sat, 24 Oct 2026 14:24:30 +0300 Subject: [PATCH] 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 --- crates/voelin-core/tests/voice_features.rs | 25 ++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/crates/voelin-core/tests/voice_features.rs b/crates/voelin-core/tests/voice_features.rs index 6b4cfb8..69f7d99 100644 --- a/crates/voelin-core/tests/voice_features.rs +++ b/crates/voelin-core/tests/voice_features.rs @@ -3,6 +3,7 @@ //! in the client database). The round trips against real servers are in //! `scripts/it-smoke.sh` (`voelinctl engine`). +use std::path::{Path, PathBuf}; use std::time::Duration; use tokio::sync::broadcast; @@ -22,6 +23,29 @@ async fn next(rx: &mut broadcast::Receiver, mut f: impl FnMut(Event) - .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 +/// (`-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")] async fn requests_without_voice_fail_with_their_ids() { let engine = Engine::start(); @@ -165,6 +189,7 @@ async fn contacts_are_kept_in_the_database() { engine.history().flush(); } // A new engine attaches the database later: the contacts come back. + closed(&path).await; let engine = Engine::start(); let mut rx = engine.subscribe(); engine.send(Command::AttachHistory(History::open(&path).unwrap()));