Conduit to yield first lines from file without leaking space or file handles?
Haskell Community [Unofficial]
May 2, 2026
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:
takeRes :: MonadResource m => Int -> ConduitT a a m ()
takeRes 0 = liftResourceT $ do
is <- getInternalState
closeInternalState is
liftIO $ writeIORef is $ ReleaseMap maxBound (minBound + 1) mempty
takeRes n = await >>= \case
Nothing -> mempty
Just x -> yield x *> takeRes (n - 1)
Full code (click for more details)
With that, it does output what you expect:
1
close
1
2
close
1
2
3
close
1
2
3
close
Discussion in the ATmosphere