Skip to content

Commit 4156904

Browse files
authored
fix: use std::sync::Mutex for SQLite connection to avoid tokio runtime panics (#164)
Replace tokio::sync::Mutex with std::sync::Mutex for the SQLite connection. Since rusqlite operations are inherently synchronous, there's no benefit to using tokio's async Mutex, and blocking_lock() panics when called from within a tokio async runtime context ("Cannot block the current thread from within a runtime"). This allows consumers to call MDK methods directly from async code without needing to wrap calls in std::thread::spawn() to escape the runtime context.
1 parent ee9bcc4 commit 4156904

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

crates/mdk-sqlite-storage/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
- **Security (Audit Issue AB)**: Added size limits to prevent disk and CPU exhaustion from unbounded user input ([#94](https://github.com/marmot-protocol/mdk/pull/94))
7575
- **Security (Audit Issue AG)**: `all_groups` now skips corrupted rows instead of failing on the first deserialization error, improving availability when database contains malformed data ([#115](https://github.com/marmot-protocol/mdk/pull/115))
7676
- Propagate `last_message_id` parse errors in `row_to_group` instead of silently converting to `None` ([#105](https://github.com/marmot-protocol/mdk/pull/105))
77+
- Changed `tokio::sync::Mutex` to `std::sync::Mutex` for SQLite connection to avoid panics when called from within tokio async runtime contexts ([#164](https://github.com/marmot-protocol/mdk/pull/164))
7778

7879
### Removed
7980

crates/mdk-sqlite-storage/src/lib.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ use std::sync::Arc;
7171
use mdk_storage_traits::{Backend, GroupId, MdkStorageError, MdkStorageProvider};
7272
use openmls_traits::storage::{StorageProvider, traits};
7373
use rusqlite::Connection;
74-
use tokio::sync::Mutex;
74+
use std::sync::Mutex;
7575

7676
mod db;
7777
pub mod encryption;
@@ -454,14 +454,14 @@ impl MdkSqliteStorage {
454454
where
455455
F: FnOnce(&Connection) -> T,
456456
{
457-
let conn = self.connection.blocking_lock();
457+
let conn = self.connection.lock().unwrap();
458458
f(&conn)
459459
}
460460

461461
/// Creates a snapshot of a group's state by copying all group-related rows
462462
/// to the snapshot table.
463463
fn snapshot_group_state(&self, group_id: &GroupId, name: &str) -> Result<(), Error> {
464-
let conn = self.connection.blocking_lock();
464+
let conn = self.connection.lock().unwrap();
465465
let group_id_bytes = group_id.as_slice();
466466
let now = std::time::SystemTime::now()
467467
.duration_since(std::time::UNIX_EPOCH)
@@ -822,7 +822,7 @@ impl MdkSqliteStorage {
822822
/// Restores a group's state from a snapshot by deleting current rows
823823
/// and re-inserting from the snapshot table.
824824
fn restore_group_from_snapshot(&self, group_id: &GroupId, name: &str) -> Result<(), Error> {
825-
let conn = self.connection.blocking_lock();
825+
let conn = self.connection.lock().unwrap();
826826
let group_id_bytes = group_id.as_slice();
827827

828828
// Check if snapshot exists BEFORE starting transaction or deleting any data.
@@ -1112,7 +1112,7 @@ impl MdkSqliteStorage {
11121112

11131113
/// Deletes a snapshot that is no longer needed.
11141114
fn delete_group_snapshot(&self, group_id: &GroupId, name: &str) -> Result<(), Error> {
1115-
let conn = self.connection.blocking_lock();
1115+
let conn = self.connection.lock().unwrap();
11161116
conn.execute(
11171117
"DELETE FROM group_state_snapshots WHERE snapshot_name = ? AND group_id = ?",
11181118
rusqlite::params![name, group_id.as_slice()],
@@ -1160,7 +1160,7 @@ impl MdkStorageProvider for MdkSqliteStorage {
11601160
&self,
11611161
group_id: &GroupId,
11621162
) -> Result<Vec<(String, u64)>, MdkStorageError> {
1163-
let conn = self.connection.blocking_lock();
1163+
let conn = self.connection.lock().unwrap();
11641164
let mut stmt = conn
11651165
.prepare_cached(
11661166
"SELECT DISTINCT snapshot_name, created_at FROM group_state_snapshots
@@ -1181,7 +1181,7 @@ impl MdkStorageProvider for MdkSqliteStorage {
11811181
}
11821182

11831183
fn prune_expired_snapshots(&self, min_timestamp: u64) -> Result<usize, MdkStorageError> {
1184-
let conn = self.connection.blocking_lock();
1184+
let conn = self.connection.lock().unwrap();
11851185
let deleted = conn
11861186
.execute(
11871187
"DELETE FROM group_state_snapshots WHERE created_at < ?",

0 commit comments

Comments
 (0)