Skip to content

Commit 1c273a2

Browse files
safareligaryb
authored andcommitted
Fix Memory leak in make loop (#12)
* use tailRecM in make loopthis shuold avoid Memory leak #10 * no need for `attempt` if `var` is killed with an error, `takeVar var` will throw that same error so the loop will be terminated. note: before #6 attempt was not used * wrap forever with attempt
1 parent 82f7c8f commit 1c273a2

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

src/Control/Monad/Aff/Bus.purs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ import Control.Monad.Aff.AVar (AVAR, AVar, killVar, makeEmptyVar, putVar, takeVa
3636
import Control.Monad.Eff.AVar as EffAvar
3737
import Control.Monad.Eff.Class (class MonadEff, liftEff)
3838
import Control.Monad.Eff.Exception as Exn
39+
import Control.Monad.Rec.Class (forever)
3940
import Data.Foldable (foldl, sequence_, traverse_)
40-
import Data.List (List, (:))
41+
import Data.List (List(..), (:))
4142
import Data.Monoid (mempty)
4243
import Data.Tuple (Tuple(..))
4344

@@ -60,14 +61,11 @@ make ∷ ∀ m eff a. MonadEff (avar ∷ AVAR | eff) m => m (BusRW a)
6061
make = liftEff do
6162
cell ← EffAvar.makeEmptyVar
6263
consumers ← EffAvar.makeVar mempty
63-
let
64-
loop = attempt (takeVar cell) >>= traverse_ \res → do
65-
vars ← takeVar consumers
66-
putVar mempty consumers
67-
sequence_ (foldl (\xs a → putVar res a : xs) mempty vars)
68-
loop
69-
launchAff_ loop
70-
64+
launchAff_ $ attempt $ forever do
65+
res ← takeVar cell
66+
vars ← takeVar consumers
67+
putVar Nil consumers
68+
sequence_ (foldl (\xs a → putVar res a : xs) mempty vars)
7169
pure $ Bus cell consumers
7270

7371
-- | Blocks until a new value is pushed to the Bus, returning the value.

0 commit comments

Comments
 (0)