IDisposable disposed prematurely in monad transformers
See original GitHub issueDescription
Monad transformers don’t work nicely with “use” IDisposable (e.g. SqlCommand). Disposable object gets disposed prematurely.
Repro steps
Minimal code to reproduce below, reproduces with other monad transformers e.g. ResultT
type SomethingDisposiable() =
interface IDisposable with
member __.Dispose() =
printfn "I'm disposed!"
member __.AsyncSomeOption() : Async<int option> =
async {
printfn "I'm doing something async..."
return Some 1
}
let reproducePrematureDisposal : Async<int option> =
OptionT.run <|
monad {
use somethingDisposable = new SomethingDisposiable()
let! (result : int) = OptionT <| somethingDisposable.AsyncSomeOption()
printfn "Unpacked async option: %A" result
return result
}
Expected behavior
expected output:
I’m doing something async… I’m disposed! Unpacked async option: 1
Actual behavior
actual output:
I’m disposed! I’m doing something async… Unpacked async option: 1
i.e. somethingDisposable already disposed by the time when AsyncSomeOption invoked.
Known workarounds
don’t use monad transformers, use async builder, write explicit control flow for nested monadic results (Option<>, Result<>).
Related information
- nuget package version: 1.1.0-CI00252
- .NET Framework 4.7.2
- Operating system: Windows 10
Issue Analytics
- State:
- Created 4 years ago
- Comments:9 (5 by maintainers)
Top Results From Across the Web
Trying to understand the types produced by monad ...
Monad transformers aren't in general "wrapping" the monad they're applied to, at least not in any obvious sense.
Read more >About monad transformers : r/haskell
Monad transformers combine two monads into one, letting you use the powers of both without constantly hopping back and forth between them. For ......
Read more >Control.Frame.Tutorial
Frame s are "restricted monad transformers", and they would normally lift a ... can then immediately dispose of the resource before the input...
Read more >Using objects that implement IDisposable
IDisposable or System.IAsyncDisposable should always be properly disposed of, regardless of variable scoping, unless otherwise explicitly stated ...
Read more >Monad Transformers
Monads are a convenient way to to sequence computation with effects. Different monads can provide different kinds of effects.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@DunetsNM I’ve applied the fix and used your repro as test.
It would be nice to add more tests for cexp-monad-trans so feel free to submit more repros here and we can add them, or if you feel like creating a PR with more tests that’s always welcome.
Thanks for the clarification. Of course, there’s not doubt that it should dispose at the end. I think that’s the issue itself.
I’m adding all the missing methods to the transformers which fixes the issue, at least testing with this kind of samples pass, so I will add some tests similar to your repro.
The idea is to produce a nuget afterwards, since there is plenty of new functionality, so you’ll be able to use this very soon.