{
  "$type": "site.standard.document",
  "bskyPostRef": {
    "cid": "bafyreihqsyvgg4i73m5i2gj262jas7r6zxnknslwfb3aabbx3vyiuzs5he",
    "uri": "at://did:plc:pi6woz4d47bkuws673w2il2r/app.bsky.feed.post/3mkumjqgmq542"
  },
  "path": "/t/conduit-to-yield-first-lines-from-file-without-leaking-space-or-file-handles/14030#post_1",
  "publishedAt": "2026-05-02T11:00:30.000Z",
  "site": "https://discourse.haskell.org",
  "textContent": "Would a conduit expert be able to write me a conduit that takes in file names and yields the first `n` lines from each file without leaking space or file handles? I have the following, but it leaks file handles and crashes on my machine if there are more than about 1000 files.\n\n\n    cabal-script-test27.hs: /tmp/manyfiles/1981: openBinaryFile: resource exhausted (Too many open files)\n\n\n\n    import Conduit\n    import qualified Data.Conduit.Combinators as CC\n    import System.Directory (doesFileExist)\n    import Data.Text (Text)\n\n    main :: IO ()\n    main = runConduitRes $\n        CC.sourceDirectoryDeep False \"/tmp/manyfiles\"\n        .| firstNLines 1\n        .| CC.mapM_ (liftIO . print)\n\n    firstNLines\n      :: Int\n      -> ConduitT FilePath Text (ResourceT IO) ()\n    firstNLines n = awaitForever $ \\fp ->\n        CC.sourceFile fp\n        .| CC.decodeUtf8Lenient\n        .| CC.linesUnbounded\n        .| CC.take n\n",
  "title": "Conduit to yield first lines from file without leaking space or file handles?"
}