lib: evaluate the new TVar state in stateTVar updates (#2031)

* lib: evaluate random generator state when it is updated

* lib: evaluate the new state in all stateTVar updates
This commit is contained in:
Evgeny @ SimpleX Chat authored and GitHub committed 2026-11-07 23:09:51 +00:00
1 parent db998ca19f
commit 91bfba321b
15 files changed
+49 -40

No files matched your search

+4 -4
View File
@@ -70,7 +70,7 @@ import Simplex.Messaging.Encoding
import Simplex.Messaging.Encoding.String (StrEncoding (..))
import Simplex.Messaging.Parsers (parseAll)
import Simplex.Messaging.Protocol (ProtoServerWithAuth (..), ProtocolServer (..), SenderId, SndPrivateAuthKey, XFTPServer, XFTPServerWithAuth)
import Simplex.Messaging.Util (groupAllOn, ifM, tshow, whenM)
import Simplex.Messaging.Util (groupAllOn, ifM, stateTVar', tshow, whenM)
import System.Exit (exitFailure)
import System.FilePath (splitFileName, (</>))
import System.IO.Temp (getCanonicalTemporaryDirectory)
@@ -331,7 +331,7 @@ cliSendFileOpts SendOptions {filePath, outputDir, numRecipients, xftpServers, re
(sndId, rIds, _) <- withRetry retryCount $ createXFTPChunk c spKey ch (L.map fst rKeys) auth Nothing
withReconnect a xftpServer retryCount $ \c' -> uploadXFTPChunk c' spKey sndId chunkSpec
logDebug $ "uploaded chunk " <> tshow chunkNo
uploaded <- atomically . stateTVar uploadedChunks $ \cs ->
uploaded <- atomically . stateTVar' uploadedChunks $ \cs ->
let cs' = fromIntegral chunkSize : cs in (sum cs', cs')
liftIO $ do
notifyProgress uploaded encSize
@@ -343,7 +343,7 @@ cliSendFileOpts SendOptions {filePath, outputDir, numRecipients, xftpServers, re
getXFTPServer gen = \case
srv :| [] -> pure srv
servers -> do
atomically $ (servers L.!!) <$> stateTVar gen (randomR (0, L.length servers - 1))
atomically $ (servers L.!!) <$> stateTVar' gen (randomR (0, L.length servers - 1))
-- M chunks, R replicas, N recipients
-- rcvReplicas: M[SentFileChunk] -> M * R * N [SentRecipientReplica]
@@ -462,7 +462,7 @@ cliReceiveFile ReceiveOptions {fileDescription, filePath, retryCount, tempPath,
let chunkSpec = XFTPRcvChunkSpec chunkPath (unFileSize chunkSize) (unFileDigest digest)
withReconnect a server retryCount $ \c -> downloadXFTPChunk g c replicaKey (unChunkReplicaId replicaId) chunkSpec
logDebug $ "downloaded chunk " <> tshow chunkNo <> " to " <> T.pack chunkPath
downloaded <- atomically . stateTVar downloadedChunks $ \cs ->
downloaded <- atomically . stateTVar' downloadedChunks $ \cs ->
let cs' = fromIntegral (unFileSize chunkSize) : cs in (sum cs', cs')
liftIO $ do
printProgress "Downloaded" downloaded encSize
+1 -1
View File
@@ -589,7 +589,7 @@ processXFTPRequest ent HTTP2Body {bodyPart} = \case
reserve = do
us <- asks usedStorage
quota <- asks $ fromMaybe maxBound . fileSizeQuota . config
atomically . stateTVar us $
atomically . stateTVar' us $
\used -> let used' = used + fromIntegral size in if used' <= quota then (True, used') else (False, used)
receive restore = do
path <- asks $ filesPath . config
+7 -7
View File
@@ -458,7 +458,7 @@ getAgentWorker' toW fromW name hasWork c@AgentClient {agentEnv} key ws work = do
newWorker :: AgentClient -> STM Worker
newWorker c = do
workerId <- stateTVar (workerSeq c) $ \next -> (next, next + 1)
workerId <- stateTVar' (workerSeq c) $ \next -> (next, next + 1)
doWork <- newTMVar () -- new worker is created with "some work to do" (indicated by () in TMVar)
action <- newTMVar Nothing
restarts <- newTVar $ RestartCount 0 0
@@ -1068,7 +1068,7 @@ reconnectSMPServer c userId srv = do
closeUserXFTPClients :: AgentClient -> UserId -> IO ()
closeUserXFTPClients c userId = do
vs <- atomically $ stateTVar (xftpClients c) $ \cs ->
vs <- atomically $ stateTVar' (xftpClients c) $ \cs ->
let (userCs, cs') = M.partitionWithKey (\(userId', _, _) _ -> userId == userId') cs
in (M.elems userCs, cs')
mapM_ (forkIO . closeClient_ c) vs
@@ -2390,7 +2390,7 @@ suspendSendingAndDatabase c =
suspendOperation :: AgentClient -> AgentOperation -> STM () -> STM ()
suspendOperation c op endedAction = do
n <- stateTVar (agentOpSel op c) $ \s -> (opsInProgress s, s {opSuspended = True})
n <- stateTVar' (agentOpSel op c) $ \s -> (opsInProgress s, s {opSuspended = True})
-- unsafeIOToSTM $ putStrLn $ "suspendOperation_ " <> show op <> " " <> show n
when (n == 0) $ whenSuspending c endedAction
@@ -2402,7 +2402,7 @@ notifySuspended c = do
endOperation :: AgentClient -> AgentOperation -> STM () -> STM ()
endOperation c op endedAction = do
(suspended, n) <- stateTVar (agentOpSel op c) $ \s ->
(suspended, n) <- stateTVar' (agentOpSel op c) $ \s ->
let n = max 0 (opsInProgress s - 1)
in ((opSuspended s, n), s {opsInProgress = n})
-- unsafeIOToSTM $ putStrLn $ "endOperation: " <> show op <> " " <> show suspended <> " " <> show n
@@ -2414,12 +2414,12 @@ whenSuspending c = whenM ((== ASSuspending) <$> readTVar (agentState c))
addSndFileInProgress :: AgentClient -> SndFileId -> STM ()
addSndFileInProgress c fId = do
added <- stateTVar (sndFilesInProgress c) $ \fs -> (S.notMember fId fs, S.insert fId fs)
added <- stateTVar' (sndFilesInProgress c) $ \fs -> (S.notMember fId fs, S.insert fId fs)
when added $ modifyTVar' (sndFilesOp c) $ \s -> s {opsInProgress = opsInProgress s + 1}
removeSndFileInProgress :: AgentClient -> SndFileId -> STM ()
removeSndFileInProgress c fId = do
removed <- stateTVar (sndFilesInProgress c) $ \fs -> (S.member fId fs, S.delete fId fs)
removed <- stateTVar' (sndFilesInProgress c) $ \fs -> (S.member fId fs, S.delete fId fs)
when removed $ endAgentOperation c AOSndFiles
removeDeletedOrFailedSndFilesInProgress :: AgentClient -> IO ()
@@ -2551,7 +2551,7 @@ pickServer = \case
(_, srv) :| [] -> pure srv
servers -> do
gen <- asks randomServer
atomically $ snd . (servers L.!!) <$> stateTVar gen (randomR (0, L.length servers - 1))
atomically $ snd . (servers L.!!) <$> stateTVar' gen (randomR (0, L.length servers - 1))
getNextServer ::
(ProtocolTypeI p, UserProtocol p) =>
@@ -53,7 +53,7 @@ import Simplex.Messaging.Notifications.Protocol
import Simplex.Messaging.Notifications.Types
import Simplex.Messaging.Protocol (NtfServer, sameSrvAddr)
import qualified Simplex.Messaging.Protocol as SMP
import Simplex.Messaging.Util (catchAllErrors, catchAllErrors', diffToMicroseconds, threadDelay', tryAllErrors, tshow, whenM)
import Simplex.Messaging.Util (catchAllErrors, catchAllErrors', diffToMicroseconds, stateTVar', threadDelay', tryAllErrors, tshow, whenM)
import System.Random (randomR)
import UnliftIO
import UnliftIO.Concurrent (forkIO)
@@ -596,5 +596,5 @@ getNtfServer c = do
[srv] -> pure $ Just srv
servers -> do
gen <- asks randomServer
atomically . stateTVar gen $
atomically . stateTVar' gen $
first (Just . (servers !!)) . randomR (0, length servers - 1)
+3 -3
View File
@@ -82,7 +82,7 @@ import Simplex.Messaging.Session
import Simplex.Messaging.TMap (TMap)
import qualified Simplex.Messaging.TMap as TM
import Simplex.Messaging.Transport
import Simplex.Messaging.Util (catchAll_, ifM, safeDecodeUtf8, toChunks, tshow, whenM, ($>>=), (<$$>))
import Simplex.Messaging.Util (catchAll_, ifM, safeDecodeUtf8, stateTVar', toChunks, tshow, whenM, ($>>=), (<$$>))
import System.Timeout (timeout)
import UnliftIO (async)
import UnliftIO.STM
@@ -288,7 +288,7 @@ connectClient ca@SMPClientAgent {agentCfg, dbService, smpClients, smpSessions, m
sessId = sessionId $ thParams smp
updateServiceSub sVar = do -- (sub, sessId')
-- We don't change active subscription in case session ID is different from disconnected client
serviceSub_ <- stateTVar sVar $ \case
serviceSub_ <- stateTVar' sVar $ \case
Just (serviceSub, sessId') | sessId == sessId' -> (Just serviceSub, Nothing)
s -> (Nothing, s)
-- We don't reset pending subscription to Nothing here to avoid any race conditions
@@ -298,7 +298,7 @@ connectClient ca@SMPClientAgent {agentCfg, dbService, smpClients, smpSessions, m
updateQueueSubs qVar = do
-- removing subscriptions that have matching sessionId to disconnected client
-- and keep the other ones (they can be made by the new client)
subs <- M.map snd <$> stateTVar qVar (M.partition ((sessId ==) . fst))
subs <- M.map snd <$> stateTVar' qVar (M.partition ((sessId ==) . fst))
if M.null subs
then pure Nothing
else Just subs <$ addSubs_ (pendingQueueSubs ca) srv subs
+5 -5
View File
@@ -261,7 +261,7 @@ import Simplex.Messaging.Agent.Store.DB (Binary (..), FromField (..), ToField (.
import Simplex.Messaging.Encoding
import Simplex.Messaging.Encoding.String
import Simplex.Messaging.Parsers (parseAll, parseString)
import Simplex.Messaging.Util (packZipWith, (<$?>))
import Simplex.Messaging.Util (packZipWith, stateTVar', (<$?>))
-- | Cryptographic algorithms.
data Algorithm = Ed25519 | Ed448 | X25519 | X448
@@ -735,7 +735,7 @@ newRandom :: IO (TVar ChaChaDRG)
newRandom = newTVarIO =<< drgNew
newRandomDRG :: TVar ChaChaDRG -> STM (TVar ChaChaDRG)
newRandomDRG g = newTVar =<< stateTVar g (`withDRG` drgNew)
newRandomDRG g = newTVar =<< stateTVar' g (`withDRG` drgNew)
generateAKeyPair :: AlgorithmI a => SAlgorithm a -> TVar ChaChaDRG -> STM AKeyPair
generateAKeyPair a g = bimap (APublicKey a) (APrivateKey a) <$> generateKeyPair g
@@ -753,7 +753,7 @@ generateDhKeyPair :: (AlgorithmI a, DhAlgorithm a) => SAlgorithm a -> TVar ChaCh
generateDhKeyPair a g = bimap (APublicDhKey a) (APrivateDhKey a) <$> generateKeyPair g
generateKeyPair :: forall a. AlgorithmI a => TVar ChaChaDRG -> STM (KeyPair a)
generateKeyPair g = stateTVar g (`withDRG` generateKeyPair_)
generateKeyPair g = stateTVar' g (`withDRG` generateKeyPair_)
generateKeyPair_ :: forall a. AlgorithmI a => MonadPseudoRandom ChaChaDRG (KeyPair a)
generateKeyPair_ = do
@@ -761,7 +761,7 @@ generateKeyPair_ = do
pure (toPublic pk, pk)
generatePrivateKey :: forall a. AlgorithmI a => TVar ChaChaDRG -> STM (PrivateKey a)
generatePrivateKey g = stateTVar g (`withDRG` generatePrivateKey_)
generatePrivateKey g = stateTVar' g (`withDRG` generatePrivateKey_)
generatePrivateKey_ :: forall a. AlgorithmI a => MonadPseudoRandom ChaChaDRG (PrivateKey a)
generatePrivateKey_ = case sAlgorithm @a of
@@ -1436,7 +1436,7 @@ randomCbNonce :: TVar ChaChaDRG -> STM CbNonce
randomCbNonce = fmap CryptoBoxNonce . randomBytes 24
randomBytes :: Int -> TVar ChaChaDRG -> STM ByteString
randomBytes n gVar = stateTVar gVar $ randomBytesGenerate n
randomBytes n gVar = stateTVar' gVar $ randomBytesGenerate n
reverseNonce :: CbNonceI n => n -> CbNonce
reverseNonce = CryptoBoxNonce . B.reverse . unCbNonce
+3 -3
View File
@@ -36,7 +36,7 @@ import qualified Simplex.Messaging.Crypto as C
import Simplex.Messaging.Crypto.Lazy (LazyByteString)
import qualified Simplex.Messaging.Crypto.Lazy as LC
import Simplex.Messaging.Parsers (defaultJSON)
import Simplex.Messaging.Util (liftEitherWith)
import Simplex.Messaging.Util (liftEitherWith, stateTVar')
import System.Directory (getFileSize)
import UnliftIO (Handle, IOMode (..), liftIO)
import qualified UnliftIO as IO
@@ -81,7 +81,7 @@ withFile (CryptoFile path cfArgs) mode action = do
hPut :: CryptoFileHandle -> LazyByteString -> IO ()
hPut (CFHandle h sb_) s = LB.hPut h =<< maybe (pure s) encrypt sb_
where
encrypt sb = atomically $ stateTVar sb (`LC.sbEncryptChunkLazy` s)
encrypt sb = atomically $ stateTVar' sb (`LC.sbEncryptChunkLazy` s)
hPutTag :: CryptoFileHandle -> IO ()
hPutTag (CFHandle h sb_) = forM_ sb_ $ B.hPut h . BA.convert . LC.sbAuth <=< readTVarIO
@@ -89,7 +89,7 @@ hPutTag (CFHandle h sb_) = forM_ sb_ $ B.hPut h . BA.convert . LC.sbAuth <=< rea
hGet :: CryptoFileHandle -> Int -> IO ByteString
hGet (CFHandle h sb_) n = B.hGet h n >>= maybe pure decrypt sb_
where
decrypt sb s = atomically $ stateTVar sb (`LC.sbDecryptChunk` s)
decrypt sb s = atomically $ stateTVar' sb (`LC.sbDecryptChunk` s)
-- | Read and validate the auth tag.
-- This function should be called after reading the whole file, it assumes you know the file size and read only the needed bytes.
+2 -2
View File
@@ -1064,7 +1064,7 @@ runClientTransport h@THandle {params = thParams@THandleParams {sessionId}} = do
q <- asks $ tbqSize . config
ts <- liftIO getSystemTime
nextClientId <- asks clientSeq
clientId <- atomically $ stateTVar nextClientId $ \next -> (next, next + 1)
clientId <- atomically $ stateTVar' nextClientId $ \next -> (next, next + 1)
c <- liftIO $ newClient clientId q thParams ts
runClientThreads c `finally` clientDisconnected c
where
@@ -1399,7 +1399,7 @@ dummyKeyX25519 = "MCowBQYDK2VuAyEA4JGSMYht18H4mas/jHeBwfcM7jLwNYJNOAhi2/g4RXg="
forkClient :: MonadUnliftIO m => Client s -> String -> m () -> m ()
forkClient Client {endThreads, endThreadSeq} label action = do
tId <- atomically $ stateTVar endThreadSeq $ \next -> (next, next + 1)
tId <- atomically $ stateTVar' endThreadSeq $ \next -> (next, next + 1)
t <- forkIO $ do
labelMyThread label
action `finally` atomically (modifyTVar' endThreads $ IM.delete tId)
@@ -48,7 +48,7 @@ import Simplex.Messaging.SystemTime
import Simplex.Messaging.TMap (TMap)
import qualified Simplex.Messaging.TMap as TM
import Simplex.Messaging.Transport (SMPServiceRole (..))
import Simplex.Messaging.Util (anyM, ifM, tshow, ($>>), ($>>=), (<$$), (<$$>))
import Simplex.Messaging.Util (anyM, ifM, stateTVar', tshow, ($>>), ($>>=), (<$$), (<$$>))
import System.IO
import UnliftIO.STM
@@ -390,7 +390,7 @@ withQueueRec qr a = atomically $ readQueueRec qr >>= mapM a
setStatus :: TVar (Maybe QueueRec) -> ServerEntityStatus -> IO (Either ErrorType ())
setStatus qr status =
atomically $ stateTVar qr $ \case
atomically $ stateTVar' qr $ \case
Just q -> (Right (), Just q {status})
Nothing -> (Left AUTH, Nothing)
+2 -2
View File
@@ -19,7 +19,7 @@ import Control.Monad.IO.Unlift (MonadUnliftIO)
import Data.Time (UTCTime)
import Simplex.Messaging.TMap (TMap)
import qualified Simplex.Messaging.TMap as TM
import Simplex.Messaging.Util (whenM, ($>>=))
import Simplex.Messaging.Util (stateTVar', whenM, ($>>=))
import UnliftIO.Exception (bracketOnError)
data SessionVar a = SessionVar
@@ -43,7 +43,7 @@ getSessVarIO sessSeq sessKey vs ts = TM.lookupOrInsertIO sessKey (createSessionV
createSessionVar :: TVar Int -> UTCTime -> STM (SessionVar a)
createSessionVar sessSeq sessionVarTs = do
sessionVar <- newEmptyTMVar
sessionVarId <- stateTVar sessSeq $ \next -> (next, next + 1)
sessionVarId <- stateTVar' sessSeq $ \next -> (next, next + 1)
pure SessionVar {sessionVar, sessionVarId, sessionVarTs}
removeSessVar :: Ord k => SessionVar a -> k -> TMap k (SessionVar a) -> STM ()
+3 -2
View File
@@ -27,6 +27,7 @@ where
import Control.Concurrent.STM
import Data.Map.Strict (Map)
import qualified Data.Map.Strict as M
import Simplex.Messaging.Util (stateTVar')
type TMap k a = TVar (Map k a)
@@ -75,7 +76,7 @@ delete k m = modifyTVar' m $ M.delete k
{-# INLINE delete #-}
lookupInsert :: Ord k => k -> a -> TMap k a -> STM (Maybe a)
lookupInsert k v m = stateTVar m $ M.alterF (,Just v) k
lookupInsert k v m = stateTVar' m $ M.alterF (,Just v) k
{-# INLINE lookupInsert #-}
-- double lookup avoids STM transaction with a shared map in most cases
@@ -86,7 +87,7 @@ lookupOrInsertIO k new m = lookupIO k m >>= maybe (atomically lookupOrInsert) (p
{-# INLINE lookupOrInsertIO #-}
lookupDelete :: Ord k => k -> TMap k a -> STM (Maybe a)
lookupDelete k m = stateTVar m $ M.alterF (,Nothing) k
lookupDelete k m = stateTVar' m $ M.alterF (,Nothing) k
{-# INLINE lookupDelete #-}
adjust :: Ord k => (a -> a) -> k -> TMap k a -> STM ()
+3 -3
View File
@@ -143,7 +143,7 @@ import Simplex.Messaging.Server.Information
import Simplex.Messaging.SystemTime (SystemSeconds)
import Simplex.Messaging.Transport.Buffer
import Simplex.Messaging.Transport.Shared
import Simplex.Messaging.Util (bshow, catchAll, catchAll_, liftEitherWith, (<$$>))
import Simplex.Messaging.Util (bshow, catchAll, catchAll_, liftEitherWith, stateTVar', (<$$>))
import Simplex.Messaging.Version
import Simplex.Messaging.Version.Internal
import System.IO.Error (isEOFError)
@@ -715,7 +715,7 @@ tPutBlock :: Transport c => THandle v c p -> ByteString -> IO (Either TransportE
tPutBlock THandle {connection = c, params = THandleParams {blockSize, encryptBlock}} block = do
block_ <- case encryptBlock of
Just TSbChainKeys {sndKey} -> do
(sk, nonce) <- atomically $ stateTVar sndKey C.sbcHkdf
(sk, nonce) <- atomically $ stateTVar' sndKey C.sbcHkdf
pure $ C.sbEncrypt sk nonce block (blockSize - 16)
Nothing -> pure $ C.pad block blockSize
bimapM (const $ pure TELargeMsg) (cPut c) block_
@@ -729,7 +729,7 @@ tGetBlock THandle {connection = c, params = THandleParams {blockSize, encryptBlo
first (const TELargeMsg) <$>
case encryptBlock of
Just TSbChainKeys {rcvKey} -> do
(sk, nonce) <- atomically $ stateTVar rcvKey C.sbcHkdf
(sk, nonce) <- atomically $ stateTVar' rcvKey C.sbcHkdf
pure $ C.sbDecrypt sk nonce msg
Nothing -> pure $ C.unPad msg
else ioe_EOF
+2 -2
View File
@@ -51,7 +51,7 @@ import Network.Socket
import qualified Network.TLS as T
import Simplex.Messaging.Transport
import Simplex.Messaging.Transport.Shared
import Simplex.Messaging.Util (catchAll_, labelMyThread, tshow, unlessM)
import Simplex.Messaging.Util (catchAll_, labelMyThread, stateTVar', tshow, unlessM)
import System.Exit (exitFailure)
import System.IO.Error (tryIOError)
import System.Mem.Weak (Weak, deRefWeak)
@@ -175,7 +175,7 @@ runTCPServerSocket :: SocketState -> TMVar Bool -> IO Socket -> (Socket -> IO ()
runTCPServerSocket (accepted, gracefullyClosed, clients) started getSocket server =
E.bracket getSocket (closeServer started clients) $ \sock ->
forever . E.bracketOnError (safeAccept sock) (close . fst) $ \(conn, _peer) -> do
cId <- atomically $ stateTVar accepted $ \cId -> let cId' = cId + 1 in cId' `seq` (cId', cId')
cId <- atomically $ stateTVar' accepted $ \cId -> let cId' = cId + 1 in (cId', cId')
closed <- newTVarIO False
let closeConn _ = do
atomically $ writeTVar closed True >> modifyTVar' clients (IM.delete cId)
+8
View File
@@ -27,6 +27,7 @@ module Simplex.Messaging.Util
mapAccumLM,
packZipWith,
tryWriteTBQueue,
stateTVar',
catchAll,
catchAll_,
tryAllErrors,
@@ -260,6 +261,13 @@ tryWriteTBQueue q a = do
pure $ not full
{-# INLINE tryWriteTBQueue #-}
stateTVar' :: TVar s -> (s -> (a, s)) -> STM a
stateTVar' var f = do
(a, s') <- f <$> readTVar var
s' `seq` writeTVar var s'
pure a
{-# INLINE stateTVar' #-}
catchAll :: IO a -> (E.SomeException -> IO a) -> IO a
catchAll = E.catch
{-# INLINE catchAll #-}
+2 -2
View File
@@ -231,7 +231,7 @@ prepareHostSession
knownHost' <- updateKnownHost ca dhPubKey
let ctrlHello = RCCtrlHello {}
-- TODO send error response if something fails
(sk, nonce') <- atomically $ stateTVar sndKey C.sbcHkdf
(sk, nonce') <- atomically $ stateTVar' sndKey C.sbcHkdf
encBody' <- liftEitherWith (const RCEBlockSize) $ C.sbEncrypt sk nonce' (LB.toStrict $ J.encode ctrlHello) helloBlockSize
let ctrlEncHello = RCCtrlEncHello {kem = kemCiphertext, encBody = encBody'}
pure (ctrlEncHello, keys, hostHello, pairing {knownHost = Just knownHost'})
@@ -356,7 +356,7 @@ prepareCtrlSession
let KEMHybridSecret hybridKey = kemHybridSecret dhPubKey dhPrivKey kemSharedKey
-- keys are swapped in controller
(sndKey, rcvKey) <- swap <$> bimapM newTVarIO newTVarIO (C.sbcInit "" hybridKey)
(sk, nonce) <- atomically $ stateTVar rcvKey C.sbcHkdf
(sk, nonce) <- atomically $ stateTVar' rcvKey C.sbcHkdf
helloBody <- liftEitherWith (const RCEDecrypt) $ C.sbDecrypt sk nonce encBody
logDebug "Decrypted ctrl HELLO"
RCCtrlHello {} <- liftEitherWith RCESyntax $ J.eitherDecodeStrict helloBody