Is this a concurrency bug in base?

Daniel Fischer daniel.is.fischer at googlemail.com
Sun Oct 9 18:13:59 CEST 2011


On Sunday 09 October 2011, 17:51:06, Jean-Marie Gaillourdet wrote:
> > That sounds plausible. Do you see any workaround? Perhaps repeatedly
> > evaluating typeOf?
> 
> typeOf' seems to be a working workaround: 
> 
> typeOf' val
>     | t1 == t2 = t1
>     | otherwise = typeOf' val
>   where
>     t1 = typeOf'' val
>     t2 = typeOf''' val
> {-# NOINLINE typeOf' #-}
> 
> 
> typeOf'' x = typeOf x
> {-# NOINLINE typeOf'' #-}
> typeOf''' x = typeOf x
> {-# NOINLINE typeOf''' #-}

That'll make it very improbable to get bad results, but not impossible.

Thread1: typeOf' (); typeOf'' (), lookup, not there
Thread2: typeOf' (); typeOf'' (), lookup, not there
Thread1: create and insert; typeOf''' (), entry present, use ~> Key 0
Thread2: create and insert, overwites entry with Key 0,
         new entry has Key 1; typeOf''' (), entry present, use ~> Key 1

It will probably take a long time until it bites, but when it does, it will 
hurt.
A proper fix would need a lock to ensure only one thread at a time can 
access the cache.



More information about the Glasgow-haskell-users mailing list