diff --git a/cli/app.rs b/cli/app.rs index 98d80af5a..4bb389384 100644 --- a/cli/app.rs +++ b/cli/app.rs @@ -137,8 +137,8 @@ impl Limbo { let conn = db.connect()?; (io, conn) }; - let mut ext_api = conn.build_turso_ext(); unsafe { + let mut ext_api = conn._build_turso_ext(); if !limbo_completion::register_extension_static(&mut ext_api).is_ok() { return Err(anyhow!( "Failed to register completion extension".to_string() diff --git a/core/ext/mod.rs b/core/ext/mod.rs index da723e8dd..517ca4090 100644 --- a/core/ext/mod.rs +++ b/core/ext/mod.rs @@ -8,11 +8,10 @@ use crate::{function::ExternalFunc, Connection, Database, LimboError, IO}; use crate::{vtab::VirtualTable, SymbolTable}; #[cfg(feature = "fs")] pub use dynamic::{add_builtin_vfs_extensions, add_vfs_module, list_vfs_modules, VfsMod}; -use parking_lot::Mutex; use std::{ ffi::{c_char, c_void, CStr, CString}, rc::Rc, - sync::Arc, + sync::{Arc, Mutex}, }; use turso_ext::{ ExtensionApi, InitAggFunction, ResultCode, ScalarFunction, VTabKind, VTabModuleImpl, @@ -61,7 +60,9 @@ unsafe extern "C" fn handle_schema_insert_database( table: *mut c_void, ) -> ResultCode { let mutex = &*(schema_data as *mut Mutex>); - let mut guard = mutex.lock(); + let Ok(mut guard) = mutex.lock() else { + return ResultCode::Error; + }; let schema = Arc::make_mut(&mut *guard); let c_str = CStr::from_ptr(table_name); diff --git a/core/lib.rs b/core/lib.rs index d47c56047..a4c74dc47 100644 --- a/core/lib.rs +++ b/core/lib.rs @@ -59,7 +59,7 @@ pub use io::{ Buffer, Completion, CompletionType, File, MemoryIO, OpenFlags, PlatformIO, SyscallIO, WriteCompletion, IO, }; -use parking_lot::{Mutex, RwLock}; +use parking_lot::RwLock; use schema::Schema; use std::sync::atomic::{AtomicUsize, Ordering}; use std::{ @@ -71,7 +71,7 @@ use std::{ num::NonZero, ops::Deref, rc::Rc, - sync::Arc, + sync::{Arc, Mutex}, }; #[cfg(feature = "fs")] use storage::database::DatabaseFile; @@ -149,7 +149,7 @@ impl fmt::Debug for Database { }; debug_struct.field("mv_store", &mv_store_status); - let init_lock_status = if self.init_lock.try_lock().is_some() { + let init_lock_status = if self.init_lock.lock().is_ok() { "unlocked" } else { "locked" @@ -297,7 +297,12 @@ impl Database { let conn = Arc::new(Connection { _db: self.clone(), pager: RefCell::new(Rc::new(pager)), - schema: RefCell::new(self.schema.lock().clone()), + schema: RefCell::new( + self.schema + .lock() + .map_err(|_| LimboError::SchemaLocked)? + .clone(), + ), auto_commit: Cell::new(true), mv_transactions: RefCell::new(Vec::new()), transaction_state: Cell::new(TransactionState::None), @@ -440,7 +445,7 @@ impl Database { #[inline] pub fn with_schema_mut(&self, f: impl FnOnce(&mut Schema) -> Result) -> Result { - let mut schema_ref = self.schema.try_lock().ok_or(LimboError::SchemaLocked)?; + let mut schema_ref = self.schema.lock().map_err(|_| LimboError::SchemaLocked)?; let schema = Arc::make_mut(&mut *schema_ref); f(schema) } @@ -786,7 +791,11 @@ impl Connection { pub fn maybe_update_schema(&self) -> Result<()> { let current_schema_version = self.schema.borrow().schema_version; - let schema = self._db.schema.try_lock().ok_or(LimboError::SchemaLocked)?; + let schema = self + ._db + .schema + .lock() + .map_err(|_| LimboError::SchemaLocked)?; if matches!(self.transaction_state.get(), TransactionState::None) && current_schema_version < schema.schema_version { diff --git a/core/storage/pager.rs b/core/storage/pager.rs index 2b5aa682c..aca164cb3 100644 --- a/core/storage/pager.rs +++ b/core/storage/pager.rs @@ -9,12 +9,12 @@ use crate::types::IOResult; use crate::util::IOExt as _; use crate::{return_if_io, Completion}; use crate::{turso_assert, Buffer, Connection, LimboError, Result}; -use parking_lot::{Mutex, RwLock}; +use parking_lot::RwLock; use std::cell::{Cell, OnceCell, RefCell, UnsafeCell}; use std::collections::HashSet; use std::rc::Rc; use std::sync::atomic::{AtomicUsize, Ordering}; -use std::sync::Arc; +use std::sync::{Arc, Mutex}; use tracing::{instrument, trace, Level}; use super::btree::{btree_init_page, BTreePage}; @@ -680,7 +680,7 @@ impl Pager { #[instrument(skip_all, level = Level::DEBUG)] pub fn maybe_allocate_page1(&self) -> Result> { if self.db_state.load(Ordering::SeqCst) < DB_STATE_INITIALIZED { - if let Some(_lock) = self.init_lock.try_lock() { + if let Ok(_lock) = self.init_lock.lock() { match ( self.db_state.load(Ordering::SeqCst), self.allocating_page1(), @@ -738,8 +738,8 @@ impl Pager { *connection ._db .schema - .try_lock() - .ok_or(LimboError::SchemaLocked)? = schema; + .lock() + .map_err(|_| LimboError::SchemaLocked)? = schema; } Ok(commit_status) } @@ -1360,8 +1360,8 @@ impl Pager { connection ._db .schema - .try_lock() - .ok_or(LimboError::SchemaLocked)? + .lock() + .map_err(|_| LimboError::SchemaLocked)? .clone(), ); } diff --git a/core/vdbe/execute.rs b/core/vdbe/execute.rs index cf670a84d..af396384b 100644 --- a/core/vdbe/execute.rs +++ b/core/vdbe/execute.rs @@ -31,7 +31,11 @@ use crate::{ }; use std::ops::DerefMut; use std::sync::atomic::AtomicUsize; -use std::{borrow::BorrowMut, rc::Rc, sync::Arc}; +use std::{ + borrow::BorrowMut, + rc::Rc, + sync::{Arc, Mutex}, +}; use crate::{pseudo::PseudoCursor, result::LimboResult}; @@ -61,7 +65,7 @@ use super::{ CommitState, }; use fallible_iterator::FallibleIterator; -use parking_lot::{Mutex, RwLock}; +use parking_lot::RwLock; use rand::{thread_rng, Rng}; use turso_sqlite3_parser::ast; use turso_sqlite3_parser::ast::fmt::ToTokens;