#2363: getChar cannot be interrupted with -threaded
----------------------------+-----------------------------------------------
Reporter: simonmar | Owner:
Type: bug | Status: new
Priority: high | Milestone: 6.10 branch
Component: libraries/base | Version: 6.8.2
Severity: normal | Resolution:
Keywords: | Difficulty: Unknown
Testcase: | Architecture: Unknown
Os: Unknown |
----------------------------+-----------------------------------------------
Comment (by judah):
Replying to [comment:2 simonmar]:
> That's ok, as long as another thread doesn't call getChar right after
your threadWaitRead, then your getChar will hang again (not likely, I
imagine, but I've trained myself to spot race conditions :-).
I think you can prevent that race by calling `hGetBufNonBlocking`, and
retrying if it doesn't read a character:
{{{
import qualified Data.ByteString.Char8 as B
myGetChar mv = do
threadWaitRead stdInput
bs <- B.hGetNonBlocking stdin 1
if B.null bs
then myGetChar mv
else putMVar mv (Just (B.head bs))
}}}
There's a potential (again, unlikely) starvation problem in that this
thread could keep getting preempted by another thread reading stdin; but
for my particular application, I'm much more concerned with
interruptibility than fairness.
> > The problem with the original code was that `getChar` (and
`hWaitForInput`) wrap foreign calls, which block the subthread they're run
in and thus cannot be halted by `killThread`. In contrast,
`threadWaitRead` wraps a primop, so it does not have that problem.
>
> Actually in the threaded RTS `threadWaitRead` is not a primop, it's a
communication with the IO manager thread.
My mistake; I didn't read the sources carefully enough.
>
> > This workaround is OK for now, but in my opinion all of the IO
functions should be interruptible by default. (And `threadWaitRead` uses
`Fd` instead of `Handle` which is more convenient/portable.)
>
> I'm surprised you think that FD is more portable. Windows doesn't have
FDs except in a compatibility layer.
Sorry, I worded that poorly. I meant of course that `Handle` is more
convenient/portable than `Fd`.
--
Ticket URL: <http://hackage.haskell.org/trac/ghc/ticket/2363#comment:3>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler_______________________________________________
Glasgow-haskell-bugs mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-bugs