Combination of groupBy and mapAsync involving access to the group causes deadlock.
Description
My code deadlocks when working with AsyncSeq pipeline.
Repro steps
Code example:
[1;2;3]
|> AsyncSeq.ofSeq
|> AsyncSeq.groupBy (fun x -> x)
|> AsyncSeq.mapAsync (fun (key, gr) -> async {
let! length = AsyncSeq.length gr
return (key, length)
})
|> AsyncSeq.toBlockingSeq
Expected behavior
No deadlock, or some xml-doc remarks with the warnings of such possibility, if this is by design.
Known workarounds
It looks like I have to eagerly enumerate the sequence, so all groups get resolved
Related information
- Operating system Win 10
- .NET Runtime: 5.0.100
I wasted an hour on the same problem.
Then, in the span of two minutes, I found this issue and read the groupBy documentation which contains the following warning:
Note that the resulting async sequence has to be processed in parallel (e.g AsyncSeq.mapAsyncParallel) becaused completion of sub-sequences depends on completion of other sub-sequences.
Using ... |> AsyncSeq.mapAsyncParallel instead of ... |> AsyncSeq.mapAsync fixes the dead-lock.
I guess this is by design. It would be nice if it could be easier to diagnose, or somehow possible to avoid the issue entirely, though :)
Perhaps putting a warning CompilerMessage attribute on groupBy makes sense. It's too subtle.