question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

IDisposable disposed prematurely in monad transformers

See original GitHub issue

Description

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:closed
  • Created 4 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
gustycommented, Jul 30, 2019

@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.

2reactions
gustycommented, Jul 29, 2019

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found