[Git][ghc/ghc][wip/export-finaliser-exceptions] base: Introduce printToHandleFinalizerExceptionHandler

Ben Gamari (@bgamari) gitlab at gitlab.haskell.org
Wed May 10 19:46:56 UTC 2023



Ben Gamari pushed to branch wip/export-finaliser-exceptions at Glasgow Haskell Compiler / GHC


Commits:
b5b3c326 by Ben Gamari at 2023-05-10T15:46:48-04:00
base: Introduce printToHandleFinalizerExceptionHandler

- - - - -


6 changed files:

- + libraries/base/GHC/IO/Handle/Text.hs-boot
- + libraries/base/GHC/IO/Handle/Types.hs-boot
- libraries/base/GHC/TopHandler.hs
- libraries/base/GHC/Weak.hs
- libraries/base/GHC/Weak/Finalize.hs
- libraries/base/System/Mem/Weak.hs


Changes:

=====================================
libraries/base/GHC/IO/Handle/Text.hs-boot
=====================================
@@ -0,0 +1,8 @@
+{-# LANGUAGE NoImplicitPrelude #-}
+
+module GHC.IO.Handle.Text ( hPutStrLn ) where
+
+import GHC.Base (String, IO)
+import {-# SOURCE #-} GHC.IO.Handle.Types (Handle)
+
+hPutStrLn :: Handle -> String -> IO ()


=====================================
libraries/base/GHC/IO/Handle/Types.hs-boot
=====================================
@@ -0,0 +1,5 @@
+{-# LANGUAGE NoImplicitPrelude #-}
+
+module GHC.IO.Handle.Types ( Handle ) where
+
+data Handle


=====================================
libraries/base/GHC/TopHandler.hs
=====================================
@@ -84,7 +84,7 @@ runMainIO main =
       main_thread_id <- myThreadId
       weak_tid <- mkWeakThreadId main_thread_id
 
-    --setFinalizerExceptionHandler printToStderrFinalizerExceptionHandler
+      --setFinalizerExceptionHandler (printToHandleFinalizerExceptionHandler stderr)
       -- For the time being, we don't install any exception handler for
       -- Handle finalization. Instead, the user should set one manually.
 


=====================================
libraries/base/GHC/Weak.hs
=====================================
@@ -31,7 +31,8 @@ module GHC.Weak (
         -- 'setFinalizerExceptionHandler'. Note that any exceptions thrown by
         -- this handler will be ignored.
         setFinalizerExceptionHandler,
-        getFinalizerExceptionHandler
+        getFinalizerExceptionHandler,
+        printToHandleFinalizerExceptionHandler
     ) where
 
 import GHC.Base


=====================================
libraries/base/GHC/Weak/Finalize.hs
=====================================
@@ -11,6 +11,7 @@ module GHC.Weak.Finalize
       -- this handler will be ignored.
       setFinalizerExceptionHandler
     , getFinalizerExceptionHandler
+    , printToHandleFinalizerExceptionHandler
       -- * Internal
     , runFinalizerBatch
     ) where
@@ -20,6 +21,8 @@ import GHC.Exception
 import GHC.IORef
 import {-# SOURCE #-} GHC.Conc.Sync (labelThreadByteArray#, myThreadId)
 import GHC.IO (catchException, unsafePerformIO)
+import {-# SOURCE #-} GHC.IO.Handle.Types (Handle)
+import {-# SOURCE #-} GHC.IO.Handle.Text (hPutStrLn)
 import GHC.Encoding.UTF8 (utf8EncodeByteArray#)
 
 data ByteArray = ByteArray ByteArray#
@@ -79,3 +82,13 @@ getFinalizerExceptionHandler = readIORef finalizerExceptionHandler
 -- @since 4.18.0.0
 setFinalizerExceptionHandler :: (SomeException -> IO ()) -> IO ()
 setFinalizerExceptionHandler = writeIORef finalizerExceptionHandler
+
+-- | An exception handler for 'Handle' finalization that prints the error to
+-- @stderr@, but doesn't rethrow it.
+--
+-- @since 4.18.0.0
+printToHandleFinalizerExceptionHandler :: Handle -> SomeException -> IO ()
+printToHandleFinalizerExceptionHandler hdl se =
+    hPutStrLn hdl msg `catchException` (\(SomeException _) -> return ())
+  where
+    msg = "Exception during weak pointer finalization (ignored): " ++ displayException se ++ "\n"


=====================================
libraries/base/System/Mem/Weak.hs
=====================================
@@ -71,6 +71,7 @@ module System.Mem.Weak (
         -- this handler will be ignored.
         setFinalizerExceptionHandler,
         getFinalizerExceptionHandler,
+        printToHandleFinalizerExceptionHandler,
 
         -- * A precise semantics
 



View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/b5b3c32648b8d00f7da14766fe84ceac662f6480

-- 
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/b5b3c32648b8d00f7da14766fe84ceac662f6480
You're receiving this email because of your account on gitlab.haskell.org.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-commits/attachments/20230510/d9acc34e/attachment-0001.html>


More information about the ghc-commits mailing list