it works correctly , anyone got an idea of why ?
It shouldn't be an issue on the sender side because it works correctly whith try_recv and I tested it with try_send.
Your try_recv example is going to block the runtime, i.e. not await for a relatively long amount of time, which can produce unexpected behavior. The fact it works suggests me that you might also be blocking the runtime somewhere else. Is this all your code?
here is all my code , it contain both cases ( I know it doesn't execute because of the loops , i just keep it here to change whenever i'll find the solution ).
tokio::spawn(async move {
info!("task spawned");
info!("{}", rx.is_closed());
loop { //this one works
match rx.try_recv() {
Ok(update) => {
match update {
MarketUpdate::Bid(tick, price) => {
// Handle bid price update
let mut actions_unlocked = actions.lock().await;
if let Some(action) = actions_unlocked.get_mut(&tick) {
action.update_bid(price);
info!(
"updated ask price for {} with value : {:?}",
action.symbol, action.bid
)
}
}
MarketUpdate::Ask(tick, price) => {
let mut actions_unlocked = actions.lock().await;
if let Some(action) = actions_unlocked.get_mut(&tick) {
action.update_ask(price);
info!(
"updated ask price for {} with value : {:?}",
action.symbol, action.ask
)
}
// Handle ask price update
}
MarketUpdate::BidSize(tick, size) => {
// Handle bid size update
}
MarketUpdate::AskSize(tick, size) => {
// Handle ask size update
}
}
}
Err(err) => (),
}
}
while let Some(update) = rx.recv().await { //this one doesn't
info!("received {:?}", update);
match update {
MarketUpdate::Bid(tick, price) => {
// Handle bid price update
let mut actions_unlocked = actions.lock().await;
if let Some(action) = actions_unlocked.get_mut(&tick) {
action.update_bid(price);
info!(
"updated ask price for {} with value : {:?}",
action.symbol, action.bid
)
}
}
MarketUpdate::Ask(tick, price) => {
let mut actions_unlocked = actions.lock().await;
if let Some(action) = actions_unlocked.get_mut(&tick) {
action.update_ask(price);
info!(
"updated ask price for {} with value : {:?}",
action.symbol, action.ask
)
}
// Handle ask price update
}
MarketUpdate::BidSize(tick, size) => {
// Handle bid size update
}
MarketUpdate::AskSize(tick, size) => {
// Handle ask size update
}
}
}
info!("task ended");
});
Async executors expect all spawned tasks to be cooperative (doing some small amount of work then yielding back to the executor via await). Something outside of this spawn can be blocking instead of awaiting and affect the behavior of the block you shared.