Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make withErrors exception safe #87

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions fs-sim/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Revision history for fs-sim

## ?.?.?.? -- ????-??-??

### Breaking

* Fix a bug where `withErrors` would not put back the previous `Errors` when an
exception is thrown during execution of the function. Though we fixed the bug,
it is also a breaking change: the type signature now has an additional
constraint.

## 0.3.1.0 -- 2024-12-10

### Non-breaking
Expand Down
15 changes: 6 additions & 9 deletions fs-sim/src/System/FS/Sim/Error.hs
Original file line number Diff line number Diff line change
Expand Up @@ -572,15 +572,12 @@ runSimErrorFS mockFS errors action = do

-- | Execute the next action using the given 'Errors'. After the action is
-- finished, the previous 'Errors' are restored.
withErrors :: MonadSTM m => StrictTVar m Errors -> Errors -> m a -> m a
withErrors errorsVar tempErrors action = do
originalErrors <- atomically $ do
originalErrors <- readTVar errorsVar
writeTVar errorsVar tempErrors
return originalErrors
res <- action
atomically $ writeTVar errorsVar originalErrors
return res
withErrors :: (MonadSTM m, MonadThrow m) => StrictTVar m Errors -> Errors -> m a -> m a
withErrors errorsVar tempErrors action =
bracket
(atomically $ swapTVar errorsVar tempErrors)
(\originalErrors -> atomically $ swapTVar errorsVar originalErrors)
$ \_ -> action

{-------------------------------------------------------------------------------
Utilities
Expand Down
Loading