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

John Lato jwlato at gmail.com
Tue Sep 2 17:01:40 UTC 2014


On Sep 2, 2014 3:59 AM, "Leon Smith" <leon.p.smith at gmail.com> wrote:
>
> On Mon, Sep 1, 2014 at 7:13 PM, John Lato <jwlato at gmail.com> wrote:
>>
>> So what should happen in your situation is that, when a separate thread
closes the fd, threadWaitRead will throw an exception promptly, not
continue to wait on a now-closed fd.
>
>
> Right,  which is what would happen most of the time.    But in the race
condition I'm pointing out,  the thread yields *before* the call to
threadWaitRead,   so there is no interest yet registered in the file
descriptor,  so closeFdWith does not raise an exception.     When the
thread that calls threadWaitRead is resumed,  it ends up waiting on an
entirely different descriptor that happens to share the same index as the
old descriptor.

I was thinking you could do away entirely with the Maybe wrapper. But I
guess that might not be possible in your case.

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.

John
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20140902/465110ec/attachment.html>


More information about the Haskell-Cafe mailing list