[GHC] #7160: C finalizers are reversed during GC

GHC cvs-ghc at haskell.org
Sat Aug 18 17:41:19 CEST 2012


#7160: C finalizers are reversed during GC
-----------------------------------------+----------------------------------
 Reporter:  int-e                        |          Owner:                  
     Type:  bug                          |         Status:  new             
 Priority:  normal                       |      Component:  Runtime System  
  Version:  7.6.1-rc1                    |       Keywords:                  
       Os:  Unknown/Multiple             |   Architecture:  Unknown/Multiple
  Failure:  Incorrect result at runtime  |       Testcase:                  
Blockedby:                               |       Blocking:                  
  Related:                               |  
-----------------------------------------+----------------------------------
 (See also thread starting at
 http://www.haskell.org/pipermail/libraries/2012-August/018302.html )

 The list of finalizers is reversed during GC (cf.
 {{{rts/sm/MarkWeak.c}}}), which may cause them to run in the wrong order.
 The following program reproduces this behaviour.

 {{{
 {-# LANGUAGE ForeignFunctionInterface, MagicHash #-}
 import GHC.ForeignPtr
 import GHC.Ptr
 import System.Mem

 -- one should really use own C function rather than this varargs one to
 avoid
 -- possible ABI issues
 foreign import ccall "&debugBelch" fun :: FunPtr (Ptr () -> Ptr () -> IO
 ())

 new name = do
     p <- newForeignPtr_ (Ptr name)
     addForeignPtrFinalizerEnv fun (Ptr "finalizer 1 (%s)\n"#) p
     addForeignPtrFinalizerEnv fun (Ptr "finalizer 2 (%s)\n"#) p
     return p

 main = do
     p <- new "p"#
     q <- new "q"#
     r <- new "r"#
     performGC -- collect p. finalizer order: 2, then 1.
     print q
     performGC -- collect q. finalizer order: 1, then 2.
               -- expected order: 2, then 1.
     print r
     performGC -- collect r. finalizer order: 2, then 1.
 }}}

-- 
Ticket URL: <http://hackage.haskell.org/trac/ghc/ticket/7160>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler



More information about the Glasgow-haskell-bugs mailing list