fzyzcjy / flutter_rust_bridge

Flutter/Dart <-> Rust binding generator, feature-rich, but seamless and simple.

Home Page:https://fzyzcjy.github.io/flutter_rust_bridge/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`dyn Store` cannot be sent between threads safely

randsoy opened this issue · comments

Describe the bug

FLUTTER_RUST_BRIDGE_HANDLER.wrap_async::<flutter_rust_bridge::for_generated::SseCodec,_,_,_>(flutter_rust_bridge::for_generated::Task...
SEVERE:    |                                   ^^^^^^^^^^ `dyn Store` cannot be sent between threads safely

api.rs

pub async fn new(){
 let mut store =
            FfiStore::new(&path, &address, &token, store_password).await?;
create(
            identity_key_pair,
            &mut store,...
}

other code

pub async fn create(
    identity_key_pair: IdentityKeyPair,
    store: &mut dyn crate::Store,
    signed_pre_key_store: &mut dyn SignedPreKeyStore,
) -> crate::Result<u32> 

...
#[async_trait(?Send)]
pub trait Store {
    async fn get_registration_id(&self) -> crate::Result<Option<u32>>;
    async fn set_registration_id(
        &mut self,
        registration_id: u32,
    ) -> crate::Result<()>;

Steps to reproduce

Hint: A simple way to reproduce is to clone and modify the https://github.com/fzyzcjy/flutter_rust_bridge/tree/master/frb_example/dart_minimal example package according to your needs.

  1. ...
  2. ...
  3. ...

Logs

SEVERE: error[E0277]: `dyn Store` cannot be sent between threads safely
SEVERE:   --> bridge/rust/src/frb_generated.rs:50:33
SEVERE:    |
SEVERE: 50 |       FLUTTER_RUST_BRIDGE_HANDLER.wrap_async::<flutter_rust_bridge::for_generated::SseCodec,_,_,_>(flutter_rust_bridge::for_generated::Task...
SEVERE:    |                                   ^^^^^^^^^^ `dyn Store` cannot be sent between threads safely
SEVERE: ...
SEVERE: 56 |   let api_private_key = <Vec<u8>>::sse_decode(&mut deserializer);deserializer.end(); move |context| async move {
SEVERE:    |  ___________________________________________________________________________________________________-
SEVERE: 57 | |                     transform_result_sse((move || async move {
SEVERE: 58 | |                          crate::api::e2e::E2e::new(api_path, api_url, api_token, api_private_key).await
SEVERE: 59 | |                     })().await)
SEVERE: 60 | |                 } })
SEVERE:    | |_________________- within this `{async block@bridge/rust/src/frb_generated.rs:56:99: 60:18}`
SEVERE:    |
SEVERE:    = help: within `{async block@bridge/rust/src/frb_generated.rs:56:99: 60:18}`, the trait `std::marker::Send` is not implemented for `dyn Store`
SEVERE:    = note: required because it appears within the type `&mut dyn Store`
SEVERE: note: required because it's used within this `async` fn body

Expected behavior

No response

Generated binding code

No response

OS

No response

Version of flutter_rust_bridge_codegen

No response

Flutter info

No response

Version of clang++

No response

Additional context

No response

Hi! Thanks for opening your first issue here! 😄

Hi, it seems that your Store is not Send. It is required for objects to be send/sync since dart threads as well as the handler rust thread can change themselves. Thus, firstly, could you please show what is your Store, as well as more error messages, such that it may be possible to see making it Send/Sync

Store is an interface that implements storage based on the situation (such as using sqlx to store to sqlite, memory storage)

Then one way is to look at the Rust compiler outputs to see what indeed makes it non-Send/Sync, and try to see whether that can be changed.

Thank you for your prompt. My problem has been resolved. The code is #[async_trait] pub trait Store :Send{

Happy to hear that!