From 317c68a86095989975407abfd2d264fa28ac0000 Mon Sep 17 00:00:00 2001 From: IC Rainbow Date: Tue, 21 Nov 2023 14:52:26 +0200 Subject: [PATCH 1/4] agent: fix leak in AgentOpState counters --- src/Simplex/Messaging/Agent/Client.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Simplex/Messaging/Agent/Client.hs b/src/Simplex/Messaging/Agent/Client.hs index 190a4bf12..a2180a337 100644 --- a/src/Simplex/Messaging/Agent/Client.hs +++ b/src/Simplex/Messaging/Agent/Client.hs @@ -276,7 +276,7 @@ agentOpSel = \case agentOperations :: [AgentClient -> TVar AgentOpState] agentOperations = [ntfNetworkOp, rcvNetworkOp, msgDeliveryOp, sndNetworkOp, databaseOp] -data AgentOpState = AgentOpState {opSuspended :: Bool, opsInProgress :: Int} +data AgentOpState = AgentOpState {opSuspended :: !Bool, opsInProgress :: !Int} data AgentState = ASForeground | ASSuspending | ASSuspended deriving (Eq, Show) From 2fe408666e24e668a198376102bff2c3a095c647 Mon Sep 17 00:00:00 2001 From: IC Rainbow Date: Tue, 21 Nov 2023 19:54:10 +0200 Subject: [PATCH 2/4] fix testAsyncCommandsRestore test --- tests/AgentTests/FunctionalAPITests.hs | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/AgentTests/FunctionalAPITests.hs b/tests/AgentTests/FunctionalAPITests.hs index 8c37579fc..2774b620a 100644 --- a/tests/AgentTests/FunctionalAPITests.hs +++ b/tests/AgentTests/FunctionalAPITests.hs @@ -1279,6 +1279,7 @@ testAsyncCommandsRestore t = do ("1", _, INV _) <- get alice' pure () disconnectAgentClient alice' + threadDelay 1000000 testAcceptContactAsync :: IO () testAcceptContactAsync = From 79d9f41fd83eb59fcea2044322dca965ce1d9945 Mon Sep 17 00:00:00 2001 From: IC Rainbow Date: Tue, 21 Nov 2023 20:10:14 +0200 Subject: [PATCH 3/4] add delay to testDuplicateMessage --- tests/AgentTests/FunctionalAPITests.hs | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/AgentTests/FunctionalAPITests.hs b/tests/AgentTests/FunctionalAPITests.hs index 2774b620a..6a80a6927 100644 --- a/tests/AgentTests/FunctionalAPITests.hs +++ b/tests/AgentTests/FunctionalAPITests.hs @@ -762,6 +762,7 @@ testDuplicateMessage t = do get bob2 =##> \case ("", c, Msg "hello 3") -> c == aliceId; _ -> False disconnectAgentClient alice2 disconnectAgentClient bob2 + threadDelay 1000000 testSkippedMessages :: HasCallStack => ATransport -> IO () testSkippedMessages t = do From 8ac97ed7004d1aba2c7e3cac14174409e1faddb4 Mon Sep 17 00:00:00 2001 From: IC Rainbow Date: Tue, 21 Nov 2023 21:30:53 +0200 Subject: [PATCH 4/4] add retry around removing tmp dir --- tests/AgentTests/FunctionalAPITests.hs | 2 -- tests/Test.hs | 16 +++++++++++++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/tests/AgentTests/FunctionalAPITests.hs b/tests/AgentTests/FunctionalAPITests.hs index 6a80a6927..8c37579fc 100644 --- a/tests/AgentTests/FunctionalAPITests.hs +++ b/tests/AgentTests/FunctionalAPITests.hs @@ -762,7 +762,6 @@ testDuplicateMessage t = do get bob2 =##> \case ("", c, Msg "hello 3") -> c == aliceId; _ -> False disconnectAgentClient alice2 disconnectAgentClient bob2 - threadDelay 1000000 testSkippedMessages :: HasCallStack => ATransport -> IO () testSkippedMessages t = do @@ -1280,7 +1279,6 @@ testAsyncCommandsRestore t = do ("1", _, INV _) <- get alice' pure () disconnectAgentClient alice' - threadDelay 1000000 testAcceptContactAsync :: IO () testAcceptContactAsync = diff --git a/tests/Test.hs b/tests/Test.hs index 6493ba860..926065354 100644 --- a/tests/Test.hs +++ b/tests/Test.hs @@ -1,8 +1,11 @@ {-# LANGUAGE TypeApplications #-} +{-# LANGUAGE NamedFieldPuns #-} import AgentTests (agentTests) import AgentTests.SchemaDump (schemaDumpTest) import CLITests +import Control.Concurrent (threadDelay) +import qualified Control.Exception as E import Control.Logger.Simple import CoreTests.BatchingTests import CoreTests.CryptoFileTests @@ -13,6 +16,8 @@ import CoreTests.RetryIntervalTests import CoreTests.UtilTests import CoreTests.VersionRangeTests import FileDescriptionTests (fileDescriptionTests) +import GHC.IO.Exception (IOException (..)) +import qualified GHC.IO.Exception as IOException import NtfServerTests (ntfServerTests) import RemoteControl (remoteControlTests) import ServerTests @@ -36,7 +41,7 @@ main = do setEnv "APNS_KEY_FILE" "./tests/fixtures/AuthKey_H82WD9K9AQ.p8" hspec . before_ (createDirectoryIfMissing False "tests/tmp") - . after_ (removeDirectoryRecursive "tests/tmp") + . after_ (eventuallyRemove "tests/tmp" 3) $ do describe "Agent SQLite schema dump" schemaDumpTest describe "Core tests" $ do @@ -59,3 +64,12 @@ main = do describe "XFTP agent" xftpAgentTests describe "XRCP" remoteControlTests describe "Server CLIs" cliTests + +eventuallyRemove :: FilePath -> Int -> IO () +eventuallyRemove path retries = case retries of + 0 -> action + n -> action `E.catch` \ioe@IOError {ioe_type, ioe_filename} -> case ioe_type of + IOException.UnsatisfiedConstraints | ioe_filename == Just path -> threadDelay 1000000 >> eventuallyRemove path (n - 1) + _ -> E.throwIO ioe + where + action = removeDirectoryRecursive path