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 memory leak in tail recursion #79

Merged
merged 2 commits into from Sep 11, 2020

Conversation

wantastic84
Copy link
Collaborator

@wantastic84 wantastic84 commented Sep 11, 2020

There was a memory leak in the tail recursion loop rec loop where disposables are not being disposed until the recursion terminates. The fix was done to replace the recursion with while loop.

To illustrate the issue, here is an example for memory leak in tail recursion:

let myDisposer name =
        { new System.IDisposable
          with member this.Dispose() = printfn "disposing %s" name }
    let mutable count = 3
    let rec loop () = async {
        if count > 0 then
            use _ = myDisposer "foo"
            count <- count - 1
            printfn "sleeping for 2 sec for count %d" count
            do! Async.Sleep(2000)
            return! loop()
    }
    loop() |> Async.RunSynchronously

In this case, disposable will not be disposed until the tail recursion terminates. Thus the outcome will be:

sleeping for 2 sec for count 3
sleeping for 2 sec for count 2
sleeping for 2 sec for count 1
disposing foo
disposing foo
disposing foo

The fix was placed to replace the recursion with while loop:

let myDisposer name =
        { new System.IDisposable
          with member this.Dispose() = printfn "disposing %s" name }
    let mutable count = 3
    let aux = async {
        count <- count - 1
        printfn "sleeping for 2 sec for count %d" count
        do! Async.Sleep(2000)
    }
    let loop = async {
        use _ = myDisposer "foo"
        while count > 0 do
            do! aux
    }
    loop |> Async.RunSynchronously

In this case, you will only have one disposable that gets disposed when the while loop terminates:

sleeping for 2 sec for count 3
sleeping for 2 sec for count 2
sleeping for 2 sec for count 1
disposing foo

removing tail recursion and taking out PushProperty to the top level
@bartelink
Copy link
Collaborator

Nice catch and fix, thanks!

@bartelink bartelink merged commit 4488bb3 into master Sep 11, 2020
@bartelink bartelink deleted the fix-memory-leak-in-tail-recursion branch September 11, 2020 14:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants