jberryman / unagi-chan

A haskell library implementing fast and scalable concurrent queues for x86, with a Chan-like API
BSD 3-Clause "New" or "Revised" License
127 stars 15 forks source link

Document getChanContents so it's clear it blocks & returns an infinite list, not a batch of available elements #29

Closed lehins closed 5 years ago

lehins commented 6 years ago

I doubt that it's a feature, but getting contents of a channel (both bounded and unbounded) will exhaust computer resources quickly if the lazy list that gets produced gets reversed:

module Main where
import Control.Concurrent.Chan.Unagi

main :: IO ()
main = do
  (_inChan, outChan) <- newChan :: IO (InChan Int, OutChan Int)
  is <- getChanContents outChan
  print $ reverse is

Conceptually it kind of make sense, but in practice it's terrible.

jberryman commented 6 years ago

It sounds like you're looking for better documentation, is that right? Were you expecting getChanContents to return as many elements as it can from the queue (i.e. return a batch in a finite list)?

lehins commented 6 years ago

@jberryman, yes, precisely as you said, I was in need of a function that would give me full contents of the channel without blocking, i.e. would return an empty list if channel is empty, similar to flushTBQueue. And I learned that it's definitely not what getChanContents does :)

An improved documentation would certainly be a plus. At least something telling a user that he'll get an infinite list of values and warning of potential problems that can happen when using that function. But in general I would always advise people to avoid using lazy IO. With getChanContents you can do blocking inside of the pure code, which can be a potential source of problems, eg. receiving a BlockedIndefinitelyOnMVar exception inside of pure code, which you would normally expect only be thrown inside of IO. It might be a bad example, since that exception indicates an issue that you normally don't recover from, nevertheless it does explain the problem I am talking about.

jberryman commented 5 years ago

https://github.com/jberryman/unagi-chan/issues/4