Skip to content

Commit 1c692ce

Browse files
committed
Drain campaign event queue to fix root-cursor memory leak
Every consumer reads campaign events from its own dupChan, so the original Chan's read end is never advanced and pins every event ever written, keeping the whole campaign's event history (including full VM snapshots) alive. Drain the original queue from a dedicated thread so the GC can reclaim delivered events, and publish VM-free, strictly forced copies of falsified/optimized tests so queued events can't retain a VM through a lazy thunk.
1 parent 9680bb1 commit 1c692ce

2 files changed

Lines changed: 15 additions & 3 deletions

File tree

lib/Echidna.hs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
module Echidna where
22

3-
import Control.Concurrent (newChan)
3+
import Control.Concurrent (forkIO, newChan, readChan)
4+
import Control.Exception (SomeException, handle)
5+
import Control.Monad (forever, void)
46
import Control.Monad.Catch (MonadThrow(..))
57
import Control.Monad.IO.Class (liftIO)
68
import Data.IORef (newIORef)
@@ -125,6 +127,11 @@ mkEnv cfg buildOutput tests world slitherInfo = do
125127
codehashMap <- newIORef mempty
126128
chainId <- Onchain.fetchChainIdFrom cfg.rpcUrl
127129
eventQueue <- newChan
130+
-- Consumers read events from their own 'dupChan', so this original read end
131+
-- is never advanced and would otherwise pin every event ever written. Drain
132+
-- it from a dedicated thread so the GC can reclaim delivered events.
133+
void $ forkIO $ handle (\(_ :: SomeException) -> pure ()) $
134+
forever $ void $ readChan eventQueue
128135
coverageRefInit <- newIORef mempty
129136
coverageRefRuntime <- newIORef mempty
130137
corpusRef <- newIORef mempty

lib/Echidna/Campaign.hs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,10 @@ updateOpenTest vm reproducer test = do
651651
, result
652652
, workerId
653653
}
654-
pushWorkerEvent (TestFalsified test')
654+
-- Publish a VM-free copy, forced strict so the event can't retain the
655+
-- VM through a thunk. testRefs keeps the full 'test'' for shrinking.
656+
let !vmFreeTest = test' { Test.vm = Nothing }
657+
pushWorkerEvent (TestFalsified vmFreeTest)
655658
pure $ Just test'
656659

657660
IntValue value' | value' > value -> do
@@ -660,7 +663,9 @@ updateOpenTest vm reproducer test = do
660663
, vm = Just vm
661664
, result
662665
}
663-
pushWorkerEvent (TestOptimized test')
666+
-- VM-free copy, as in the TestFalsified case above.
667+
let !vmFreeTest = test' { Test.vm = Nothing }
668+
pushWorkerEvent (TestOptimized vmFreeTest)
664669
pure $ Just test'
665670
where
666671
value =

0 commit comments

Comments
 (0)