Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
chesedo opened this issue Oct 29, 2023 · 4 comments
Open

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

chesedo opened this issue Oct 29, 2023 · 4 comments

Comments

@chesedo
Copy link

chesedo commented Oct 29, 2023

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
@lypanov
Copy link

lypanov commented Nov 1, 2023

This blocked my getting started experience with Turso entirely alas.

@Naveenravi07
Copy link

Same here

@digizeph
Copy link

digizeph commented Dec 8, 2023

Failed to build for 0.33. Had to revert back to 0.31. I wonder what was the underlying change introduced causing this.

@LucioFranco
Copy link
Contributor

Hi all, sorry about the delay. We have actually been working on a new libsql client that lives in https://github.com/tursodatabase/libsql we are almost ready to publish it to cratesio which we can do next week. This client explicitly supports working in axum because it implements both Send and Sync so it should solve this problem. We plan on deprecating this library soon. In the meantime its quite stable so feel free to try it out via:

libsql = { git = "https://github.com/tursodatabase/libsql" }

if you just want to compile the the http only features (not do any C building to build the sqlite3 code) you can do that like so:

libsql = { git = "https://github.com/tursodatabase/libsql", default-features = false, features = ["remote"] }

This example shows you how you can connect either via the http/remote protocol or open a normal in memory libsql-sqlite3 database.

Feel free to ping me on discord if you run into any issues!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants