Skip to content

Commit

Permalink
Merge pull request #513 from IntersectMBO/jdral/release-reftracker-ex…
Browse files Browse the repository at this point in the history
…ception

Release a `RefTracker` even if the corresponding finaliser threw an error
  • Loading branch information
jorisdral authored Jan 7, 2025
2 parents 5821748 + 2880e5e commit 0c74e7a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src-control/Control/RefCount.hs
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,8 @@ releaseRef ::
-> m ()
releaseRef ref@Ref{refobj} = do
assertNoDoubleRelease ref
decrementRefCounter (getRefCounter refobj)
releaseRefTracker ref
decrementRefCounter (getRefCounter refobj)

{-# COMPLETE DeRef #-}
#if MIN_VERSION_GLASGOW_HASKELL(9,0,0,0)
Expand Down
9 changes: 9 additions & 0 deletions test-control/Test/Control/RefCount.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ tests = testGroup "Control.RefCount" [
, testProperty "prop_ref_never_released0" prop_ref_never_released0
, testProperty "prop_ref_never_released1" prop_ref_never_released1
, testProperty "prop_ref_never_released2" prop_ref_never_released2
, testProperty "prop_release_ref_exception" prop_release_ref_exception
#endif
]

Expand Down Expand Up @@ -182,5 +183,13 @@ expectRefNeverReleased e@RefNeverReleased{} =
return (tabulate "displayException" [displayException e] (property True))
expectRefNeverReleased e =
return (counterexample (displayException e) $ property False)

-- | If a finaliser throws an exception, then the 'RefTracker' is still released
prop_release_ref_exception :: Property
prop_release_ref_exception = once $ ioProperty $ do
finalised <- newIORef False
ref <- newRef (writeIORef finalised True >> error "oops") TestObject
_ <- try @SomeException (releaseRef ref)
checkForgottenRefs
#endif

0 comments on commit 0c74e7a

Please sign in to comment.