<div dir="ltr"><div dir="ltr"><div dir="ltr">Here's the relevant code from GHC's event manager:<div><br>modifyFd :: KQueue -> Fd -> E.Event -> E.Event -> IO Bool<br>modifyFd kq fd oevt nevt = kqueueControl (kqueueFd kq) evs<br> where<br> evs<br> | nevt == mempty = toEvents fd (toFilter oevt) flagDelete noteEOF<br> | otherwise = toEvents fd (toFilter nevt) flagAdd noteEOF<br><br>modifyFdOnce :: KQueue -> Fd -> E.Event -> IO Bool<br>modifyFdOnce kq fd evt =<br> kqueueControl (kqueueFd kq) (toEvents fd (toFilter evt) (flagAdd .|. flagOneshot) noteEOF)</div><div><br></div>kqueueControl :: KQueueFd -> [Event] -> IO Bool<br>kqueueControl kfd evts =<br> withTimeSpec (TimeSpec 0 0) $ \tp -><br> withArrayLen evts $ \evlen evp -> do<br> res <- kevent False kfd evp evlen nullPtr 0 tp<br> if res == -1<br> then do<br> err <- getErrno<br> case err of<br> _ | err == eINTR -> return True<br> _ | err == eINVAL -> return False<br> _ | err == eNOTSUP -> return False<br> _ -> throwErrno "kevent"<div><br></div><div>And the kevent haskell function just wraps C's kevent function. Here's FreeBSD's docs on the two relevant error codes:</div><br> [EINVAL] The specified time limit or filter is invalid.</div><div dir="ltr"><br></div><div>There is no similar documentation for ENOTSUP. I also found this in the mailing list archive: <a href="https://mail.haskell.org/pipermail/ghc-devs/2013-March/000798.html">https://mail.haskell.org/pipermail/ghc-devs/2013-March/000798.html</a>. It confirms my suspicions. Immediately calling the callback when event registration fails leads to undesirable behavior. The caller of threadWaitRead will think that a file description is ready for a read when it really isn't, and then whatever buffer-copying uninterruptible FFI call they perform next will potentially block the runtime. The linked thread suggests a fix (fallback on select when kqueue doesn't work). What's weird is that the EPoll backend just throws an exception when it gets an error like this. Maybe it would be more honest to make the kqueue backend do the same.</div><div dir="ltr"><br></div><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Feb 6, 2019 at 8:56 PM Carter Schonwald <<a href="mailto:carter.schonwald@gmail.com">carter.schonwald@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">under what circumstances would those failure modes happen where the process can still run? (if need be, i can help you dig into the *BSD code bases if we aren't sure when the kqueue code would do that)</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Feb 6, 2019 at 7:10 PM Andrew Martin <<a href="mailto:andrew.thaddeus@gmail.com" target="_blank">andrew.thaddeus@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr"><div dir="ltr">Digging through the different backends, it looks like only the kqueue backend is even capable of returning False when modifyFd/modifyFdOnce is called. This happens when kevent returns eINTR or eINVAL. Why do we call the callback here instead of just throwing an error like we do in so many other cases?</div></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Feb 6, 2019 at 5:44 PM Andrew Martin <<a href="mailto:andrew.thaddeus@gmail.com" target="_blank">andrew.thaddeus@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr">In the event manager's registerFd_, we find:<div><br></div><div><div> registerFd_ :: EventManager -> IOCallback -> Fd -> Event -> Lifetime</div><div> -> IO (FdKey, Bool)</div><div> registerFd_ mgr@(EventManager{..}) cb !fd !evs lt = do</div><div> ... <- ...</div><div><div> (modify,ok) <- withMVar (callbackTableVar mgr fd) $ \tbl -> do</div></div><div> ... <- ...</div><div><div> -- this simulates behavior of old IO manager:</div><div> -- i.e. just call the callback if the registration fails.</div><div> when (not ok) (cb reg evs)</div><div> return (reg,modify)</div></div><div><br></div><div>A comment and a question. Comment: registerFd_ is only ever called in contexts where exceptions are masked, so withMVar is doing some needless mask/restore. Question: why do we immidiately call the callback if event registration fails? This means that if event registration fails during something like `threadWaitRead`, the end result would be that `threadWaitRead` would just return immidiately. That doesn't seem good.</div><div><br></div>-- <br><div dir="ltr" class="gmail-m_-8499773063188378321gmail-m_-3964963980700534815gmail-m_2515906648899415829gmail_signature">-Andrew Thaddeus Martin</div></div></div></div></div></div></div>
</blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr" class="gmail-m_-8499773063188378321gmail-m_-3964963980700534815gmail_signature">-Andrew Thaddeus Martin</div>
_______________________________________________<br>
Libraries mailing list<br>
<a href="mailto:Libraries@haskell.org" target="_blank">Libraries@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries</a><br>
</blockquote></div>
</blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr" class="gmail_signature">-Andrew Thaddeus Martin</div></div></div>