Tokio tasks: one dies -> all dies

I have a group of tokio tasks, all which returns Result<T, E>.

I would like to express the following constraint: if any one of the tasks returns Err, kill all the other tasks in the group too.

Is there a standard / idiomatic / builtin way to express this idea ? (This is in part motivated by Erlang linked processes.)

1 Like

I think collect from tokio_stream::StreamExt does that ? If you can express your algorithm as a Stream of course

Tokio's unstable JoinSet would make it easy to do that. You can also get the same effect using FuturesUnordered by using its iter method to iterate through and call abort on every join handle.

2 Likes

A bit off topic, do you have an idea when JoinSet will be added to the stable API ?

I almost reached for it yesterday evening and couldn't be bothered to setup my build to use the unstable features (which means you did your job right by making it "difficult" to use unstable features :smiley:)

Uhh, when we decide whether join_one should return Result<Option<T>, JoinError>> or Option<Result<T, JoinError>> I guess?

4 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.