[Haskell-cafe] Race conditions with threadWait(Read/Write) and closeFdWith

Leon Smith leon.p.smith at gmail.com
Tue Sep 2 19:02:31 UTC 2014


On Tue, Sep 2, 2014 at 1:01 PM, John Lato <jwlato at gmail.com> wrote:

> I was thinking you could do away entirely with the Maybe wrapper. But I
> guess that might not be possible in your case.
>
Actually,  the approach currently on github uses -1 (an invalid descriptor)
to represent Nothing,  and does away with Maybe altogether,  but that's
just a implementation detail.

> Have you actually observed this behavior? I suspect that the thread will
> never yield between readMVar and threadWaitRead because there are no
> allocations.  I suppose an async exception could arise, so it might be
> correct to run that line with exceptions masked.
>
Not without inserting a superfluous readMVar read between reading the Fd
and the threadWaitRead so that a demonstration can precisely control the
interleaving of several threads.    You are probably correct,  that there
aren't any yield points between the readMVar and the start of the
threadWaitRead call,   but are there any potential yield points inside of
threadWaitRead itself before the interest in the file descriptor is
properly registered in the IO manager?     And I agree with Felipe, even if
correct, this sort of assumption seems much too strong and fragile to
safely make in a library that I hope not to have to maintain too much.

As for async exceptions,  the binding definitely needs work to make it safe
in their presence,  but that's another issue entirely.   =)

Best,
Leon
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20140902/f2750540/attachment.html>


More information about the Haskell-Cafe mailing list