ryanfowler / async-sqlite

A Rust library to interact with sqlite from an async context.
MIT License
8 stars 4 forks source link

`DatabaseBusy` error on pool creation with a new file #10

Open tomasol opened 5 months ago

tomasol commented 5 months ago

Hi, thanks for the library. I am running into an issue during tests, where I am creating a temporary database with a pool:

let pool = PoolBuilder::new()
            .path(path)
            .journal_mode(JournalMode::Wal)
            .open()
            .await?;

Occasionally I get an error:

Rusqlite(SqliteFailure(Error { code: DatabaseBusy, extended_code: 5 }, Some("database is locked")))

The workaround is to create the file before creating the pool:

let client = ClientBuilder::new()
            .path(&path)
            .journal_mode(JournalMode::Wal)
            .open()
            .await?;
        client.close().await?;
let pool = ...

Not sure if this is worth fixing or just mentioning in docs.

ryanfowler commented 1 month ago

Sorry for the delayed response, but thanks for filing an issue! I'm curious, what connection parameters are you using to connect?

tomasol commented 1 month ago

No problem. Here is a more complete example:

async_test!(check_init);

async fn check_init() {
    let file = tempfile::NamedTempFile::new().unwrap();
    let path = file.path();
    let pool = PoolBuilder::new()
        .path(path)
        .journal_mode(JournalMode::Wal)
        // .num_conns(1)
        .open()
        .await
        .unwrap();
}

Running the test in a loop fails within seconds.

while true; do cargo test --workspace check_init || break; done

Note that the file must not exist before the test. The other workaround is to set num_conns(1).