Skip to content

Commit f8f172f

Browse files
agent: fix race when pending subscriptions are never subscribed (#1756)
* agent: fix race when pending subscriptions are never subscribed * small agent --------- Co-authored-by: Evgeny @ SimpleX Chat <259188159+evgeny-simplex@users.noreply.github.com>
1 parent 9c07ddf commit f8f172f

2 files changed

Lines changed: 12 additions & 6 deletions

File tree

src/Simplex/Messaging/Agent/Client.hs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -764,12 +764,15 @@ resubscribeSMPSession c@AgentClient {smpSubWorkers, workerSeq} tSess = do
764764
(pure Nothing) -- prevent race with cleanup and adding pending queues in another call
765765
(Just <$> getSessVar workerSeq tSess smpSubWorkers ts)
766766
newSubWorker v = do
767-
a <- async $ void (E.tryAny runSubWorker) >> atomically (cleanup v)
767+
a <- async $ void $ E.tryAny $ runSubWorker v
768768
atomically $ putTMVar (sessionVar v) a
769-
runSubWorker = do
769+
runSubWorker v = do
770770
ri <- asks $ reconnectInterval . config
771771
withRetryForeground ri isForeground (isNetworkOnline c) $ \_ loop -> do
772-
pending <- atomically $ SS.getPendingSubs tSess $ currentSubs c
772+
pending <- atomically $ do
773+
qs <- SS.getPendingSubs tSess $ currentSubs c
774+
when (M.null qs) $ cleanup v
775+
pure qs
773776
unless (M.null pending) $ do
774777
liftIO $ waitUntilForeground c
775778
liftIO $ waitForUserNetwork c

src/Simplex/Messaging/Client/Agent.hs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -307,11 +307,14 @@ reconnectClient ca@SMPClientAgent {active, agentCfg, smpSubWorkers, workerSeq} s
307307
(Just <$> getSessVar workerSeq srv smpSubWorkers ts)
308308
newSubWorker :: SessionVar (Async ()) -> IO ()
309309
newSubWorker v = do
310-
a <- async $ void (E.tryAny runSubWorker) >> atomically (cleanup v)
310+
a <- async $ void $ E.tryAny $ runSubWorker v
311311
atomically $ putTMVar (sessionVar v) a
312-
runSubWorker =
312+
runSubWorker v =
313313
withRetryInterval (reconnectInterval agentCfg) $ \_ loop -> do
314-
subs <- getPending TM.lookupIO readTVarIO
314+
subs <- atomically $ do
315+
s <- getPending TM.lookup readTVar
316+
when (noPending s) $ cleanup v
317+
pure s
315318
unless (noPending subs) $ whenM (readTVarIO active) $ do
316319
void $ netTimeoutInt tcpConnectTimeout NRMBackground `timeout` runExceptT (reconnectSMPClient ca srv subs)
317320
loop

0 commit comments

Comments
 (0)