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

FuturesOrdered: handle index overflow correctly #2689

Closed
taiki-e opened this issue Jan 15, 2023 · 1 comment · Fixed by #2810
Closed

FuturesOrdered: handle index overflow correctly #2689

taiki-e opened this issue Jan 15, 2023 · 1 comment · Fixed by #2810
Labels
A-stream Area: futures::stream bug

Comments

@taiki-e
Copy link
Member

taiki-e commented Jan 15, 2023

See #2664

@conradludgate explained how to fix this, but it does not seem trivial.

(By the way, what I had in mind when I mentioned this issue in #2664 was to make the index 64-bit to mitigate the risk of overflow.)

@taiki-e taiki-e added bug A-stream Area: futures::stream labels Jan 15, 2023
@taiki-e
Copy link
Member Author

taiki-e commented Jan 15, 2023

push_back does not appear to decrement the index when futures are popped, so the code that uses push_back is probably more likely to be affected by this issue.

let mut set = FuturesOrdered::new();
for _ in 0..3 {
  set.push_back(std::future::ready(1));
  set.next().await;
  dbg!(set.len());
  dbg!(set.next_incoming_index);
  dbg!(set.next_outgoing_index);
}
/* result:
[futures-util/src/stream/futures_ordered.rs:254] set.len() = 0
[futures-util/src/stream/futures_ordered.rs:255] set.next_incoming_index = 1
[futures-util/src/stream/futures_ordered.rs:256] set.next_outgoing_index = 1
[futures-util/src/stream/futures_ordered.rs:254] set.len() = 0
[futures-util/src/stream/futures_ordered.rs:255] set.next_incoming_index = 2
[futures-util/src/stream/futures_ordered.rs:256] set.next_outgoing_index = 2
[futures-util/src/stream/futures_ordered.rs:254] set.len() = 0
[futures-util/src/stream/futures_ordered.rs:255] set.next_incoming_index = 3
[futures-util/src/stream/futures_ordered.rs:256] set.next_outgoing_index = 3
*/
let mut set = FuturesOrdered::new();
for _ in 0..3 {
  set.push_front(std::future::ready(1));
  set.next().await;
  dbg!(set.len());
  dbg!(set.next_incoming_index);
  dbg!(set.next_outgoing_index);
}
/* result:
[futures-util/src/stream/futures_ordered.rs:254] set.len() = 0
[futures-util/src/stream/futures_ordered.rs:255] set.next_incoming_index = 0
[futures-util/src/stream/futures_ordered.rs:256] set.next_outgoing_index = 0
[futures-util/src/stream/futures_ordered.rs:254] set.len() = 0
[futures-util/src/stream/futures_ordered.rs:255] set.next_incoming_index = 0
[futures-util/src/stream/futures_ordered.rs:256] set.next_outgoing_index = 0
[futures-util/src/stream/futures_ordered.rs:254] set.len() = 0
[futures-util/src/stream/futures_ordered.rs:255] set.next_incoming_index = 0
[futures-util/src/stream/futures_ordered.rs:256] set.next_outgoing_index = 0
*/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-stream Area: futures::stream bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant