bikeshedder / deadpool

Dead simple pool implementation for rust with async-await
Apache License 2.0
1.08k stars 137 forks source link

The `Future`s returned by `managed::Pool` `async fn`s are not `Send` #318

Closed SergioBenitez closed 6 months ago

SergioBenitez commented 7 months ago

As in the title: in 0.11. The culprit is the Manager trait, which doesn't require the Futures returned by its methods as needing to be Send. This infects every function that calls any of these methods and awaits the returned future, making the Future returned by those functions !Send. This makes using the managed part of deadpool with a multithreaded runtime either impossible or extremely cumbersome/slow.

It should suffice to add a Send bound to the return impl in the methods:

/// Manager responsible for creating new [`Object`]s or recycling existing ones.
pub trait Manager: Sync + Send {
    /// Type of [`Object`]s that this [`Manager`] creates and recycles.
    type Type;
    /// Error that this [`Manager`] can return when creating and/or recycling
    /// [`Object`]s.
    type Error;

    /// Creates a new instance of [`Manager::Type`].
-    fn create(&self) -> impl Future<Output = Result<Self::Type, Self::Error>>;
+    fn create(&self) -> impl Future<Output = Result<Self::Type, Self::Error>> + Send;

    /// Tries to recycle an instance of [`Manager::Type`].
    ///
    /// # Errors
    ///
    /// Returns [`Manager::Error`] if the instance couldn't be recycled.
    fn recycle(
        &self,
        obj: &mut Self::Type,
        metrics: &Metrics,
-    ) -> impl Future<Output = RecycleResult<Self::Error>>;
+    ) -> impl Future<Output = RecycleResult<Self::Error>> + Send;

    /// Detaches an instance of [`Manager::Type`] from this [`Manager`].
    ///
    /// This method is called when using the [`Object::take()`] method for
    /// removing an [`Object`] from a [`Pool`]. If the [`Manager`] doesn't hold
    /// any references to the handed out [`Object`]s then the default
    /// implementation can be used which does nothing.
    fn detach(&self, _obj: &mut Self::Type) {}
}
bikeshedder commented 6 months ago

This is a huge whoopsie.

While I'm at it I also added type Type: Send and type Error: Send to it.

bikeshedder commented 6 months ago

I just released deadpool version 0.12.0 at crates.io with this fix:

All the other crates were released as well but with a change in the minor version number as this is is only a breaking change for the core deadpool crate.

Thanks a lot for reporting this! :thumbsup: