[commit: ghc] master: Docs: clarify the interaction between throwSTM and catchSTM. (503ddd6)

git at git.haskell.org git at git.haskell.org
Mon Oct 29 19:12:29 UTC 2018


Repository : ssh://git@git.haskell.org/ghc

On branch  : master
Link       : http://ghc.haskell.org/trac/ghc/changeset/503ddd6ede3e54748360f86ed744dcef0be71a28/ghc

>---------------------------------------------------------------

commit 503ddd6ede3e54748360f86ed744dcef0be71a28
Author: Ian Denhardt <ian at zenhack.net>
Date:   Fri Oct 26 18:05:05 2018 -0400

    Docs: clarify the interaction between throwSTM and catchSTM.
    
    The previous doc comments were not terribly clear on what was or wasn't
    rolled back when an exception was caught in STM. This misunderstanding
    was the source of a bug in another project of mine, and folks on
    `#haskell` found it confusing as well.


>---------------------------------------------------------------

503ddd6ede3e54748360f86ed744dcef0be71a28
 libraries/base/GHC/Conc/Sync.hs | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/libraries/base/GHC/Conc/Sync.hs b/libraries/base/GHC/Conc/Sync.hs
index 6751de7..7038b0d 100644
--- a/libraries/base/GHC/Conc/Sync.hs
+++ b/libraries/base/GHC/Conc/Sync.hs
@@ -752,7 +752,12 @@ orElse (STM m) e = STM $ \s -> catchRetry# m (unSTM e) s
 -- | A variant of 'throw' that can only be used within the 'STM' monad.
 --
 -- Throwing an exception in @STM@ aborts the transaction and propagates the
--- exception.
+-- exception. If the exception is caught via 'catchSTM', only the changes
+-- enclosed by the catch are rolled back; changes made outside of 'catchSTM'
+-- persist.
+--
+-- If the exception is not caught inside of the 'STM', it is re-thrown by
+-- 'atomically', and the entire 'STM' is rolled back.
 --
 -- Although 'throwSTM' has a type that is an instance of the type of 'throw', the
 -- two functions are subtly different:
@@ -770,7 +775,12 @@ orElse (STM m) e = STM $ \s -> catchRetry# m (unSTM e) s
 throwSTM :: Exception e => e -> STM a
 throwSTM e = STM $ raiseIO# (toException e)
 
--- |Exception handling within STM actions.
+-- | Exception handling within STM actions.
+--
+-- @'catchSTM' m f@ catches any exception thrown by @m@ using 'throwSTM',
+-- using the function @f@ to handle the exception. If an exception is
+-- thrown, any changes made by @m@ are rolled back, but changes prior to
+-- @m@ persist.
 catchSTM :: Exception e => STM a -> (e -> STM a) -> STM a
 catchSTM (STM m) handler = STM $ catchSTM# m handler'
     where



More information about the ghc-commits mailing list