Error in code sample?
Error in code sample?
Posted Sep 27, 2025 16:34 UTC (Sat) by excors (subscriber, #95769)In reply to: Error in code sample? by Nikratio
Parent article: Canceling asynchronous Rust
Posted Sep 28, 2025 9:53 UTC (Sun)
by Nikratio (subscriber, #71966)
[Link] (2 responses)
I got "timeout" mixed up with "cancellation", since the previous paragraph (and the article overall) is about cancel-correctness. The code handles timeouts correctly, but still drops the message on cancellation.
I think a version that handles both wouldn't be much harder, so I'm surprised that the sample didn't look something like this:
loop {
Posted Sep 28, 2025 11:32 UTC (Sun)
by excors (subscriber, #95769)
[Link]
In contrast, `timeout` constructs a new Future that wraps the original Future, and (on timeout) the outer Future completes but the inner Future is dropped (cancelled). And any resources owned by the inner Future (e.g. `msg`, in the original `timeout(tx.send(msg))` example) are dropped too, which is the issue being solved by the later example.
In practice the code should have some better error handling for Ok(Err) that isn't simply `return;`, but maybe e.g. it's going to reset the whole channel and the old message is redundant so it's okay to drop it; that will depend on context and is outside the scope of this example.
I think in general you shouldn't move the next_message() call in between reserve() and send(), because next_message() might be expensive and perhaps async itself; you might hold the reservation for a very long time, wasting the channel's capacity. You'd need a different way to handle `msg` in the error case, if not simply dropping it.
Posted Sep 30, 2025 0:31 UTC (Tue)
by riking (subscriber, #95706)
[Link]
Error in code sample?
match timeout(Duration::from_secs(5), tx.reserve()).await {
Ok(Ok(permit)) => {
msg = next_message();
permit.send();
}
Ok(Err(_)) => return,
Err(_) => println!("No space for 5 seconds"),
}
}
Error in code sample?
Error in code sample?