Skip to content
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

Fix LinkedSubSource leak in Async.Choice #7892

Merged
merged 2 commits into from Nov 28, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
57 changes: 38 additions & 19 deletions src/fsharp/FSharp.Core/async.fs
Expand Up @@ -1293,35 +1293,54 @@ namespace Microsoft.FSharp.Control
| Choice1Of2 computations ->
ProtectedCode ctxt (fun ctxt ->
let ctxtWithSync = DelimitSyncContext ctxt
let noneCount = ref 0
let exnCount = ref 0
let mutable count = computations.Length
let mutable noneCount = 0
let mutable someOrExnCount = 0
let innerCts = new LinkedSubSource(ctxtWithSync.token)

let scont (result: 'T option) =
match result with
| Some _ ->
if Interlocked.Increment exnCount = 1 then
innerCts.Cancel(); ctxtWithSync.trampolineHolder.ExecuteWithTrampoline (fun () -> ctxtWithSync.cont result)
let result =
match result with
| Some _ ->
if Interlocked.Increment &someOrExnCount = 1 then
innerCts.Cancel(); ctxtWithSync.trampolineHolder.ExecuteWithTrampoline (fun () -> ctxtWithSync.cont result)
else
fake()

| None ->
if Interlocked.Increment &noneCount = computations.Length then
innerCts.Cancel(); ctxtWithSync.trampolineHolder.ExecuteWithTrampoline (fun () -> ctxtWithSync.cont None)
else
fake()

if Interlocked.Decrement &count = 0 then
innerCts.Dispose()

result

let econt (exn: ExceptionDispatchInfo) =
let result =
if Interlocked.Increment &someOrExnCount = 1 then
innerCts.Cancel(); ctxtWithSync.trampolineHolder.ExecuteWithTrampoline (fun () -> ctxtWithSync.econt exn)
else
fake()

| None ->
if Interlocked.Increment noneCount = computations.Length then
innerCts.Cancel(); ctxtWithSync.trampolineHolder.ExecuteWithTrampoline (fun () -> ctxtWithSync.cont None)
if Interlocked.Decrement &count = 0 then
innerCts.Dispose()

result

let ccont (exn: OperationCanceledException) =
let result =
if Interlocked.Increment &someOrExnCount = 1 then
innerCts.Cancel(); ctxtWithSync.trampolineHolder.ExecuteWithTrampoline (fun () -> ctxtWithSync.ccont exn)
else
fake()

let econt (exn: ExceptionDispatchInfo) =
if Interlocked.Increment exnCount = 1 then
innerCts.Cancel(); ctxtWithSync.trampolineHolder.ExecuteWithTrampoline (fun () -> ctxtWithSync.econt exn)
else
fake()
if Interlocked.Decrement &count = 0 then
innerCts.Dispose()

let ccont (exn: OperationCanceledException) =
if Interlocked.Increment exnCount = 1 then
innerCts.Cancel(); ctxtWithSync.trampolineHolder.ExecuteWithTrampoline (fun () -> ctxtWithSync.ccont exn)
else
fake()
result

for c in computations do
QueueAsync innerCts.Token scont econt ccont c |> unfake
Expand Down