[Git][ghc/ghc][master] 2 commits: base: Export {get,set}ExceptionFinalizer from System.Mem.Weak

Marge Bot (@marge-bot) gitlab at gitlab.haskell.org
Tue May 16 11:28:42 UTC 2023



Marge Bot pushed to branch master at Glasgow Haskell Compiler / GHC


Commits:
41ecfc34 by Ben Gamari at 2023-05-16T07:28:15-04:00
base: Export {get,set}ExceptionFinalizer from System.Mem.Weak

As proposed in CLC Proposal #126 [1].

[1]: https://github.com/haskell/core-libraries-committee/issues/126

- - - - -
67330303 by Ben Gamari at 2023-05-16T07:28:16-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
=====================================
@@ -64,6 +64,15 @@ module System.Mem.Weak (
         mkWeakPair,
         -- replaceFinaliser
 
+        -- * Handling exceptions
+        -- | When an exception is thrown by a finalizer called by the
+        -- garbage collector, GHC calls a global handler which can be set with
+        -- 'setFinalizerExceptionHandler'. Note that any exceptions thrown by
+        -- this handler will be ignored.
+        setFinalizerExceptionHandler,
+        getFinalizerExceptionHandler,
+        printToHandleFinalizerExceptionHandler,
+
         -- * A precise semantics
 
         -- $precise


=====================================
libraries/base/changelog.md
=====================================
@@ -14,6 +14,8 @@
   * Add `Type.Reflection.decTypeRep`, `Data.Typeable.decT` and `Data.Typeable.hdecT` equality decisions functions.
       ([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.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/-/compare/bdb93cd28f4a40e9a9f28b0976ca8fa4f250cad2...67330303714ab64751e538f318932a70c36392b6

-- 
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/bdb93cd28f4a40e9a9f28b0976ca8fa4f250cad2...67330303714ab64751e538f318932a70c36392b6
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/20230516/b3c3f991/attachment-0001.html>


More information about the ghc-commits mailing list