From 66105cde1f8eee62721f5ebe4bc8cdb5e94a82cd Mon Sep 17 00:00:00 2001 From: Avinash Sajjanshetty Date: Wed, 16 Apr 2025 18:52:11 +0530 Subject: [PATCH 1/2] bugfix: fix currect use of `block_on` --- libsql/src/database.rs | 42 +++++++++++++++++++++++++++++++----------- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/libsql/src/database.rs b/libsql/src/database.rs index da0cc7dfac..501f244810 100644 --- a/libsql/src/database.rs +++ b/libsql/src/database.rs @@ -682,17 +682,37 @@ impl Database { }; use tokio::sync::Mutex; - let _ = tokio::task::block_in_place(move || { - let rt = tokio::runtime::Builder::new_current_thread() - .enable_all() - .build() - .unwrap(); - rt.block_on(async { - // we will ignore if any errors occurred during the bootstrapping the db, - // because the client could be offline when trying to connect. - let _ = db.bootstrap_db().await; - }) - }); + if let Some(sync_ctx) = &db.sync_ctx { + let sync_ctx = sync_ctx.clone(); + // we will ignore if any errors occurred during the bootstrapping the db, + // because the client could be offline when trying to connect. + match tokio::runtime::Handle::try_current() { + Ok(_) => { + std::thread::spawn(move || { + let rt = tokio::runtime::Builder::new_current_thread() + .enable_all() + .build() + .unwrap(); + rt.block_on(async { + let mut locked_ctx = sync_ctx.lock().await; + let _ = crate::sync::bootstrap_db(&mut locked_ctx).await; + }); + }) + .join() + .expect("bootstrap thread panicked"); + } + Err(_) => { + let rt = tokio::runtime::Builder::new_current_thread() + .enable_all() + .build() + .unwrap(); + rt.block_on(async { + let mut locked_ctx = sync_ctx.lock().await; + let _ = crate::sync::bootstrap_db(&mut locked_ctx).await; + }); + } + } + } let local = db.connect()?; From 652f432a4e4b1b00e0a3d2508c6e376ad4db86e7 Mon Sep 17 00:00:00 2001 From: Avinash Sajjanshetty Date: Thu, 17 Apr 2025 17:37:33 +0530 Subject: [PATCH 2/2] Simplify usage of runtime --- libsql/src/database.rs | 41 ++++++++++++----------------------------- 1 file changed, 12 insertions(+), 29 deletions(-) diff --git a/libsql/src/database.rs b/libsql/src/database.rs index 501f244810..a583c6a652 100644 --- a/libsql/src/database.rs +++ b/libsql/src/database.rs @@ -683,35 +683,18 @@ impl Database { use tokio::sync::Mutex; if let Some(sync_ctx) = &db.sync_ctx { - let sync_ctx = sync_ctx.clone(); - // we will ignore if any errors occurred during the bootstrapping the db, - // because the client could be offline when trying to connect. - match tokio::runtime::Handle::try_current() { - Ok(_) => { - std::thread::spawn(move || { - let rt = tokio::runtime::Builder::new_current_thread() - .enable_all() - .build() - .unwrap(); - rt.block_on(async { - let mut locked_ctx = sync_ctx.lock().await; - let _ = crate::sync::bootstrap_db(&mut locked_ctx).await; - }); - }) - .join() - .expect("bootstrap thread panicked"); - } - Err(_) => { - let rt = tokio::runtime::Builder::new_current_thread() - .enable_all() - .build() - .unwrap(); - rt.block_on(async { - let mut locked_ctx = sync_ctx.lock().await; - let _ = crate::sync::bootstrap_db(&mut locked_ctx).await; - }); - } - } + let sync_ctx_clone = sync_ctx.clone(); + std::thread::spawn(move || { + let rt = tokio::runtime::Runtime::new().unwrap(); + // we will ignore if any errors occurred during the bootstrapping the db, + // because the client could be offline when trying to connect. + rt.block_on(async { + let mut locked_ctx = sync_ctx_clone.lock().await; + let _ = crate::sync::bootstrap_db(&mut locked_ctx).await; + }); + }) + .join() + .expect("thread panicked while bootstrapping the db"); } let local = db.connect()?;