FSharp.Control.FusionTasks icon indicating copy to clipboard operation
FSharp.Control.FusionTasks copied to clipboard

Memory leak in ValueTask.AsAsync()

Open mvkara opened this issue 2 years ago • 1 comments
trafficstars

Memory leak is in the ContinueWith() section with the captured lambda parameter (despite the use of the wildcard) or the task being captured longer than it is needed.

When in use with a recursive async where it does a ValueTask/Task conversion, looks at the result and potentially recurses the action in the ContinueWith block appears effectively for the lifetime of the loop at times. This could be the life of the whole program as the Action may never return meaning the task's result is held in memory by this ContinueWith function. If the async is recursive and runs this over and over this could result in all task results remaining in memory. Switching to old fashioned Async.AwaitTask when awaiting the task in the async loop fixes the issue.

I think that the Infrastructures.asAsyncV function retains the object when memory profiling. Could be the wildcard arg, or the task object itself. I found this when profiling, tough leak to spot.

mvkara avatar Dec 11 '22 19:12 mvkara

@mvkara Thanks for the report, I have released FusionTasks as a final version. But now that I see it, I think I might be able to do something more, so I will try to think of more ways to implement this.

kekyo avatar Dec 16 '22 00:12 kekyo