Bug in hDuplicateTo in GHC 6.4

Volker Wysk post at volker-wysk.de
Wed Apr 6 11:16:01 EDT 2005


Hello.

The following program demonstrates the bug:

import GHC.Handle
import GHC.IOBase
import GHC.Conc
import IO

main = do
   h <- openFile "/tmp/out" WriteMode
   hDuplicateTo h stdout
   
   fdh <- getfd h
   fdstdout <- getfd stdout
   hPutStrLn stderr ("h: " ++ show fdh ++ "\nstdout: " ++ show fdstdout)

   hClose h
   putStrLn "bla"


getfd h@(FileHandle _ mvar) = do
   h__ <- takeMVar mvar
   let fd = fromIntegral (haFD h__)
   putMVar mvar h__
   return fd


The output:

h: 1
stdout: 1
dup: <stdout>: hPutChar: invalid argument (Bad file descriptor)


After hDuplicateTo, both handles use the same file descriptor. Closing one of 
them invalidates the file descriptor underneath the other.

The documentation isn't completely clear about what hDuplicateTo is supposed 
to do. But from the name and the comment in base/GHC/Handle.hs I conclude 
that it should make two handles which use two different fds which refer to 
the same file, like dup2 does.


Bye,
V.W.



More information about the Glasgow-haskell-bugs mailing list