Skip to content

Commit 5f1f9e4

Browse files
committed
Fix ValueTask.ignore based on Stephen Toub's insights
1 parent b291d2a commit 5f1f9e4

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

src/FSharp.Control.TaskSeq/Utils.fs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ module ValueTask =
3333

3434
/// Ignore a ValueTask<'T>, returns a non-generic ValueTask.
3535
let inline ignore (vtask: ValueTask<'T>) =
36-
if vtask.IsCompleted then
36+
// this implementation follows Stephen Toub's advice, see:
37+
// https://github.com/dotnet/runtime/issues/31503#issuecomment-554415966
38+
if vtask.IsCompletedSuccessfully then
39+
// ensure any side effect executes
40+
vtask.Result |> ignore
3741
ValueTask()
3842
else
3943
ValueTask(vtask.AsTask())
@@ -63,6 +67,7 @@ module Task =
6367
/// Convert a Task<'T> into a non-generic Task, ignoring the result
6468
let inline ignore (task: Task<'T>) =
6569
TaskBuilder.task {
70+
// ensure the task is awaited
6671
let! _ = task
6772
return ()
6873
}

0 commit comments

Comments
 (0)