dotnet / dotnet/fsharp

Leakage in async computation awaiting for a ValueTask

Open
#17,814 1 comment 0 reactions 0 assignees View on GitHub
Area-Library Bug Triage-Investigate
Dominant language
F#
Stars
4.3k
Forks
876
Avg merge
4d 11h
Merged PRs (30d)
131

Description

this code

```
let rec streamLoop (enumerator: IAsyncEnumerator<'reply>) = async {
let! hasNext = enumerator.MoveNextAsync()

if hasNext then
try onNewItem enumerator.Current with | _ -> ()
return! streamLoop enumerator }
```

creates a memory leak. Memory in Visual Studio Diagnostic Tools reveal a constantly increasing number of `ContinuationTaskFromResultTask`.

Fix simply adding .AsTask() |> Async.AwaitTask

```
let rec streamLoop (enumerator: IAsyncEnumerator<'reply>) = async {
let! hasNext = enumerator.MoveNextAsync().AsTask() |> Async.AwaitTask

if hasNext then
try onNewItem enumerator.Current with | _ -> ()
return! streamLoop enumerator }
```
Suggests incorrrect interaction of F# Async with ValueTask<> or IAsyncEnumerator<>.MoveNextAsync()

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.