@@ -1145,7 +1145,8 @@ startJoinInvitation c userId connId sq_ enableNtfs cReqUri pqSup =
11451145 let cData = ConnData {userId, connId, connAgentVersion, enableNtfs, lastExternalSndId = 0 , deleted = False , ratchetSyncState = RSOk , pqSupport}
11461146 case sq_ of
11471147 Just sq@ SndQueue {e2ePubKey = Just _k} -> do
1148- e2eSndParams <- withStore c $ \ db ->
1148+ e2eSndParams <- withStore c $ \ db -> do
1149+ lockConnForUpdate db connId
11491150 getSndRatchet db connId v >>= \ case
11501151 Right r -> pure $ Right $ snd r
11511152 Left e -> do
@@ -1159,6 +1160,7 @@ startJoinInvitation c userId connId sq_ enableNtfs cReqUri pqSup =
11591160 sndKey_ = snd <$> invLink_
11601161 (q, _) <- lift $ newSndQueue userId " " qInfo sndKey_
11611162 withStore c $ \ db -> runExceptT $ do
1163+ liftIO $ lockConnForUpdate db connId
11621164 e2eSndParams <- createRatchet_ db g maxSupported pqSupport e2eRcvParams
11631165 sq' <- maybe (ExceptT $ updateNewConnSnd db connId q) pure sq_
11641166 pure (cData, sq', e2eSndParams, lnkId_)
@@ -1237,7 +1239,8 @@ joinConnSrv c nm userId connId enableNtfs cReqUri@CRContactUri {} cInfo pqSup su
12371239 AgentConfig {smpClientVRange = vr, smpAgentVRange, e2eEncryptVRange = e2eVR} <- asks config
12381240 let qUri = SMPQueueUri vr $ (rcvSMPQueueAddress rq) {queueMode = Just QMMessaging }
12391241 crData = ConnReqUriData SSSimplex smpAgentVRange [qUri] Nothing
1240- e2eRcvParams <- withStore' c $ \ db ->
1242+ e2eRcvParams <- withStore' c $ \ db -> do
1243+ lockConnForUpdate db connId
12411244 getRatchetX3dhKeys db connId >>= \ case
12421245 Right keys -> pure $ CR. mkRcvE2ERatchetParams (maxVersion e2eVR) keys
12431246 Left e -> do
@@ -1957,7 +1960,7 @@ runSmpQueueMsgDelivery c@AgentClient {subQ} sq@SndQueue {userId, connId, server,
19571960 withRetryLock2 ri' qLock $ \ riState loop -> do
19581961 liftIO $ waitWhileSuspended c
19591962 liftIO $ waitForUserNetwork c
1960- resp <- tryError $ case msgType of
1963+ resp <- tryAllErrors $ case msgType of
19611964 AM_CONN_INFO -> sendConfirmation c NRMBackground sq msgBody
19621965 AM_CONN_INFO_REPLY -> sendConfirmation c NRMBackground sq msgBody
19631966 _ -> case pendingMsgPrepData_ of
@@ -2097,10 +2100,12 @@ runSmpQueueMsgDelivery c@AgentClient {subQ} sq@SndQueue {userId, connId, server,
20972100 notifyDelMsgs :: InternalId -> AgentErrorType -> UTCTime -> AM ()
20982101 notifyDelMsgs msgId err expireTs = do
20992102 notifyDel msgId $ MERR (unId msgId) err
2100- msgIds_ <- withStore' c $ \ db -> getExpiredSndMessages db connId sq expireTs
2103+ msgIds_ <- withStore' c $ \ db -> do
2104+ msgIds_ <- getExpiredSndMessages db connId sq expireTs
2105+ forM_ msgIds_ $ \ msgId' -> deleteSndMsgDelivery db connId sq msgId' False `catchAll_` pure ()
2106+ pure msgIds_
21012107 forM_ (L. nonEmpty msgIds_) $ \ msgIds -> do
21022108 notify $ MERRS (L. map unId msgIds) err
2103- withStore' c $ \ db -> forM_ msgIds $ \ msgId' -> deleteSndMsgDelivery db connId sq msgId' False `catchAll_` pure ()
21042109 atomically $ incSMPServerStat' c userId server sentExpiredErrs (length msgIds_ + 1 )
21052110 delMsg :: InternalId -> AM ()
21062111 delMsg = delMsgKeep False
@@ -3025,7 +3030,8 @@ processSMPTransmissions c@AgentClient {subQ} (tSess@(userId, srv, _), _v, sessId
30253030 throwE e
30263031 agentClientMsg :: TVar ChaChaDRG -> ByteString -> AM (Maybe (InternalId , MsgMeta , AMessage , CR. RatchetX448 ))
30273032 agentClientMsg g encryptedMsgHash = withStore c $ \ db -> runExceptT $ do
3028- rc <- ExceptT $ getRatchet db connId -- ratchet state pre-decryption - required for processing EREADY
3033+ liftIO $ lockConnForUpdate db connId
3034+ rc <- ExceptT $ getRatchetForUpdate db connId -- ratchet state pre-decryption - required for processing EREADY
30293035 (agentMsgBody, pqEncryption) <- agentRatchetDecrypt' g db connId rc encAgentMessage
30303036 liftEither (parse smpP (SEAgentError $ AGENT A_MESSAGE ) agentMsgBody) >>= \ case
30313037 agentMsg@ (AgentMessage APrivHeader {sndMsgId, prevMsgHash} aMessage) -> do
@@ -3260,6 +3266,7 @@ processSMPTransmissions c@AgentClient {subQ} (tSess@(userId, srv, _), _v, sessId
32603266 Just sqs' -> do
32613267 (sq_@ SndQueue {sndPrivateKey}, dhPublicKey) <- lift $ newSndQueue userId connId qInfo Nothing
32623268 sq2 <- withStore c $ \ db -> do
3269+ lockConnForUpdate db connId
32633270 liftIO $ mapM_ (deleteConnSndQueue db connId) delSqs
32643271 addConnSndQueue db connId (sq_ :: NewSndQueue ) {primary = True , dbReplaceQueueId = Just dbQueueId}
32653272 logServer " <--" c srv rId $ " MSG <QADD>:" <> logSecret' srvMsgId <> " " <> logSecret (senderId queueAddress)
@@ -3564,7 +3571,7 @@ agentRatchetEncrypt db cData msg getPaddedLen pqEnc_ currentE2EVersion = do
35643571
35653572agentRatchetEncryptHeader :: DB. Connection -> ConnData -> (VersionSMPA -> PQSupport -> Int ) -> Maybe PQEncryption -> CR. VersionE2E -> ExceptT StoreError IO (CR. MsgEncryptKeyX448 , Int , PQEncryption )
35663573agentRatchetEncryptHeader db ConnData {connId, connAgentVersion = v, pqSupport} getPaddedLen pqEnc_ currentE2EVersion = do
3567- rc <- ExceptT $ getRatchet db connId
3574+ rc <- ExceptT $ getRatchetForUpdate db connId
35683575 let paddedLen = getPaddedLen v pqSupport
35693576 (mek, rc') <- withExceptT (SEAgentError . cryptoError) $ CR. rcEncryptHeader rc pqEnc_ currentE2EVersion
35703577 liftIO $ updateRatchet db connId rc' CR. SMDNoChange
@@ -3573,7 +3580,7 @@ agentRatchetEncryptHeader db ConnData {connId, connAgentVersion = v, pqSupport}
35733580-- encoded EncAgentMessage -> encoded AgentMessage
35743581agentRatchetDecrypt :: TVar ChaChaDRG -> DB. Connection -> ConnId -> ByteString -> ExceptT StoreError IO (ByteString , PQEncryption )
35753582agentRatchetDecrypt g db connId encAgentMsg = do
3576- rc <- ExceptT $ getRatchet db connId
3583+ rc <- ExceptT $ getRatchetForUpdate db connId
35773584 agentRatchetDecrypt' g db connId rc encAgentMsg
35783585
35793586agentRatchetDecrypt' :: TVar ChaChaDRG -> DB. Connection -> ConnId -> CR. RatchetX448 -> ByteString -> ExceptT StoreError IO (ByteString , PQEncryption )
0 commit comments