From 8dac96f415151c4247d251b882b274d577289f79 Mon Sep 17 00:00:00 2001 From: JRoberts <8711996+jr-simplex@users.noreply.github.com> Date: Thu, 22 Dec 2022 19:18:38 +0400 Subject: [PATCH] core: wait chat started in deleteTimedItem threads (#1626) --- src/Simplex/Chat.hs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/Simplex/Chat.hs b/src/Simplex/Chat.hs index d989784f5..d3eb2f989 100644 --- a/src/Simplex/Chat.hs +++ b/src/Simplex/Chat.hs @@ -1739,8 +1739,7 @@ cleanupManager :: forall m. ChatMonad m => User -> m () cleanupManager user = do forever $ do flip catchError (toView . CRChatError) $ do - agentStarted <- asks agentAsync - atomically $ readTVar agentStarted >>= \a -> unless (isJust a) retry + waitChatStarted cleanupTimedItems threadDelay $ cleanupManagerInterval * 1000000 where @@ -1775,6 +1774,7 @@ deleteTimedItem :: ChatMonad m => User -> (ChatRef, ChatItemId) -> UTCTime -> m deleteTimedItem user (ChatRef cType chatId, itemId) deleteAt = do ts <- liftIO getCurrentTime threadDelay $ diffInMicros deleteAt ts + waitChatStarted case cType of CTDirect -> do (ct, ci) <- withStore $ \db -> (,) <$> getContact db user chatId <*> getDirectChatItem db user chatId itemId @@ -3589,6 +3589,11 @@ withUser action = withUser' $ \user -> chatStarted :: ChatMonad m => m Bool chatStarted = fmap isJust . readTVarIO =<< asks agentAsync +waitChatStarted :: ChatMonad m => m () +waitChatStarted = do + agentStarted <- asks agentAsync + atomically $ readTVar agentStarted >>= \a -> unless (isJust a) retry + withAgent :: ChatMonad m => (AgentClient -> ExceptT AgentErrorType m a) -> m a withAgent action = asks smpAgent