编译器表示数据不能在线程之间安全共享,即使数据被包裹在互斥体中

Compiler says that data cannot be shared between threads safely even though the data is wrapped within a Mutex

我正在使用 Rocket,它有一个 State 传递给 HTTP 请求。此结构包含一个 Mutex<DatastoreInstance>,它提供对 SQLite 数据库的访问权限,并使用互斥锁锁定以确保读写安全。

pub struct DatastoreInstance {
    conn: Connection,
}

DatastoreInstance 结构看起来像这样时,只有一个 SQLite 连接一切正常,但我也想在这个结构中添加一个事务对象:

pub struct DatastoreInstance {
    conn: Connection,
    events_transaction: Transaction,
}

这没有编译,因为 Transaction 对象需要引用一个 Connection 对象,该对象应该有它知道的生命周期。 我正在使用的 rusqlite 中的 ConnectionTransaction 对象定义如下:

pub struct Connection {
    db: RefCell<InnerConnection>,
    cache: StatementCache,
    path: Option<PathBuf>,
}

pub struct Transaction<'conn> {
    conn: &'conn Connection,
    drop_behavior: DropBehavior,
}

为了解决生命周期问题,我必须添加这些生命周期参数才能使其正常工作:

pub struct DatastoreInstance<'a> {
    conn: Connection,
    events_transaction: Transaction<'a>,
}

这就是结果,根据我对生命周期和互斥体的理解应该可以工作,但现在我收到一个编译器错误,告诉我:

`std::cell::RefCell<lru_cache::LruCache<std::string::String, rusqlite::raw_statement::RawStatement>>` cannot be shared between threads safely
    |                                                                                                            
    = help: within `rusqlite::Connection`, the trait `std::marker::Sync` is not implemented for `std::cell::RefCell<lru_cache::LruCache<std::string::String, rusqlite::raw_statement::RawStatement>>`
    = note: required because it appears within the type `rusqlite::cache::StatementCache`                        
    = note: required because it appears within the type `rusqlite::Connection`                                   
    = note: required because of the requirements on the impl of `std::marker::Send` for `&rusqlite::Connection`  
    = note: required because it appears within the type `datastore::DatastoreInstance<'_>`                       
    = note: required because of the requirements on the impl of `std::marker::Send` for `std::sync::Mutex<datastore::DatastoreInstance<'_>>`
    = note: required because it appears within the type `endpoints::ServerState<'_>`                             
    = note: required by `rocket::State`

根据我对互斥锁的理解,这段代码应该是有效的,因为整个 DatastoreInstance 结构被包裹在一个 Mutex 中,这应该保证一次只有一个线程引用这个对象。

我错过了什么?

为什么编译器在 Transaction 引用的 Connection 中而不是仅在 Connection 中引用后发现 RefCell 不再安全?

我对互斥锁的工作原理理解有误吗?我的生命周期是否无效并以某种方式破坏 read/write 安全? ConnectionTransaction 在同一个结构中的设计是否是破坏 read/write 安全性的糟糕设计?我是否需要以某种方式重新设计我的数据结构以使其安全?或者我只是错过了一些非常明显的东西?

一个Mutex只是SendSync if the value it contains is itself Send:

impl<T: ?Sized + Send> Send for Mutex<T>    
impl<T: ?Sized + Send> Sync for Mutex<T>

一个&T只是Send when T is Sync:

impl<'a, T> Send for &'a T
where
    T: Sync + ?Sized, 

还有一个RefCellis never Sync

impl<T> !Sync for RefCell<T>
where
    T: ?Sized, 

如错误消息所述,您的交易包含对 RefCell 的引用。有互斥量并不重要,跨线程共享它本质上不是内存安全的。简单复现:

use std::{cell::RefCell, sync::Mutex};

struct Connection(RefCell<i32>);
struct Transaction<'a>(&'a Connection);

fn is_send<T: Send>(_: T) {}

fn main() {
    let c = Connection(RefCell::new(42));
    let t = Transaction(&c);
    let m = Mutex::new(t);

    is_send(m);
}
error[E0277]: `std::cell::RefCell<i32>` cannot be shared between threads safely
  --> src/main.rs:13:5
   |
13 |     is_send(m);
   |     ^^^^^^^ `std::cell::RefCell<i32>` cannot be shared between threads safely
   |
   = help: within `Connection`, the trait `std::marker::Sync` is not implemented for `std::cell::RefCell<i32>`
   = note: required because it appears within the type `Connection`
   = note: required because of the requirements on the impl of `std::marker::Send` for `&Connection`
   = note: required because it appears within the type `Transaction<'_>`
   = note: required because of the requirements on the impl of `std::marker::Send` for `std::sync::Mutex<Transaction<'_>>`
note: required by `is_send`
  --> src/main.rs:6:1
   |
6  | fn is_send<T: Send>(_: T) {}
   | ^^^^^^^^^^^^^^^^^^^^^^^^^

Why doesn't the compiler find RefCell to be safe anymore after being within a Connection referenced within a Transaction instead of solely within a Connection?

RefCell 很好,它是对 RefCell 引用,但不是。

Is the design of having the Connection and Transaction within the same struct a bad design [...] Do I need to redesign my data structures

是的。