{
  "$type": "site.standard.document",
  "bskyPostRef": {
    "cid": "bafyreihor4ddvyz7vwpip3ptumomyqpvdmvwk7ii4rmm6olfujjmmwn27i",
    "uri": "at://did:plc:pi6woz4d47bkuws673w2il2r/app.bsky.feed.post/3mkutbs3aoes2"
  },
  "path": "/t/conduit-to-yield-first-lines-from-file-without-leaking-space-or-file-handles/14030#post_4",
  "publishedAt": "2026-05-02T13:37:01.000Z",
  "site": "https://discourse.haskell.org",
  "tags": [
    "(click for more details)"
  ],
  "textContent": "I’ve looked into this for longer than I should have, but I think the problem is that you need to make `take` aware of the `ResourceT` for this to work. Here’s a hack I came up with:\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 = await >>= \\case\n      Nothing -> mempty\n      Just x -> yield x *> takeRes (n - 1)\n\n\nFull code (click for more details)\n\nWith that, it does output what you expect:\n\n\n    1\n    close\n    1\n    2\n    close\n    1\n    2\n    3\n    close\n    1\n    2\n    3\n    close\n",
  "title": "Conduit to yield first lines from file without leaking space or file handles?"
}