Skip to content

Commit 4777af3

Browse files
authored
Merge pull request #87 from input-output-hk/jdral/withErrors-exception-safe
Make `withErrors` exception safe
2 parents 8423cb3 + 9680339 commit 4777af3

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

fs-sim/CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
# Revision history for fs-sim
22

3+
## ?.?.?.? -- ????-??-??
4+
5+
### Breaking
6+
7+
* Fix a bug where `withErrors` would not put back the previous `Errors` when an
8+
exception is thrown during execution of the function. Though we fixed the bug,
9+
it is also a breaking change: the type signature now has an additional
10+
constraint.
11+
312
## 0.3.1.0 -- 2024-12-10
413

514
### Non-breaking

fs-sim/src/System/FS/Sim/Error.hs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -572,15 +572,12 @@ runSimErrorFS mockFS errors action = do
572572

573573
-- | Execute the next action using the given 'Errors'. After the action is
574574
-- finished, the previous 'Errors' are restored.
575-
withErrors :: MonadSTM m => StrictTVar m Errors -> Errors -> m a -> m a
576-
withErrors errorsVar tempErrors action = do
577-
originalErrors <- atomically $ do
578-
originalErrors <- readTVar errorsVar
579-
writeTVar errorsVar tempErrors
580-
return originalErrors
581-
res <- action
582-
atomically $ writeTVar errorsVar originalErrors
583-
return res
575+
withErrors :: (MonadSTM m, MonadThrow m) => StrictTVar m Errors -> Errors -> m a -> m a
576+
withErrors errorsVar tempErrors action =
577+
bracket
578+
(atomically $ swapTVar errorsVar tempErrors)
579+
(\originalErrors -> atomically $ swapTVar errorsVar originalErrors)
580+
$ \_ -> action
584581

585582
{-------------------------------------------------------------------------------
586583
Utilities

0 commit comments

Comments
 (0)