#`tokio::select` running wrong arm

12 messages · Page 1 of 1 (latest)

compact shard
#

Hey, i've got the following function which is responsible for calling self.on_start, self.on_refresh, and self.on_stop, which are all async. There's a tokio::sync::mpsc::Receiver for when to refresh and when to stop, but for whatever reason, when i'm emitting to the receiver for stopping, it's sometimes matching on the refresh arm instead (seemingly 50/50 randomly). is a biased select what I'm looking for? is a tokio::select the wrong tool to use in the first place?

async fn start(
  &mut self,
  mut refresh_rx: Receiver<()>,
  mut stop_rx: Receiver<()>,
) {
  tokio::select! {
    output = self.on_start() => output,
    _ = refresh_rx.recv() => {
      println!("refresh called");
      _ = self.on_refresh();
    },
    _ = stop_rx.recv() => {
      println!("stop called");
      _ = self.on_stop();
    },
  }
merry perch
#

try Some(_) = refresh_rx.recv() => to only consider actual sending events

compact shard
#

btw would you happen to know whether i'd run into problems with the refresh arm here if the expectation is that it can be called multiple times?

#

if i understand it correctly, it'd exit the select once any of the arms complete

merry perch
#

yes, usually select is placed in a loop

compact shard
#

roger that. you're a godsend 🙌

compact shard
#

just realized i can break within the stop arm, my bad

merry perch
#

either that or you use the "precondition" functionality of select to disable certain branches

#

i.e. the if ... part in v1 = (&mut rx1), if a.is_none() => a = Some(v1.unwrap()),