@@ -1671,29 +1671,34 @@ namespace Microsoft.FSharp.Control
16711671 Action< obj>( fun _ ->
16721672if latch.Enter() then
16731673// if we got here - then we need to unregister RegisteredWaitHandle + trigger cancellation
1674- // entrance to TP callback is protected by latch - so savedCont will never be called
1675- match ! rwhwith
1676- | None-> ()
1677- | Some rwh-> rwh.Unregister( null ) |> ignore
1674+ // entrance to TP callback is protected by latch - so savedCont will never be called
1675+ lock rwh( fun () ->
1676+ match ! rwhwith
1677+ | None-> ()
1678+ | Some rwh-> rwh.Unregister( null ) |> ignore)
16781679 Async.Start( async { do ( aux.ccont( OperationCanceledException()) |> unfake) }))
16791680
1680- and registration: CancellationTokenRegistration= aux.token.Register( cancelHandler, null )
1681+ and registration: CancellationTokenRegistration= aux.token.Register( cancelHandler, null )
16811682
16821683let savedCont = args.cont
16831684try
1684- rwh:= Some( ThreadPool.RegisterWaitForSingleObject
1685- ( waitObject= waitHandle,
1686- callBack= WaitOrTimerCallback( fun _ timeOut ->
1687- if latch.Enter() then
1688- rwh:= None
1689- registration.Dispose()
1690- aux.trampolineHolder.Protect( fun () -> savedCont( not timeOut)) |> unfake),
1691- state= null ,
1692- millisecondsTimeOutInterval= millisecondsTimeout,
1693- executeOnlyOnce= true ));
1694- FakeUnit
1685+ lock rwh( fun () ->
1686+ rwh:= Some( ThreadPool.RegisterWaitForSingleObject
1687+ ( waitObject= waitHandle,
1688+ callBack= WaitOrTimerCallback( fun _ timeOut ->
1689+ if latch.Enter() then
1690+ lock rwh( fun () -> rwh.Value.Value.Unregister( null ) |> ignore)
1691+ rwh:= None
1692+ registration.Dispose()
1693+ aux.trampolineHolder.Protect( fun () -> savedCont( not timeOut)) |> unfake),
1694+ state= null ,
1695+ millisecondsTimeOutInterval= millisecondsTimeout,
1696+ executeOnlyOnce= true ));
1697+ FakeUnit)
16951698with _ ->
1696- if latch.Enter() then reraise() // reraise exception only if we successfully enter the latch (no other continuations were called)
1699+ if latch.Enter() then
1700+ registration.Dispose()
1701+ reraise() // reraise exception only if we successfully enter the latch (no other continuations were called)
16971702else FakeUnit
16981703)
16991704#endif