unsafeness of unsafeInterleaveIO

Simon Marlow simonmar at microsoft.com
Mon Jun 13 07:23:40 EDT 2005


On 11 June 2005 02:23, Remi Turk wrote:

> On Sat, Jun 11, 2005 at 01:55:57AM +0200, Thomas Jäger wrote:
>>> Just looking at the documentation for System.IO.unsafeInterleaveIO,
>>> what exactly is unsafe about it?
>> 
>> It can create "pure values" that trigger side effects during their
>> evaluation. This can be abused to do IO outside of an IO monad
>> (actually, hGetContents can already be used for that purpose).
>> 
>> In the worst case, it can even crash the RTS:
>>> import Control.Concurrent.STM
>>> import System.IO.Unsafe
>>> 
>>> main :: IO ()
>>> main = atomically =<< unsafeInterleaveIO (atomically $ return $
>>> return ()) 
>> 
>> Thomas
> 
> Stares at a core-dump.
> I wonder whether this would be worth a bug-report, or perhaps a
> warning in STM's docs about (understandable) undefined behaviour
> in this case. Interestingly, Tomasz Zielonka's FakeSTM [1]
> survives it.

Yes, the STM implementation can't cope with nested transactions - I noticed this once before when using unsafePerformIO.  I'll see if it would be possible to raise an exception when a nested atomically is detected.

Cheers,
	Simon


More information about the Glasgow-haskell-users mailing list