From 503ddd6ede3e54748360f86ed744dcef0be71a28 Mon Sep 17 00:00:00 2001 From: Ian Denhardt Date: Fri, 26 Oct 2018 18:05:05 -0400 Subject: 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. --- 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 6751de72a8..7038b0db04 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 -- cgit v1.2.1