bug in dupChan

Meurig Sage meurig@dcs.gla.ac.uk
Wed, 24 Jan 2001 18:07:00 -0000


This is a multi-part message in MIME format.

------=_NextPart_000_02DA_01C08630.72DDBAE0
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

Hi=20
I think that there's a bug in dupChan in Chan.lhs

I tried the following program.=20

main =3D do
 chan <- newChan
 ch <- dupChan chan
 writeChan chan "done"
 x <- readChan chan
 y <- readChan ch
 prnt ("Got "++x ++" "++y)

Now if I remember correctly this should print "Got done done".

Instead it exits with=20
C:\Test\test.exe: no threads to run:  infinite loop or deadlock?

I looked for the code for dupChan in hslibs and it says

dupChan :: Chan a -> IO (Chan a)
dupChan (Chan _read write) =3D do
   new_read <- newEmptyMVar
   hole     <- readMVar write
   putMVar new_read hole
   return (Chan new_read write)

Shouldn't this instead be=20

dupChan :: Chan a -> IO (Chan a)
dupChan (Chan _read write) =3D do
   new_read <- newEmptyMVar
   hole     <- readMVar write
   putMVar new_read hole
** putMVar write hole **
   return (Chan new_read write)

That's at least what the Concurrent haskell paper says.

Hugs 98 seems to have the same bug with its library.

Meurig

------=_NextPart_000_02DA_01C08630.72DDBAE0
Content-Type: text/html;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 5.50.4207.2601" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT face=3DArial size=3D2>Hi </FONT></DIV>
<DIV><FONT face=3DArial size=3D2>I think that there's a bug in dupChan =
in=20
Chan.lhs</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>I tried the following program. =
</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT><FONT face=3DArial=20
size=3D2></FONT>&nbsp;</DIV><FONT face=3DArial size=3D2>main =3D =
do<BR>&nbsp;chan &lt;-=20
newChan<BR>&nbsp;ch &lt;- dupChan chan<BR>&nbsp;writeChan chan =
"done"<BR>&nbsp;x=20
&lt;- readChan chan<BR>&nbsp;y &lt;- readChan ch<BR>&nbsp;prnt ("Got =
"++x ++"=20
"++y)</FONT>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>Now if I remember correctly this should =
print "Got=20
done done".</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>Instead it exits with </FONT></DIV>
<DIV><FONT face=3DArial size=3D2>C:\Test\test.exe: no threads to =
run:&nbsp; infinite=20
loop or deadlock?</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>I looked for the code for dupChan in =
hslibs and it=20
says</FONT></DIV><FONT face=3DArial size=3D2>
<DIV><BR>dupChan :: Chan a -&gt; IO (Chan a)<BR>dupChan (Chan _read =
write) =3D=20
do<BR>&nbsp;&nbsp; new_read &lt;- newEmptyMVar<BR>&nbsp;&nbsp;=20
hole&nbsp;&nbsp;&nbsp;&nbsp; &lt;- readMVar write<BR>&nbsp;&nbsp; =
putMVar=20
new_read hole<BR>&nbsp;&nbsp; return (Chan new_read write)</DIV>
<DIV>&nbsp;</DIV>
<DIV>Shouldn't this instead be </DIV>
<DIV></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>dupChan :: Chan a -&gt; IO (Chan =
a)<BR>dupChan=20
(Chan _read write) =3D do<BR>&nbsp;&nbsp; new_read &lt;-=20
newEmptyMVar<BR>&nbsp;&nbsp; hole&nbsp;&nbsp;&nbsp;&nbsp; &lt;- readMVar =

write<BR>&nbsp;&nbsp; putMVar new_read hole</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>** putMVar&nbsp;write hole =
**<BR>&nbsp;&nbsp;=20
return (Chan new_read write)</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>That's at least what the Concurrent =
haskell paper=20
says.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>Hugs 98 seems to have the same bug with =
its=20
library.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>Meurig</FONT></DIV></BODY></HTML>

------=_NextPart_000_02DA_01C08630.72DDBAE0--