Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iterAsyncParallel and iterAsyncParallelThrottled may fail to cancel #122

Open
thednaz opened this issue Nov 9, 2020 · 0 comments
Open

Comments

@thednaz
Copy link
Contributor

thednaz commented Nov 9, 2020

Description

iterAsyncParallel and iterAsyncParallelThrottled may fail to cancel; this can be observed when exceptions are thrown from another Async in Async.Parallel.

Repro steps

Execute the following code snippet

let r = Random()
 
let handle x = async {
    do! Async.Sleep (r.Next(200))
    printfn "%A" x
}

let fakeAsync = async {    
    do! Async.Sleep 500
    return "hello"
}

let makeAsyncSeqBatch () =
    let rec loop() = asyncSeq {            
        let! batch =  fakeAsync |> Async.Catch
        match batch with
        | Choice1Of2 batch ->
          if (Seq.isEmpty batch) then
            do! Async.Sleep 500
            yield! loop()
          else
            yield batch
            yield! loop() 
        | Choice2Of2 err ->
             printfn "Problem getting batch: %A" err
    }
    
    loop()

let x = makeAsyncSeqBatch () |> AsyncSeq.concatSeq |> AsyncSeq.iterAsyncParallel handle
let exAsync = async {
    do! Async.Sleep 2000
    failwith "error"
}

[x; exAsync] |> Async.Parallel |> Async.Ignore |> Async.RunSynchronously

When exAsync throws, Async.Parallel will attempt to cancel the iteration of the AsyncSeq before it returns. iterAsyncParallel and iterAsyncParalleThrottled will sometimes not return and continue running forever. Changing to iterAsync always stops the iteration of the AsyncSeq reliably.

Known workarounds

Don't use iterAsyncParallel or iterAsyncParalleThrottled, use iterAsync instead.

Related information

  • Operating system
    Windows
  • Branch
    2.0.21
  • .NET Runtime, CoreCLR or Mono Version
    .NET Core 3.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant