Skip to content

v0.32.0 can no longer be used as an axum state (!Sync) #52

@chesedo

Description

@chesedo

The following minimal Axum code used to work with v0.31.0, but no longer works with the last version. I believe this is because the local client is no longer Sync in v0.32.0 (while it was in v0.31.0).

/// cargo add axum libsql-client
/// cargo add tokio --features rt-multi-thread
use std::sync::Arc;

use axum::{extract::State, routing::get, Router};
use libsql_client::Client;

async fn root(State(client): State<Arc<Client>>) -> String {
    let result = client
        .execute("SELECT 'hello, world!'")
        .await
        .unwrap()
        .rows
        .first()
        .map(|row| row.values.get(0))
        .flatten()
        .map(|value| value.to_string());

    result.unwrap()
}

#[tokio::main]
async fn main() {
    let client = libsql_client::Client::from_config(libsql_client::Config {
        url: format!(
            "file:///{}/example.db",
            std::env::current_dir().unwrap().display()
        )
        .parse()
        .unwrap(),
        auth_token: None,
    })
    .await
    .unwrap();

    let state = Arc::new(client);

    // This line will have a bunch of errors for the route
    let app = Router::new().route("/", get(root)).with_state(state);

    axum::Server::bind(&"0.0.0.0:3000".parse().unwrap())
        .serve(app.into_make_service())
        .await
        .unwrap();
}

Error

With v0.32.0 the following snippet of the compile error is interesting:

*mut libsql_sys::ffi::sqlite3` cannot be shared between threads safely

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions