Opened 13 years ago

Closed 13 years ago

Last modified 48 years ago

#241 closed bug (Fixed)

hReady always returns True when used on win32 non-Console

Reported by: wglozer Owned by: simonmar
Priority: normal Milestone:
Component: libraries/haskell98 Version: 6.2.1
Keywords: Cc:
Operating System: Architecture:
Type of failure: None/Unknown Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:


The hReady function always returns True when called on
a stdin stream that is not a real win32 console stream,
even if there is no data available.  This causes
blocking of programs that depend on hReady to return
true only when there is at least one character available.

The following program will demonstrate this behavior
when run from inside an msys rxvt window, an emacs
shell buffer, etc:


import IO
import Monad

main = do ready <- hReady stdin
          when ready $ do
            putStr("stdin is ready\n")
            do c <- getChar
               putStr("getChar read '" ++ [c] ++ "'\n")


I've tested this on Windows XP SP1, using the latest
ghc 6.2.1.              

Attachments (2)

console.2.hs (354 bytes) - added by wglozer 13 years ago.
inputReady.2.c (1.9 KB) - added by wglozer 13 years ago.

Download all attachments as: .zip

Change History (4)

Changed 13 years ago by wglozer

Attachment: console.2.hs added

comment:1 Changed 13 years ago by wglozer

Logged In: YES 

After further investigation it looks like
libraries/base/cbits/inputReady.c uses
WaitForMultipleObjects(...) on the stdin handle, and this
method always returns that the handle is available, even
when there is no input.

I've fixed this by calling PeekNamedPipe(...) if the return
code is WAIT_OBJECT_0, and if PeekNamedPipe(...) succeeds
return 1 if any bytes are available, otherwise 0. 
PeekNamedPipe will fail if the handle isn't a pipe, in which
case I'm just returning 1, the same as if the code wasn't
there at all.

Changed 13 years ago by wglozer

Attachment: inputReady.2.c added

comment:2 Changed 13 years ago by simonmar

Status: assignedclosed
Logged In: YES 

Fixed, thanks.
Note: See TracTickets for help on using tickets.