Skip to content

bracket releases too early when combined with NonDet #489

@tomjaguarpaw

Description

@tomjaguarpaw

When I use bracket (or rather bracket_, in the example below) inside a runNonDet block the resource is released too early. Is this expected?

(I feel this must be a common question, so apologies if it's answered elsewhere, but I didn't find this precise issue in the documentation, nor from searching the issues here.)

{-# LANGUAGE GHC2021 #-}
{-# LANGUAGE DataKinds #-}

import Control.Applicative ((<|>))
import Polysemy (embed, runM)
import Polysemy.NonDet (runNonDet)
import Polysemy.Resource (bracket_, runResource)

example :: IO [Int]
example =
  runM $
    runNonDet $
      runResource $
        bracket_
          (embed (putStrLn "Acquired"))
          (embed (putStrLn "Released"))
          ( do
              r <- pure 1 <|> pure 2
              embed (putStrLn "Used")
              pure r
          )
-- ghci> example
-- Acquired
-- Used
-- Released
-- Used
-- Released
-- [1,2]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions