From 63c4f54a89a02fee1bd89a279e5b07b9e0b8a3b0 Mon Sep 17 00:00:00 2001 From: "Evgeny @ SimpleX Chat" <259188159+evgeny-simplex@users.noreply.github.com> Date: Sat, 21 Nov 2026 19:11:35 +0000 Subject: [PATCH] core, directory: stop cancelled actions instead of continuing them (#8279) --- apps/simplex-directory-service/src/Directory/Service.hs | 4 ++-- src/Simplex/Chat/AppSettings.hs | 4 ++-- src/Simplex/Chat/Bot/Store.hs | 4 ++-- src/Simplex/Chat/Library/Commands.hs | 6 +++--- src/Simplex/Chat/Library/Internal.hs | 6 +++--- src/Simplex/Chat/Library/Subscriber.hs | 2 +- src/Simplex/Chat/Terminal/Input.hs | 4 ++-- src/Simplex/Chat/Terminal/Notification.hs | 4 ++-- tests/Bots/DirectoryTests.hs | 8 ++++++++ 9 files changed, 25 insertions(+), 17 deletions(-) diff --git a/apps/simplex-directory-service/src/Directory/Service.hs b/apps/simplex-directory-service/src/Directory/Service.hs index f43f22b503..09afd6b43a 100644 --- a/apps/simplex-directory-service/src/Directory/Service.hs +++ b/apps/simplex-directory-service/src/Directory/Service.hs @@ -77,7 +77,7 @@ import Simplex.Messaging.Protocol (ErrorType (..)) import Simplex.Messaging.SimplexName (SimplexNameInfo (..), SimplexNameType (..), shortNameInfoStr) import Simplex.Messaging.TMap (TMap) import qualified Simplex.Messaging.TMap as TM -import Simplex.Messaging.Util (eitherToMaybe, raceAny_, safeDecodeUtf8, tshow, unlessM, (<$$>)) +import Simplex.Messaging.Util (catchOwn', eitherToMaybe, raceAny_, safeDecodeUtf8, tshow, unlessM, (<$$>)) import System.Directory (getAppUserDataDirectory, removeFile) import System.Exit (exitFailure) import System.Process (readProcess) @@ -341,7 +341,7 @@ directoryServiceEvent opts@DirectoryOpts {adminUsers, superUsers, serviceName, o SDRUser -> deUserCommand ct ciId cmd SDRAdmin -> deAdminCommand ct ciId cmd SDRSuperUser -> deSuperUserCommand ct ciId cmd - DEVoiceUploadEnded filePath -> void (try $ removeFile filePath :: IO (Either SomeException ())) + DEVoiceUploadEnded filePath -> removeFile filePath `catchOwn'` \_ -> pure () DELogChatResponse r -> logInfo r where groupLinkText (CCLink cReq sLnk_) = maybe (strEncodeTxt $ simplexChatContact cReq) strEncodeTxt sLnk_ diff --git a/src/Simplex/Chat/AppSettings.hs b/src/Simplex/Chat/AppSettings.hs index 32cee15bc9..068825d8b8 100644 --- a/src/Simplex/Chat/AppSettings.hs +++ b/src/Simplex/Chat/AppSettings.hs @@ -16,7 +16,7 @@ import Data.Text (Text) import Simplex.Chat.Types.UITheme import Simplex.Messaging.Client (NetworkConfig, defaultNetworkConfig) import Simplex.Messaging.Parsers (defaultJSON, dropPrefix, enumJSON) -import Simplex.Messaging.Util (catchAll_) +import Simplex.Messaging.Util (catchOwn') data AppPlatform = APIOS | APAndroid | APDesktop deriving (Show) @@ -290,4 +290,4 @@ instance FromJSON AppSettings where readAppSettings :: FilePath -> Maybe AppSettings -> IO AppSettings readAppSettings f platformDefaults = combineAppSettings (fromMaybe defaultAppSettings platformDefaults) . fromMaybe defaultParseAppSettings - <$> (J.decodeFileStrict f `catchAll_` pure Nothing) + <$> (J.decodeFileStrict f `catchOwn'` \_ -> pure Nothing) diff --git a/src/Simplex/Chat/Bot/Store.hs b/src/Simplex/Chat/Bot/Store.hs index bfa5b04ebc..3cd23c7b69 100644 --- a/src/Simplex/Chat/Bot/Store.hs +++ b/src/Simplex/Chat/Bot/Store.hs @@ -17,14 +17,14 @@ import Simplex.Chat.Controller import Simplex.Chat.Types import Simplex.Messaging.Agent.Store.Common (withTransaction) import qualified Simplex.Messaging.Agent.Store.DB as DB -import Simplex.Messaging.Util (catchAll) +import Simplex.Messaging.Util (catchOwn') withDB' :: Text -> ChatController -> (DB.Connection -> IO a) -> IO (Either String a) withDB' cxt cc a = withDB cxt cc $ ExceptT . fmap Right . a withDB :: Text -> ChatController -> (DB.Connection -> ExceptT String IO a) -> IO (Either String a) withDB cxt ChatController {chatStore} action = do - r_ <- withTransaction chatStore (runExceptT . action) `catchAll` (pure . Left . show) + r_ <- withTransaction chatStore (runExceptT . action) `catchOwn'` (pure . Left . show) case r_ of Left e -> logError $ "Database error: " <> cxt <> " " <> T.pack e Right _ -> pure () diff --git a/src/Simplex/Chat/Library/Commands.hs b/src/Simplex/Chat/Library/Commands.hs index d42763a714..ea2bf56444 100644 --- a/src/Simplex/Chat/Library/Commands.hs +++ b/src/Simplex/Chat/Library/Commands.hs @@ -218,7 +218,7 @@ loadImageData path = case map toLower (takeExtension path) of bs <- B.readFile path if B.null bs then pure $ Left $ "image file is empty: " <> path - else first (\e -> "image file cannot be resized: " <> path <> " (" <> e <> ")") <$> (runExceptT (resize bs) `catchAll` (pure . Left . show)) + else first (\e -> "image file cannot be resized: " <> path <> " (" <> e <> ")") <$> (runExceptT (resize bs) `catchOwn'` (pure . Left . show)) resize bs = do (img, profile) <- ExceptT $ decodeResizable bs ImageData . safeDecodeUtf8 . LB.toStrict <$> ExceptT (resizeImageDataUri (fromIntegral maxProfileImageSize) (cropToSquare img) profile) @@ -230,7 +230,7 @@ fixedImagePreview :: ImageData fixedImagePreview = ImageData "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAAAXNSR0IArs4c6QAAAKVJREFUeF7t1kENACEUQ0FQhnVQ9lfGO+xggITQdvbMzArPey+8fa3tAfwAEdABZQspQStgBssEcgAIkSAJkiAJljtEgiRIgmUCSZAESZAESZAEyx0iQRIkwTKBJEiCv5fgvTd1wDmn7QAP4AeIgA4oW0gJWgEzWCZwbQ7gAA7ggLKFOIADOKBMIAeAEAmSIAmSYLlDJEiCJFgmkARJkARJ8N8S/ADTZUewBvnTOQAAAABJRU5ErkJggg==" imagePreview :: CryptoFile -> IO ImageData -imagePreview f = either (const fixedImagePreview) (ImageData . safeDecodeUtf8 . LB.toStrict) <$> (runExceptT preview `catchAll` (pure . Left . show)) +imagePreview f = either (const fixedImagePreview) (ImageData . safeDecodeUtf8 . LB.toStrict) <$> (runExceptT preview `catchOwn'` (pure . Left . show)) where preview = do s <- withExceptT show $ CF.readFile f @@ -3639,7 +3639,7 @@ processChatCommand cxt nm = \case Just XFTPRcvFile {agentRcvFileId} -> do forM_ (liveRcvFileTransferPath ftr) $ \filePath -> do fsFilePath <- lift $ toFSFilePath filePath - liftIO $ removeFile fsFilePath `catchAll_` pure () + liftIO $ removeFile fsFilePath `catchOwn'` \_ -> pure () lift . forM_ agentRcvFileId $ \(AgentRcvFileId aFileId) -> withAgent' (`xftpDeleteRcvFile` aFileId) aci_ <- fmap updatedAChatItem <$> resetRcvCIFileStatus user fileId CIFSRcvInvitation diff --git a/src/Simplex/Chat/Library/Internal.hs b/src/Simplex/Chat/Library/Internal.hs index 36b5233010..cdf16d07e6 100644 --- a/src/Simplex/Chat/Library/Internal.hs +++ b/src/Simplex/Chat/Library/Internal.hs @@ -544,8 +544,8 @@ deleteFilesLocally files = where delete :: FilePath -> IO () delete fPath = - removeFile fPath `catchAll` \_ -> - removePathForcibly fPath `catchAll_` pure () + removeFile fPath `catchOwn'` \_ -> + removePathForcibly fPath `catchOwn'` \_ -> pure () -- perform an action only if filesFolder is set (i.e. on mobile devices) withFilesFolder :: (FilePath -> CM ()) -> CM () withFilesFolder action = asks filesFolder >>= readTVarIO >>= mapM_ action @@ -2182,7 +2182,7 @@ closeFileHandle :: Int64 -> (ChatController -> TVar (Map Int64 Handle)) -> CM' ( closeFileHandle fileId files = do fs <- asks files h_ <- atomically . stateTVar' fs $ \m -> (M.lookup fileId m, M.delete fileId m) - liftIO $ mapM_ hClose h_ `catchAll_` pure () + liftIO $ mapM_ hClose h_ `catchOwn'` \_ -> pure () -- The roster file has no chat item, so chat-item file enumeration misses it; clean it up by group. cleanupGroupRosterFile :: User -> GroupInfo -> CM () diff --git a/src/Simplex/Chat/Library/Subscriber.hs b/src/Simplex/Chat/Library/Subscriber.hs index 76bcb36be9..715f309234 100644 --- a/src/Simplex/Chat/Library/Subscriber.hs +++ b/src/Simplex/Chat/Library/Subscriber.hs @@ -1310,7 +1310,7 @@ processAgentMessageConn cxt user@User {userId} entity gks_ corrId agentConnId ag let newStatus = GSSError $ agentSndError err -- error cannot be AUTH error here withStore' $ \db -> forM_ msgIds $ \msgId -> - updateGroupItemsErrorStatus db msgId (groupMemberId' m) newStatus `catchAll_` pure () + updateGroupItemsErrorStatus db msgId (groupMemberId' m) newStatus `catchOwn'` \_ -> pure () eToView $ ChatErrorAgent err (AgentConnId agentConnId) (Just connEntity) ERR err@(AGENT (A_DUPLICATE (Just DroppedMsg {brokerTs, attempts}))) | isRelay membership -> diff --git a/src/Simplex/Chat/Terminal/Input.hs b/src/Simplex/Chat/Terminal/Input.hs index d6681518a9..d8be5f4a0a 100644 --- a/src/Simplex/Chat/Terminal/Input.hs +++ b/src/Simplex/Chat/Terminal/Input.hs @@ -35,7 +35,7 @@ import Simplex.Chat.Terminal.Output import Simplex.Chat.Types (User (..)) import Simplex.Messaging.Agent.Store.Common (DBStore, withTransaction) import qualified Simplex.Messaging.Agent.Store.DB as DB -import Simplex.Messaging.Util (catchAll_, safeDecodeUtf8, whenM) +import Simplex.Messaging.Util (catchOwn', safeDecodeUtf8, whenM) import System.Exit (exitSuccess) import System.Terminal hiding (insertChars) import UnliftIO.STM @@ -329,7 +329,7 @@ updateTermState user_ st chatPrefix live tw (key, ms) ts@TerminalState {inputStr "SELECT local_display_name FROM " <> table <> " WHERE user_id = ? AND local_display_name LIKE ?" getNameSfxs_ :: ToRow p => Text -> p -> Query -> IO [String] getNameSfxs_ pfx ps q = - withTransaction st (\db -> hasPfx pfx . map fromOnly <$> DB.query db q ps) `catchAll_` pure [] + withTransaction st (\db -> hasPfx pfx . map fromOnly <$> DB.query db q ps) `catchOwn'` \_ -> pure [] commands = ["connect", "search", "tail", "info", "clear", "delete", "code", "verify"] <> ["file", "freceive", "fcancel", "fstatus", "fforward", "image", "image_forward"] diff --git a/src/Simplex/Chat/Terminal/Notification.hs b/src/Simplex/Chat/Terminal/Notification.hs index 932eae32ef..b6f0a8b292 100644 --- a/src/Simplex/Chat/Terminal/Notification.hs +++ b/src/Simplex/Chat/Terminal/Notification.hs @@ -9,7 +9,7 @@ module Simplex.Chat.Terminal.Notification (Notification (..), initializeNotifica import Data.List (isInfixOf) import Data.Maybe (isJust) import qualified Data.Text as T -import Simplex.Messaging.Util (catchAll_) +import Simplex.Messaging.Util (catchOwn') import System.Directory (createDirectoryIfMissing, doesFileExist, findExecutable, getAppUserDataDirectory) import System.FilePath (combine) import System.Info (os) @@ -36,7 +36,7 @@ noNotifications :: Notification -> IO () noNotifications _ = pure () hideException :: (a -> IO ()) -> (a -> IO ()) -hideException f a = f a `catchAll_` pure () +hideException f a = f a `catchOwn'` \_ -> pure () initLinuxNotify :: IO (Notification -> IO ()) initLinuxNotify = do diff --git a/tests/Bots/DirectoryTests.hs b/tests/Bots/DirectoryTests.hs index c33d9df230..65f6a051f3 100644 --- a/tests/Bots/DirectoryTests.hs +++ b/tests/Bots/DirectoryTests.hs @@ -23,6 +23,7 @@ import Directory.Options import Directory.Service import System.Directory (emptyPermissions, setOwnerExecutable, setOwnerReadable, setOwnerWritable, setPermissions) import Simplex.Chat.Bot.KnownContacts +import Simplex.Chat.Bot.Store (withDB') import Simplex.Chat.Controller (ChatConfig (..), ChatController (smpAgent)) import qualified Simplex.Chat.Markdown as MD import Simplex.Chat.Options (ChatOpts (..), CoreChatOpts (..)) @@ -93,6 +94,8 @@ directoryServiceTests = do it "should respond to unknown command during captcha" testCaptchaUnknownCommand describe "store log" $ do it "should restore directory service state" testRestoreDirectory + describe "database" $ do + it "should stop a database action at its time limit" testDBActionTimeLimit describe "captcha" $ do it "should accept some incorrect spellings" testCaptcha it "should generate captcha of correct length" testGetCaptchaStr @@ -1688,6 +1691,11 @@ testRestoreDirectory ps = do cath #> "@'SimpleX Directory' security" groupFoundN' 2 cath "security" +testDBActionTimeLimit :: HasCallStack => TestParams -> IO () +testDBActionTimeLimit ps = + withNewTestChat ps "bob" bobProfile $ \TestCC {chatController = cc} -> + timeout 100000 (withDB' "testDBActionTimeLimit" cc $ \_ -> threadDelay 1000000) `shouldReturn` Nothing + testCaptcha :: HasCallStack => TestParams -> IO () testCaptcha _ps = do let captcha = "23456789ABCDEFGHIJKLMNOPQRSTUVWXYZabdefghijkmnpqrty"