Skip to content

Commit cb5ff21

Browse files
committed
Make sure handleIncomingMessages uses strict state
This fixes another memory leak
1 parent db75158 commit cb5ff21

File tree

1 file changed

+18
-18
lines changed
  • distributed-process/src/Control/Distributed/Process

1 file changed

+18
-18
lines changed

distributed-process/src/Control/Distributed/Process/Node.hs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,8 @@ import qualified Data.Map as Map
2626
, partitionWithKey
2727
, filterWithKey
2828
)
29-
import qualified Data.List as List (delete, (\\))
3029
import Data.Set (Set)
31-
import qualified Data.Set as Set (empty, insert, delete, member, (\\), fromList)
30+
import qualified Data.Set as Set (empty, insert, delete, member, filter)
3231
import Data.Foldable (forM_)
3332
import Data.Maybe (isJust)
3433
import Data.Typeable (Typeable)
@@ -248,25 +247,25 @@ removeConnectionsFrom ident =
248247
Map.filterWithKey $ \(fr, _to) _conn -> fr /= ident
249248

250249
handleIncomingMessages :: LocalNode -> IO ()
251-
handleIncomingMessages node = go [] Map.empty Map.empty Set.empty
250+
handleIncomingMessages node = go Set.empty Map.empty Map.empty Set.empty
252251
where
253-
go :: [NT.ConnectionId] -- ^ Connections whose purpose we don't yet know
252+
go :: Set NT.ConnectionId -- ^ Connections whose purpose we don't yet know
254253
-> Map NT.ConnectionId LocalProcess -- ^ Connections to local processes
255254
-> Map NT.ConnectionId TypedChannel -- ^ Connections to typed channels
256255
-> Set NT.ConnectionId -- ^ Connections to our controller
257256
-> IO ()
258-
go uninitConns procs chans ctrls = do
257+
go !uninitConns !procs !chans !ctrls = do
259258
event <- NT.receive endpoint
260259
case event of
261260
NT.ConnectionOpened cid _rel _theirAddr ->
262261
-- TODO: Check if _rel is ReliableOrdered, and if not, treat as
263262
-- (**) below.
264-
go (cid : uninitConns) procs chans ctrls
263+
go (Set.insert cid uninitConns) procs chans ctrls
265264
NT.Received cid payload ->
266265
case ( Map.lookup cid procs
267266
, Map.lookup cid chans
268267
, cid `Set.member` ctrls
269-
, cid `elem` uninitConns
268+
, cid `Set.member` uninitConns
270269
) of
271270
(Just proc, _, _, _) -> do
272271
let msg = payloadToMessage payload
@@ -286,7 +285,7 @@ handleIncomingMessages node = go [] Map.empty Map.empty Set.empty
286285
mProc <- withMVar state $ return . (^. localProcessWithId lpid)
287286
case mProc of
288287
Just proc ->
289-
go (List.delete cid uninitConns)
288+
go (Set.delete cid uninitConns)
290289
(Map.insert cid proc procs)
291290
chans
292291
ctrls
@@ -298,7 +297,7 @@ handleIncomingMessages node = go [] Map.empty Map.empty Set.empty
298297
-- remote node as having died, and we should close
299298
-- incoming connections (this requires a Transport layer
300299
-- extension). (**)
301-
go (List.delete cid uninitConns) procs chans ctrls
300+
go (Set.delete cid uninitConns) procs chans ctrls
302301
SendPortIdentifier chId -> do
303302
let lcid = sendPortLocalId chId
304303
lpid = processLocalId (sendPortProcessId chId)
@@ -308,20 +307,20 @@ handleIncomingMessages node = go [] Map.empty Map.empty Set.empty
308307
mChannel <- withMVar (processState proc) $ return . (^. typedChannelWithId lcid)
309308
case mChannel of
310309
Just channel ->
311-
go (List.delete cid uninitConns)
310+
go (Set.delete cid uninitConns)
312311
procs
313312
(Map.insert cid channel chans)
314313
ctrls
315314
Nothing ->
316315
-- Unknown typed channel
317316
-- TODO (**) above
318-
go (List.delete cid uninitConns) procs chans ctrls
317+
go (Set.delete cid uninitConns) procs chans ctrls
319318
Nothing ->
320319
-- Unknown process
321320
-- TODO (**) above
322-
go (List.delete cid uninitConns) procs chans ctrls
321+
go (Set.delete cid uninitConns) procs chans ctrls
323322
NodeIdentifier _ ->
324-
go (List.delete cid uninitConns)
323+
go (Set.delete cid uninitConns)
325324
procs
326325
chans
327326
(Set.insert cid ctrls)
@@ -330,7 +329,7 @@ handleIncomingMessages node = go [] Map.empty Map.empty Set.empty
330329
-- TODO (**) above
331330
go uninitConns procs chans ctrls
332331
NT.ConnectionClosed cid ->
333-
go (List.delete cid uninitConns)
332+
go (Set.delete cid uninitConns)
334333
(Map.delete cid procs)
335334
(Map.delete cid chans)
336335
(Set.delete cid ctrls)
@@ -341,10 +340,11 @@ handleIncomingMessages node = go [] Map.empty Map.empty Set.empty
341340
{ ctrlMsgSender = nid
342341
, ctrlMsgSignal = Died nid DiedDisconnect
343342
}
344-
go (uninitConns List.\\ cids)
345-
(Map.filterWithKey (\k _ -> k `notElem` cids) procs)
346-
(Map.filterWithKey (\k _ -> k `notElem` cids) chans)
347-
(ctrls Set.\\ Set.fromList cids)
343+
let notRemoved k = k `notElem` cids
344+
go (Set.filter notRemoved uninitConns)
345+
(Map.filterWithKey (const . notRemoved) procs)
346+
(Map.filterWithKey (const . notRemoved) chans)
347+
(Set.filter notRemoved ctrls)
348348
NT.ErrorEvent (NT.TransportError (NT.EventConnectionLost Nothing _) _) ->
349349
-- TODO: We should treat an asymetrical connection loss (incoming
350350
-- connection broken, but outgoing connection still potentially ok)

0 commit comments

Comments
 (0)