Tokio OnceCell + Sqlx Pool seems to be stuck
⚓ Rust 📅 2026-04-26 👤 surdeus 👁️ 2I'm trying to create a static database pool in a Dioxus project using tokio OnceCell, in the example bellow as I try to create de database pool it seems to get locked forever. I'm suspecting this is not really a Dioxus problem but something about the lock I'm not understanding:
#[cfg(feature = "server")]
pub(crate) static POOL: tokio::sync::OnceCell<sqlx::MySqlPool> = tokio::sync::OnceCell::const_new();
// The prinln! on this funtion output in this order:
//
// TRYING TO GET POOL
// POOL INIT RUNNING
// TRYING TO GET POOL
//
// And after that the page just keeps loading forever
#[cfg(feature = "server")]
pub(crate) async fn db_pool() -> Result<&'static sqlx::MySqlPool, sqlx::Error> {
println!("TRYING TO GET POOL");
let pool = POOL.get_or_try_init(||
println!("POOL INIT RUNNING");
sqlx::Pool::PoolOptions::new().connect_with(
sqlx::mysql::MySqlConnectWith::new()
// pasing database connect params ...
)
).await;
println!("RETURNING POOL");
pool
}
#[server]
pub async fn resouce_one() -> Result<Vec<ResourceOne>, MyErr> {
let pool = db_pool.await.map_err(MyErr::Database)?;
//run sqlx::query_as
}
#[server]
pub async fn resouce_two() -> Result<Vec<ResourceTwo>, MyErr> {
let pool = db_pool.await.map_err(MyErr::Database)?;
//run sqlx::query_as
}
#[component]
pub fn MyComponent() -> Element {
// Syntax sugar, these will make two requests to an Axum server using Reqwest
// creating contention on the lock, which I'm apparently not handling properly
// at least its what I think is going on
let resource_one = use_loader(|| resource_one());
let resource_two = use_loader(|| resource_two());
rsx! {
div {
ErrorBoundary {
handle_err: |_| rsx! { "NOPE" },
ResourceOne { data: resource_one?.read().clone() }
ResourceTwo { data: resource_two?.read().clone() }
}
}
}
}
1 post - 1 participant
🏷️ Rust_feed