提交 7ea28524 编写于 作者: M Marcio Rinaldi 提交者: Kevin Ransom (msft)

Dispose when async workflow is cancelled (#1555)

* Add test for #1436

* Dispose when async workflow is cancelled
上级 bcf53a97
......@@ -299,6 +299,28 @@ type AsyncModule() =
Assert.IsTrue(isSet())
for _i = 1 to 3 do test()
[<Test>]
member this.``OnCancel.RaceBetweenCancellationAndDispose``() =
let flag = ref 0
let cts = new System.Threading.CancellationTokenSource()
let go = async {
use disp =
cts.Cancel()
{ new IDisposable with
override __.Dispose() = incr flag }
while true do
do! Async.Sleep 50
}
try
Async.RunSynchronously (go, cancellationToken = cts.Token)
with
#if FX_NO_OPERATION_CANCELLED
_ -> ()
#else
:? System.OperationCanceledException -> ()
#endif
Assert.AreEqual(1, !flag)
[<Test>]
member this.``OnCancel.CancelThatWasSignalledBeforeRunningTheComputation``() =
let test() =
......
......@@ -909,7 +909,11 @@ namespace Microsoft.FSharp.Control
/// Implement use/Dispose
let usingA (r:'T :> IDisposable) (f:'T -> Async<'a>) : Async<'a> =
tryFinallyA (fun () -> Microsoft.FSharp.Core.LanguagePrimitives.IntrinsicFunctions.Dispose r) (callA f r)
let mutable x = 0
let disposeFunction _ =
if Interlocked.CompareExchange(&x, 1, 0) = 0 then
Microsoft.FSharp.Core.LanguagePrimitives.IntrinsicFunctions.Dispose r
tryFinallyA disposeFunction (callA f r) |> whenCancelledA disposeFunction
let ignoreA p =
bindA p (fun _ -> doneA)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册