Asynchronous exceptions and laziness bugs in Data.Unique.newUnique

Bas van Dijk v.dijk.bas at gmail.com
Wed Mar 17 11:49:03 EDT 2010


Hello,

I just browsed over the source of Data.Unique.newUnique[1]:

newUnique :: IO Unique
newUnique = do
   val <- takeMVar uniqSource
   let next = val+1
   putMVar uniqSource next
   return (Unique next)

I think asynchronous exceptions should be blocked. Currently if an
asynchronous exception is thrown after taking the MVar, putMVar is not
executed anymore, leaving the MVar in the empty state. A subsequent
call to newUnique will then dead-lock when it tries to take the MVar.

Another problem is the laziness of 'next'. When you put 'next' in the
MVar you actually put the thunk 'val+1' not an Integer in WHNF. When
you create say a million uniques the last unique will be a very large
thunk like 0+1+1...+1. Forcing that thunk (when you print the hash of
the unique for example) will probably overflow your stack.

The attached patch adds the necessary block and strictly evaluates the
next unique value.

regards,

Bas

[1] http://haskell.org/ghc/docs/latest/html/libraries/base-4.2.0.0/src/Data-Unique.html#newUnique
-------------- next part --------------
A non-text attachment was scrubbed...
Name: bugfixes-for-newUnique.dpatch
Type: application/octet-stream
Size: 28085 bytes
Desc: not available
Url : http://www.haskell.org/pipermail/libraries/attachments/20100317/6f24d48d/bugfixes-for-newUnique-0001.obj


More information about the Libraries mailing list