[GHC] #13167: GC and weak reference finalizers and exceptions

GHC ghc-devs at haskell.org
Sun Jan 22 14:50:08 UTC 2017


#13167: GC and weak reference finalizers and exceptions
-------------------------------------+-------------------------------------
           Reporter:  Yuras          |             Owner:
               Type:  bug            |            Status:  new
           Priority:  normal         |         Milestone:
          Component:  Compiler       |           Version:  8.0.1
           Keywords:                 |  Operating System:  Unknown/Multiple
       Architecture:                 |   Type of failure:  None/Unknown
  Unknown/Multiple                   |
          Test Case:                 |        Blocked By:
           Blocking:                 |   Related Tickets:
Differential Rev(s):                 |         Wiki Page:
-------------------------------------+-------------------------------------
 When GC runs a number of finalizers in a row, and the first of them throws
 an exception, then other finalizers are ignored. The relevant piece of
 code is
 [https://github.com/ghc/ghc/blob/fb4092642f057f258d07cd6979925f4e2579eda6/libraries/base/GHC/Weak.hs#L144
 here].

 The following program reproduces the issue:

 {{{
 import Data.IORef
 import Control.Monad
 import Control.Exception
 import System.Mem

 main :: IO ()
 main = do
   run
   run
   run
   run
   performMajorGC
   performMajorGC

 run :: IO ()
 run = do
   ref <- newIORef ()
   void $ mkWeakIORef ref $ do
     putStr "."
     throwIO $ ErrorCall "failed"
 }}}

 I expect it to output "....", but I get only "."

 The issue makes it unsafe to rely on finalizer for resource cleanup
 because unrelated finalizer (e.g. from some other library) may prevent
 your finalizer from running.

 Actually I was sure the issue is known, but today I tried to find a
 reference to it, and failed.

 If it is by design, then it should be documented somewhere.

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


More information about the ghc-tickets mailing list