Thread-backed handles: thread blocked indefinitely in an MVar operation

Simon Marlow marlowsd at gmail.com
Tue Jun 22 04:31:02 EDT 2010


On 21/06/2010 03:51, Dimitry Golubovsky wrote:

> I am experimenting with the new feature in GHC such as custom Handles.
> I use GHC 6.12.2, base 4.2.0.1
>
> I have pasted the code in question as
> http://hpaste.org/fastcgi/hpaste.fcgi/view?id=26413#a26413
>
> I am trying to create a Handle using a thread as data producer. In my
> example is is a union of directories, and the Handle contents is
> newline-separated list of full paths of all files in the union. This
> list is produced by a thread which reads contents of all directories
> in the union, turns them into absolute filenames, and sends to the
> channel. The method of BufferedIO responsible for filling in the
> Handle's buffer reads data from the channel and places it in the
> buffer.
>
> The problem is, after the contents of the Handle (via hGetContents)
> has been consumed, the program crashes with the following message:
>
> <program name>: thread blocked indefinitely in an MVar operation
>
> that is, the BlockedIndefinitelyOnMVar exception has occurred.
>
> What is done wrong in my code? The dirClose method seems to be called
> (its output shows up), however with or without the call to killThread,
> the exception occurs anyway.

So the problem is that fillReadBuffer has to return 0 to indicate EOF, 
but you are killing the slave thread as soon as it has reached the end 
of the stream.  So you get one call to fillReadBuffer that returns the 
data up to the end of the stream, and the next call that should return 0 
blocks on the Chan indefinitely because the slave thread has already 
been killed.

Cheers,
	Simon


More information about the Glasgow-haskell-users mailing list