Skip to content

Commit d1eb920

Browse files
committed
Fix ValueTask.tryFinally
1 parent 7285499 commit d1eb920

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

src/FSharpPlus/Extensions/ValueTask.fs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -315,18 +315,26 @@ module ValueTask =
315315
let mutable ran = false
316316
let compensation () =
317317
if not ran then
318-
compensation ()
319318
ran <- true
319+
compensation ()
320320
try
321321
let task = body ()
322-
if task.IsCompleted then compensation (); task
322+
if task.IsCompleted then
323+
try
324+
compensation ()
325+
task
326+
with e -> raise e
323327
else
324328
let tcs = TaskCompletionSource<'T> ()
325329
let f = function
326-
| Succeeded r -> tcs.SetResult r
327-
| Faulted exn -> tcs.SetException exn.InnerExceptions
328-
| Canceled -> tcs.SetCanceled ()
329-
task.ConfigureAwait(false).GetAwaiter().UnsafeOnCompleted (fun () -> compensation (); f task)
330+
| Succeeded r -> tcs.SetResult r
331+
| Faulted aex -> tcs.SetException aex.InnerExceptions
332+
| Canceled -> tcs.SetCanceled ()
333+
task.ConfigureAwait(false).GetAwaiter().UnsafeOnCompleted (fun () ->
334+
try
335+
compensation ()
336+
f task
337+
with e -> tcs.SetException e)
330338
ValueTask<'T> tcs.Task
331339
with _ ->
332340
compensation ()

0 commit comments

Comments
 (0)