Skip to content

AsyncSeq.cache fails with SynchronizationLockException #33

@odeac

Description

@odeac

The exception SynchronizationLockException is thrown when using AsyncSeq.cache

Build info:

  • .NET45.
  • FSharp.ControlAsyncSeq v2.0.1
  • FSharp.Core 4.3.0.0

Please note that the issue reproduces with v2.0.0 but it doesn't reproduce with v1.15.0

Exception details:

System.Threading.SynchronizationLockException was unhandled by user code
  HResult=-2146233064
  Message=Object synchronization method was called from an unsynchronized block of code.
  Source=FSharp.Control.AsyncSeq
  StackTrace:
       at [email protected](Unit unitVar) in C:\GitHub\dsyme\FSharp.Control.AsyncSeq\src\FSharp.Control.AsyncSeq\AsyncSeq.fs:line 817
       at FSharp.Control.AsyncSeq.tryFinally@326-1.System-IDisposable-Dispose() in C:\GitHub\dsyme\FSharp.Control.AsyncSeq\src\FSharp.Control.AsyncSeq\AsyncSeq.fs:line 351
       at [email protected](FSharpOption`1 _arg1) in C:\GitHub\dsyme\FSharp.Control.AsyncSeq\src\FSharp.Control.AsyncSeq\AsyncSeq.fs:line 340
       at [email protected](a a)
  InnerException: 

This question was also raised on stack overflow

The sample code is listed below.

open FSharp.Control

[<EntryPoint>]
let main argv = 
    let asyncOp x =
        async {
            do! Async.Sleep x
            return x
        }

    let chooser x = if x > 1000 then Some x else None

    let execute (input : AsyncSeq<int>) =
        async {
            printfn "First result... Expect 3s sleep"
            let! r1 = AsyncSeq.tryPick chooser input
            printfn "Computing second result. Expect no sleep"
            let! r2 = AsyncSeq.tryPick chooser input
            printfn "Done"
            return (r1,r2)
        }

    printfn "Starting..."

    let v =
        [1000;2000;3000]
        |> AsyncSeq.ofSeq
        |> AsyncSeq.mapAsync asyncOp
        |> AsyncSeq.cache
        |> execute
        |> Async.RunSynchronously


    printfn "results: = %A" v
    do System.Console.ReadKey() |> ignore
    0

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions