Skip to content

sea-orm-sync + rusqlite , cannot put DatabaseConnection in OnceLock because it is not Sync #3070

@johnny-smitherson

Description

@johnny-smitherson

Description

Can't put rusqlite database connection objects in statics / globals because of compiler error:

static DB_LOCK: std::sync::OnceLock<DatabaseConnection> = OnceLock::new();

This cause

error[E0277]: `(dyn for<'a, 'b> Fn(&'a sea_orm::metric::Info<'b>) + 'static)` cannot be shared between threads safely
   --> packages/storage_crackhouse/src/basic/mod.rs:115:17
    |
115 | static DB_LOCK: std::sync::OnceLock<DatabaseConnection> = OnceLock::new();
    |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `(dyn for<'a, 'b> Fn(&'a sea_orm::metric::Info<'b>) + 'static)` cannot be shared between threads safely
    |
    = help: the trait `Sync` is not implemented for `(dyn for<'a, 'b> Fn(&'a sea_orm::metric::Info<'b>) + 'static)`
    = note: required for `Arc<(dyn for<'a, 'b> Fn(&'a sea_orm::metric::Info<'b>) + 'static)>` to implement `Sync`
note: required because it appears within the type `Option<Arc<(dyn for<'a, 'b> Fn(&'a sea_orm::metric::Info<'b>) + 'static)>>`
   --> /home/p/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/option.rs:600:10
    |
600 | pub enum Option<T> {
    |          ^^^^^^
note: required because it appears within the type `sea_orm::driver::rusqlite::RusqliteSharedConnection`
   --> /home/p/V/sea-orm/sea-orm-sync/src/driver/rusqlite.rs:42:12
    |
 42 | pub struct RusqliteSharedConnection {
    |            ^^^^^^^^^^^^^^^^^^^^^^^^
note: required because it appears within the type `DatabaseConnectionType`
   --> /home/p/V/sea-orm/sea-orm-sync/src/database/db_connection.rs:34:10
    |
 34 | pub enum DatabaseConnectionType {
    |          ^^^^^^^^^^^^^^^^^^^^^^
note: required because it appears within the type `DatabaseConnection`
   --> /home/p/V/sea-orm/sea-orm-sync/src/database/db_connection.rs:24:12
    |
 24 | pub struct DatabaseConnection {
    |            ^^^^^^^^^^^^^^^^^^
    = note: required for `OnceLock<DatabaseConnection>` to implement `Sync`
    = note: shared static variables must have a type that implements `Sync`


Steps to Reproduce

  1. static DB_LOCK: std::sync::OnceLock<DatabaseConnection> = OnceLock::new();
  2. compiler error

Expected Behavior

compile

Actual Behavior

no compile

Reproduces How Often

always

Workarounds

diff --git a/sea-orm-sync/src/metric.rs b/sea-orm-sync/src/metric.rs
index 86bf48d6..6c37fe86 100644
--- a/sea-orm-sync/src/metric.rs
+++ b/sea-orm-sync/src/metric.rs
@@ -1,6 +1,6 @@
 use std::{sync::Arc, time::Duration};
 
-pub(crate) type Callback = Arc<dyn Fn(&Info<'_>)>;
+pub(crate) type Callback = Arc<dyn Fn(&Info<'_>)+Send+Sync>;
 
 #[allow(unused_imports)]
 pub(crate) use inner::metric;

johnny-smitherson@e7492d9

Versions

same as #3068

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No fields configured for Bug.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions