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

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



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


Commits:
859f5f81 by Ben Gamari at 2023-05-10T15:48:45-04:00
base: Introduce printToHandleFinalizerExceptionHandler

- - - - -


7 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
- libraries/base/changelog.md


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
+-- the given 'Handle', 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
 


=====================================
libraries/base/changelog.md
=====================================
@@ -14,7 +14,7 @@
       ([CLC proposal #98](https://github.com/haskell/core-libraries-committee/issues/98))
   * Add `Data.Functor.unzip` ([CLC proposal #88](https://github.com/haskell/core-libraries-committee/issues/88))
   * Add `System.Mem.Weak.{get,set}FinalizerExceptionHandler`, which allows the user to set the global handler invoked by when a `Weak` pointer finalizer throws an exception. ([CLC proposal #126](https://github.com/haskell/core-libraries-committee/issues/126))
-  * Add `System.Mem.Weak.printToStderrFinalizerExceptionHandler`, which can be used with `setFinalizerExceptionHandler` to print exceptions thrown by finalizers to `stderr`.  ([CLC proposal #126](https://github.com/haskell/core-libraries-committee/issues/126))
+  * Add `System.Mem.Weak.printToHandleFinalizerExceptionHandler`, which can be used with `setFinalizerExceptionHandler` to print exceptions thrown by finalizers to the given `Handle`. ([CLC proposal #126](https://github.com/haskell/core-libraries-committee/issues/126))
   * Implement more members of `instance Foldable (Compose f g)` explicitly.
       ([CLC proposal #57](https://github.com/haskell/core-libraries-committee/issues/57))
   * Add `Eq` and `Ord` instances for `SSymbol`, `SChar`, and `SNat`.



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

-- 
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/859f5f8164d81fa185e3903f0570840a1f943a88
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/6c757d7c/attachment-0001.html>


More information about the ghc-commits mailing list