{
  "$type": "site.standard.document",
  "bskyPostRef": {
    "cid": "bafyreig7hmr6ggle6yaoy7jilqy6l5x3w7f7ip3otzohrj2swr5mfxu5fu",
    "uri": "at://did:plc:pi6woz4d47bkuws673w2il2r/app.bsky.feed.post/3mkutaxvunts2"
  },
  "path": "/t/conduit-to-yield-first-lines-from-file-without-leaking-space-or-file-handles/14030#post_6",
  "publishedAt": "2026-05-02T14:00:12.000Z",
  "site": "https://discourse.haskell.org",
  "textContent": "Thanks for looking into it! Unfortunately I don’t think the problem can be solved by making `takeRes` `ResourceT`-aware. After all, there may be no resource at all, and even if there is, it doesn’t know which one to free. For example, some other resource can be released too soon:\n\n\n    {- cabal:\n    build-depends: base, text, directory, conduit, resourcet\n    -}\n    {-# LANGUAGE GHC2021 #-}\n    {-# LANGUAGE LambdaCase #-}\n\n    import Conduit\n    import Control.Monad.Trans.Resource\n    import Control.Monad.Trans.Resource.Internal\n    import Data.Conduit.Combinators qualified as CC\n    import Data.IORef\n\n    main :: IO ()\n    main =\n      runConduitRes $\n        bracketP\n          (pure ())\n          (\\() -> putStrLn \"should close at end\")\n          ( \\() ->\n              CC.enumFromTo (1 :: Int) 4\n                .| awaitForever\n                  ( \\x ->\n                      bracketP\n                        (pure ())\n                        (\\_ -> putStrLn \"close\")\n                        (\\_ -> CC.enumFromTo (1 :: Int) x)\n                        .| takeRes 3\n                  )\n                .| CC.mapM_ (liftIO . print)\n                .| sinkNull\n          )\n\n    takeRes :: (MonadResource m) => Int -> ConduitT a a m ()\n    takeRes 0 = liftResourceT $ do\n      is <- getInternalState\n      closeInternalState is\n      liftIO $ writeIORef is $ ReleaseMap maxBound (minBound + 1) mempty\n    takeRes n =\n      await >>= \\case\n        Nothing -> mempty\n        Just x -> yield x *> takeRes (n - 1)\n\n\n\n    ghci> main\n    1\n    close\n    1\n    2\n    close\n    1\n    2\n    3\n    close\n    should close at end\n    1\n    2\n    3\n    close\n",
  "title": "Conduit to yield first lines from file without leaking space or file handles?"
}