core, directory: stop cancelled actions instead of continuing them (#8279)

This commit is contained in:
Evgeny @ SimpleX Chat authored and GitHub committed 2026-11-21 19:11:35 +00:00
1 parent 20ee7cb148
commit 63c4f54a89
9 files changed
+25 -17

No files matched your search

@@ -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_
+2 -2
View File
@@ -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)
+2 -2
View File
@@ -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 ()
+3 -3
View File
@@ -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
+3 -3
View File
@@ -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 ()
+1 -1
View File
@@ -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 ->
+2 -2
View File
@@ -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"]
+2 -2
View File
@@ -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
+8
View File
@@ -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"