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

Disposable will be disposed before async is run #54

Open
nojaf opened this issue Jan 22, 2024 · 3 comments
Open

Disposable will be disposed before async is run #54

nojaf opened this issue Jan 22, 2024 · 3 comments

Comments

@nojaf
Copy link
Contributor

nojaf commented Jan 22, 2024

Original request by @Smaug123:

let foo () =
    use blah = thing
    async {
        return "hi"
    }

We should warn that use blah will be disposed of before the async is run. This should happen when a function returns an Async/Task.

An additional request is that this check can be disabled by a code comment.
Something like:

let foo () =
    // Note: disposed before returned async is run
    use blah = thing
    async {
        return "hi"
    }
@dawedawe
Copy link
Contributor

Hey @Smaug123 ,

to be sure I get the requirements right.
A use in a local func should not trigger a warning, right? Only top level use expressions in the async/task returning function.
Right?

let foo () =
    let localFunc () =
        use t = new DisposableThing() // warning here?
        ()
    async {
        return "hi"
    }

@Smaug123
Copy link
Contributor

That's correct, because I think most readers would correctly expect the use to be disposed when leaving the scope of the local function. The analyser is specifically to catch the unintended interaction between implicit-disposal (from use) and deferred execution (from Async/Task); I think there's much less likely to be a bug in code of the creation-and-disposal-within-local-function pattern you quoted there.


In principle we could catch a related bug, I guess, where we return anything that closes over the disposed object:

let foo () =
    use blah = new DisposableThing ()
    fun () -> blah

This isn't quite the same as the async bug (though it is very related), but I think it's probably much less common and it's also got many more edge cases to consider, so we should probably just ignore that one.

@dawedawe
Copy link
Contributor

Alright, thanks!

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

No branches or pull requests

3 participants