-
Notifications
You must be signed in to change notification settings - Fork 847
Fix 3219 - make StartAsTask wait for cancellation to take effect on task #3256
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -974,33 +974,16 @@ namespace Microsoft.FSharp.Control | |
| let tcs = new TaskCompletionSource<_>(taskCreationOptions) | ||
|
|
||
| // The contract: | ||
| // a) cancellation signal should always propagate to task | ||
| // b) CancellationTokenSource that produced a token must not be disposed until the task.IsComplete | ||
| // We are: | ||
| // 1) registering for cancellation signal here so that not to miss the signal | ||
| // 2) disposing the registration just before setting result/exception on TaskCompletionSource - | ||
| // otherwise we run a chance of disposing registration on already disposed CancellationTokenSource | ||
| // (See (b) above) | ||
| // 3) ensuring if reg is disposed, we do SetResult | ||
| let barrier = VolatileBarrier() | ||
| let reg = token.Register(fun _ -> if barrier.Proceed then tcs.SetCanceled()) | ||
| // a) cancellation signal should always propagate to the computation | ||
| // b) when the task IsCompleted -> nothing is running anymore | ||
| let task = tcs.Task | ||
| let disposeReg() = | ||
| barrier.Stop() | ||
| if not (task.IsCanceled) then reg.Dispose() | ||
|
|
||
| let a = | ||
| async { | ||
| try | ||
| let! result = computation | ||
| do | ||
| disposeReg() | ||
| tcs.TrySetResult(result) |> ignore | ||
| with exn -> | ||
| disposeReg() | ||
| tcs.TrySetException(exn) |> ignore | ||
| } | ||
| Start(token, a) | ||
| queueAsync | ||
| token | ||
| (fun r -> tcs.SetResult r |> fake) | ||
|
||
| (fun edi -> tcs.SetException edi.SourceException |> fake) | ||
|
||
| (fun _ -> tcs.SetCanceled() |> fake) | ||
|
||
| computation | ||
| |> unfake | ||
| task | ||
|
|
||
| [<Sealed>] | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It makes me a bit nervous that I don't see a way to remove this race condition (ie calling
cts.Cancelafter the bind)