digital-society-coop / axum-sqlx-tx

Request-scoped sqlx transactions for axum
MIT License
45 stars 11 forks source link

LazyTransactionState<DB> isn't Send #40

Open cbzehner opened 5 months ago

cbzehner commented 5 months ago

I just pulled this into a small side-project to test it out and got a warning that LazyTransactionState<DB> isn't Send. Let me know if I can provide more information to make this bug report easier.

   Compiling axum-sqlx-tx v0.8.0
error: future cannot be sent between threads safely
   --> /Users/cbzehner/.cargo/registry/src/index.crates.io-6f17d22bba15001f/axum-sqlx-tx-0.8.0/src/tx.rs:177:9
    |
177 | /         Box::pin(async move {
178 | |             let ext: &Extension<DB> = parts.extensions.get().ok_or(Error::MissingExtension)?;
179 | |
180 | |             let tx = ext.acquire().await?;
...   |
185 | |             })
186 | |         })
    | |__________^ future created by async block is not `Send`
    |
    = help: within `LazyTransactionState<DB>`, the trait `Sync` is not implemented for `<<DB as Marker>::Driver as sqlx::Database>::Connection`
note: future is not `Send` as this value is used across an await
   --> /Users/cbzehner/.cargo/registry/src/index.crates.io-6f17d22bba15001f/axum-sqlx-tx-0.8.0/src/extension.rs:88:46
    |
86  |         match &self.0 {
    |               ------- has type `&LazyTransactionState<DB>` which is not `Send`
87  |             LazyTransactionState::Unacquired { state } => {
88  |                 let tx = state.transaction().await?;
    |                                              ^^^^^ await occurs here, with `&self.0` maybe used later
...
95  |     }
    |     - `&self.0` is later dropped here
    = note: required for the cast from `Pin<Box<[async block@/Users/cbzehner/.cargo/registry/src/index.crates.io-6f17d22bba15001f/axum-sqlx-tx-0.8.0/src/tx.rs:177:18: 186:10]>>` to `Pin<Box<(dyn futures_core::Future<Output = Result<Tx<DB, E>, E>> + Send + 'ctx)>>`
help: consider further restricting the associated type
    |
175 |         'state: 'ctx, <<DB as Marker>::Driver as sqlx::Database>::Connection: Sync
    |                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

error: could not compile `axum-sqlx-tx` (lib) due to previous error
connec commented 3 months ago

That's interesting, I've not seen this in any of my uses so far – can you share the project (or a minimal reproduction)? That would make it easier to understand the context and test a fix.