solidsnack/coproc — pull request #1 by basvandijk (2012-10-22 13:17:38+00:00)
Title: Make backgroundReadFIFOs safe with regard to asynchronous exceptions

Hi Jason,

In the following code:

```haskell
backgroundReadFIFOs :: [ByteString] -> IO [ByteString]
backgroundReadFIFOs theFIFOs = do
cells <- sequence (newEmptyMVar <$ theFIFOs)
sequence_ [ forkIO (drainFIFO p >>= putMVar c) | p <- theFIFOs | c <- cells ]
sequence (takeMVar <$> cells)
```

if an asynchronous exception is thrown to one of the forked threads then the corresponding `putMVar` won't be executed anymore resulting in a dead-lock when the corresponding `takeMVar` is called.

A nice solution is to use Simon Marlow's `async` package and replace all of this code with a simple:

```haskell
backgroundReadFIFOs = mapConcurrently drainFIFO
```

You may even consider removing this definition all together and just call `mapConcurrently drainFIFO` at call sites.