fix: deadlock when connecting/receiving connections rapidly
This commit is contained in:
@@ -29,8 +29,10 @@ import Control.Concurrent.STM
|
|||||||
, readTMVar
|
, readTMVar
|
||||||
, readTVar
|
, readTVar
|
||||||
, readTVarIO
|
, readTVarIO
|
||||||
|
, tryPutTMVar
|
||||||
, tryTakeTMVar
|
, tryTakeTMVar
|
||||||
, writeTQueue
|
, writeTQueue
|
||||||
|
, writeTVar
|
||||||
)
|
)
|
||||||
import Control.Exception (SomeException, try)
|
import Control.Exception (SomeException, try)
|
||||||
import Control.Monad (forM_, forever, unless, void)
|
import Control.Monad (forM_, forever, unless, void)
|
||||||
@@ -112,7 +114,7 @@ handleNewConn ch = do
|
|||||||
Nothing -> return ()
|
Nothing -> return ()
|
||||||
Just p -> do
|
Just p -> do
|
||||||
modifyTVar (rtConnPromises rt) (Map.delete peerAddr)
|
modifyTVar (rtConnPromises rt) (Map.delete peerAddr)
|
||||||
putTMVar p ref
|
void $ tryPutTMVar p ref
|
||||||
_else -> liftIO $ chClose ch
|
_else -> liftIO $ chClose ch
|
||||||
|
|
||||||
-- Connection pool
|
-- Connection pool
|
||||||
@@ -140,9 +142,11 @@ getOrCreateConn peer = do
|
|||||||
liftIO $ chSend ch (encode (NMHandshake (rtNodeId rt)))
|
liftIO $ chSend ch (encode (NMHandshake (rtNodeId rt)))
|
||||||
ref <- spawnConnTree peer ch
|
ref <- spawnConnTree peer ch
|
||||||
liftIO $ atomically $ do
|
liftIO $ atomically $ do
|
||||||
modifyTVar (rtConnections rt) (Map.insert peer ref)
|
conns <- readTVar (rtConnections rt)
|
||||||
|
unless (Map.member peer conns) $
|
||||||
|
modifyTVar (rtConnections rt) (Map.insert peer ref)
|
||||||
modifyTVar (rtConnPromises rt) (Map.delete peer)
|
modifyTVar (rtConnPromises rt) (Map.delete peer)
|
||||||
putTMVar promise ref
|
void $ tryPutTMVar promise ref
|
||||||
return ref
|
return ref
|
||||||
|
|
||||||
-- Message dispatch
|
-- Message dispatch
|
||||||
|
|||||||
Reference in New Issue
Block a user