FSharp.Control.AsyncSeq icon indicating copy to clipboard operation
FSharp.Control.AsyncSeq copied to clipboard

Combination of groupBy and mapAsync involving access to the group causes deadlock.

Open voroninp opened this issue 5 years ago • 2 comments

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

voroninp avatar Nov 21 '20 21:11 voroninp

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 :)

mlaily avatar Apr 20 '21 16:04 mlaily

Perhaps putting a warning CompilerMessage attribute on groupBy makes sense. It's too subtle.

dsyme avatar Apr 21 '21 11:04 dsyme