Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix pin violation in much of concurrentstream #187

Merged
merged 3 commits into from
Jun 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ futures-lite = "1.12.0"
pin-project = "1.0.8"
slab = { version = "0.4.8", optional = true }
smallvec = { version = "1.11.0", optional = true }
futures-buffered = "0.2.6"

[dev-dependencies]
async-io = "2.3.2"
Expand Down
8 changes: 4 additions & 4 deletions src/concurrent_stream/for_each.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::{Consumer, ConsumerState};
use crate::future::FutureGroup;
use futures_buffered::FuturesUnordered;
use futures_lite::StreamExt;
use pin_project::pin_project;

Expand All @@ -22,7 +22,7 @@ where
// NOTE: we can remove the `Arc` here if we're willing to make this struct self-referential
count: Arc<AtomicUsize>,
#[pin]
group: FutureGroup<ForEachFut<F, FutT, T, FutB>>,
group: FuturesUnordered<ForEachFut<F, FutT, T, FutB>>,
limit: usize,
f: F,
_phantom: PhantomData<(T, FutB)>,
Expand All @@ -44,7 +44,7 @@ where
f,
_phantom: PhantomData,
count: Arc::new(AtomicUsize::new(0)),
group: FutureGroup::new(),
group: FuturesUnordered::new(),
}
}
}
Expand All @@ -69,7 +69,7 @@ where
// Space was available! - insert the item for posterity
this.count.fetch_add(1, Ordering::Relaxed);
let fut = ForEachFut::new(this.f.clone(), future, this.count.clone());
this.group.as_mut().insert_pinned(fut);
this.group.as_mut().push(fut);

ConsumerState::Continue
}
Expand Down
8 changes: 4 additions & 4 deletions src/concurrent_stream/from_concurrent_stream.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use super::{ConcurrentStream, Consumer, ConsumerState, IntoConcurrentStream};
use crate::future::FutureGroup;
#[cfg(all(feature = "alloc", not(feature = "std")))]
use alloc::vec::Vec;
use core::future::Future;
use core::pin::Pin;
use futures_buffered::FuturesUnordered;
use futures_lite::StreamExt;
use pin_project::pin_project;

Expand Down Expand Up @@ -32,14 +32,14 @@ impl<T> FromConcurrentStream<T> for Vec<T> {
#[pin_project]
pub(crate) struct VecConsumer<'a, Fut: Future> {
#[pin]
group: FutureGroup<Fut>,
group: FuturesUnordered<Fut>,
output: &'a mut Vec<Fut::Output>,
}

impl<'a, Fut: Future> VecConsumer<'a, Fut> {
pub(crate) fn new(output: &'a mut Vec<Fut::Output>) -> Self {
Self {
group: FutureGroup::new(),
group: FuturesUnordered::new(),
output,
}
}
Expand All @@ -54,7 +54,7 @@ where
async fn send(self: Pin<&mut Self>, future: Fut) -> super::ConsumerState {
let mut this = self.project();
// unbounded concurrency, so we just goooo
this.group.as_mut().insert_pinned(future);
this.group.as_mut().push(future);
ConsumerState::Continue
}

Expand Down
8 changes: 4 additions & 4 deletions src/concurrent_stream/try_for_each.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::concurrent_stream::ConsumerState;
use crate::future::FutureGroup;
use crate::private::Try;
use futures_buffered::FuturesUnordered;
use futures_lite::StreamExt;
use pin_project::pin_project;

Expand All @@ -26,7 +26,7 @@ where
count: Arc<AtomicUsize>,
// TODO: remove the `Pin<Box>` from this signature by requiring this struct is pinned
#[pin]
group: FutureGroup<TryForEachFut<F, FutT, T, FutB, B>>,
group: FuturesUnordered<TryForEachFut<F, FutT, T, FutB, B>>,
limit: usize,
residual: Option<B::Residual>,
f: F,
Expand All @@ -50,7 +50,7 @@ where
f,
residual: None,
count: Arc::new(AtomicUsize::new(0)),
group: FutureGroup::new(),
group: FuturesUnordered::new(),
_phantom: PhantomData,
}
}
Expand Down Expand Up @@ -93,7 +93,7 @@ where
// Space was available! - insert the item for posterity
this.count.fetch_add(1, Ordering::Relaxed);
let fut = TryForEachFut::new(this.f.clone(), future, this.count.clone());
this.group.as_mut().insert_pinned(fut);
this.group.as_mut().push(fut);
ConsumerState::Continue
}

Expand Down
31 changes: 0 additions & 31 deletions src/future/future_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,37 +274,6 @@ impl<F: Future> FutureGroup<F> {
Key(index)
}

/// Insert a value into a pinned `FutureGroup`
conradludgate marked this conversation as resolved.
Show resolved Hide resolved
///
/// This method is private because it serves as an implementation detail for
/// `ConcurrentStream`. We should never expose this publicly, as the entire
/// point of this crate is that we abstract the futures poll machinery away
/// from end-users.
pub(crate) fn insert_pinned(self: Pin<&mut Self>, future: F) -> Key
where
F: Future,
{
let mut this = self.project();
// SAFETY: inserting a value into the futures slab does not ever move
// any of the existing values.
let index = unsafe { this.futures.as_mut().get_unchecked_mut() }.insert(future);
this.keys.insert(index);
let key = Key(index);

// If our slab allocated more space we need to
// update our tracking structures along with it.
let max_len = this.futures.as_ref().capacity().max(index);
this.wakers.resize(max_len);
this.states.resize(max_len);

// Set the corresponding state
this.states[index].set_pending();
let mut readiness = this.wakers.readiness();
readiness.set_ready(index);

key
}

/// Create a stream which also yields the key of each item.
///
/// # Example
Expand Down
Loading