Skip to content

`catchSTM` does not roll back `newTVar`

Summary

It appears that catchSTM does not rollback newTVar when it encounters an exception. This results in a situation where a transaction is only partially rolled back, violating atomicity.

Steps to reproduce

Run this code block:

import GHC.Conc
import Control.Exception

data Error = Error (TVar String)
instance Show Error where
    show (Error t) = "Error"
instance Exception Error

act = catchSTM (do
        t <- newTVar "Hello"
        writeTVar t "Hi"
        throwSTM $ Error t)
    (\(Error t) -> readTVar t)

main = atomically act >>= putStrLn

Expected behavior

It outputs Hello. It appears that writeTVar was rolled back but newTVar was not.

Environment

  • GHC version used: 8.6.5
Edited by TheKing01
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information