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 Subscription cancelation when never awaiting #1349

Merged
merged 1 commit into from
May 30, 2022
Merged
Changes from all 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
33 changes: 23 additions & 10 deletions futures/src/subscription/tracker.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use crate::{BoxFuture, MaybeSend, Subscription};

use futures::{channel::mpsc, sink::Sink};
use futures::{
channel::mpsc,
sink::{Sink, SinkExt},
};
use std::{collections::HashMap, marker::PhantomData};

/// A registry of subscription streams.
Expand Down Expand Up @@ -64,7 +67,7 @@ where
+ MaybeSend
+ Clone,
{
use futures::{future::FutureExt, stream::StreamExt};
use futures::stream::StreamExt;

let mut futures: Vec<BoxFuture<()>> = Vec::new();

Expand All @@ -85,19 +88,29 @@ where
continue;
}

let (cancel, cancelled) = futures::channel::oneshot::channel();
let (cancel, mut canceled) = futures::channel::oneshot::channel();

// TODO: Use bus if/when it supports async
let (event_sender, event_receiver) =
futures::channel::mpsc::channel(100);

let stream = recipe.stream(event_receiver.boxed());

let future = futures::future::select(
cancelled,
stream.map(Ok).forward(receiver.clone()),
)
.map(|_| ());
let mut receiver = receiver.clone();
let mut stream = recipe.stream(event_receiver.boxed());

let future = async move {
loop {
let select =
futures::future::select(&mut canceled, stream.next());

match select.await {
futures::future::Either::Left(_)
| futures::future::Either::Right((None, _)) => break,
futures::future::Either::Right((Some(message), _)) => {
let _ = receiver.send(message).await;
}
}
}
};

let _ = self.subscriptions.insert(
id,
Expand Down