From db1cf85e2a48c3363685483ef4f97cfdd9eff6b9 Mon Sep 17 00:00:00 2001 From: "Evgeny @ SimpleX Chat" <259188159+evgeny-simplex@users.noreply.github.com> Date: Fri, 6 Nov 2026 18:56:17 +0000 Subject: [PATCH] ntf server: resume push notifications after APNS errors (#2020) * ntf server: resume push notifications after APNS errors * ntf server: fix push delivery test assertion --- src/Simplex/Messaging/Notifications/Server.hs | 5 ++- .../Notifications/Server/Push/APNS.hs | 2 +- tests/NtfServerTests.hs | 36 +++++++++++++++++++ 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/src/Simplex/Messaging/Notifications/Server.hs b/src/Simplex/Messaging/Notifications/Server.hs index 735774394..1d3c315b4 100644 --- a/src/Simplex/Messaging/Notifications/Server.hs +++ b/src/Simplex/Messaging/Notifications/Server.hs @@ -677,7 +677,7 @@ runPushWorker s srvHost_ isOwn q = forever $ do (tkn@NtfTknRec {ntfTknId, token = t@(DeviceToken pp _), tknStatus}, ntf) <- atomically (readTBQueue q) liftIO $ logDebug $ "sending push notification to " <> T.pack (show pp) st <- asks store - case ntf of + r <- tryAny $ case ntf of PNVerification _ -> liftIO (deliverNotification st pp tkn ntf) >>= \case Right _ -> do @@ -700,6 +700,9 @@ runPushWorker s srvHost_ isOwn q = forever $ do Right () -> do incNtfStatT t ntfDelivered when isOwn $ liftIO $ mapM_ (`incServerStat` ntfDeliveredOwn stats) srvHost_ + case r of + Right () -> pure () + Left e -> logError $ "push notification error (" <> tshow pp <> ", " <> tshow ntfTknId <> "): " <> tshow e where checkActiveTkn :: NtfTknStatus -> M () -> M () checkActiveTkn status action diff --git a/src/Simplex/Messaging/Notifications/Server/Push/APNS.hs b/src/Simplex/Messaging/Notifications/Server/Push/APNS.hs index d5495239d..bbb0dd13e 100644 --- a/src/Simplex/Messaging/Notifications/Server/Push/APNS.hs +++ b/src/Simplex/Messaging/Notifications/Server/Push/APNS.hs @@ -232,9 +232,9 @@ data APNSPushClient = APNSPushClient createAPNSPushClient :: HostName -> APNSPushClientConfig -> IO APNSPushClient createAPNSPushClient apnsHost apnsCfg@APNSPushClientConfig {authKeyFileEnv, authKeyAlg, authKeyIdEnv, appTeamId} = do https2Client <- newTVarIO Nothing - connectHTTPS2 apnsHost apnsCfg https2Client >>= either (throwIO . userError . show) (\_ -> pure ()) privateKey <- readECPrivateKey =<< getEnv authKeyFileEnv authKeyId <- T.pack <$> getEnv authKeyIdEnv + connectHTTPS2 apnsHost apnsCfg https2Client >>= either (throwIO . userError . show) (\_ -> pure ()) let jwtHeader = JWTHeader {alg = authKeyAlg, kid = authKeyId} jwtToken <- newTVarIO =<< mkApnsJWTToken appTeamId jwtHeader privateKey nonceDrg <- C.newRandom diff --git a/tests/NtfServerTests.hs b/tests/NtfServerTests.hs index 962d199a2..951dcd8d1 100644 --- a/tests/NtfServerTests.hs +++ b/tests/NtfServerTests.hs @@ -23,6 +23,7 @@ import Data.ByteString.Char8 (ByteString) import Data.List (isInfixOf) import Data.List.NonEmpty (NonEmpty (..)) import qualified Data.List.NonEmpty as L +import Data.Maybe (isJust) import Data.Text.Encoding (encodeUtf8) import qualified Database.PostgreSQL.Simple as PSQL import NtfClient @@ -40,6 +41,7 @@ import ServerTests pattern Resp, ) import qualified Simplex.Messaging.Agent.Protocol as AP +import Simplex.Messaging.Agent.RetryInterval (RetryInterval (..)) import Simplex.Messaging.Agent.Store.Postgres.Options (DBOpts (..)) import Simplex.Messaging.Client.Agent (SMPClientAgentConfig (..)) import qualified Simplex.Messaging.Crypto as C @@ -67,6 +69,9 @@ ntfServerTests ps@(t, _) = do it "rejects APNS test provider unless enabled" $ testApnsTestProviderRejected t describe "APNS client" $ do it "should validate APNS server certificate" testApnsServerCertificate + describe "APNS push delivery" $ do + it "resumes sending notifications after the push provider is unreachable" $ testPushWorkerResumesAfterOutage t + it "does not connect to the push provider when the key file is not configured" testNoConnectWithoutKeyFile describe "Database pool" $ do it "answers SNEW with one database connection" $ testSNEWOneDBConnection t describe "Notification subscriptions (NKEY)" $ testNotificationSubscription ps createNtfQueueNKEY @@ -115,6 +120,37 @@ testApnsServerCertificate = withAPNSMockServer $ \_ -> do NtfServerConfig {apnsConfig} = ntfServerCfg certError s (e :: IOError) = s `isInfixOf` show e +testPushWorkerResumesAfterOutage :: HasTestEnv => ASrvTransport -> Expectation +testPushWorkerResumesAfterOutage (ATransport (t :: TProxy c 'TServer)) = do + g <- C.newRandom + (tknPub, tknKey) <- atomically $ C.generateAuthKeyPair C.SEd25519 g + (dhPub, _dhPriv :: C.PrivateKeyX25519) <- atomically $ C.generateKeyPair g + let tkn = DeviceToken PPApnsTest "abcd" + newTkn = NewNtfTkn tkn tknPub dhPub + withNtfServerCfg ntfCfg $ \_ -> + testNtfClient $ \(nh :: THandleNTF c 'TClient) -> do + RespNtf "1" NoEntity (NRTknId _ _) <- signSendRecvNtf nh tknKey ("1", NoEntity, TNEW newTkn) + threadDelay 1000000 + withAPNSMockServer $ \apns -> do + RespNtf "2" NoEntity (NRTknId _ _) <- signSendRecvNtf nh tknKey ("2", NoEntity, TNEW newTkn) + delivered <- timeout 5000000 $ getMockNotification apns tkn + isJust delivered `shouldBe` True + where + NtfServerConfig {apnsConfig} = ntfServerCfg + ntfCfg = + ntfServerCfg + { transports = [(ntfTestPort, ATransport t, False)], + apnsConfig = apnsConfig {reconnectInterval = RetryInterval {initialInterval = 100000, increaseAfter = 0, maxInterval = 200000}} + } + +testNoConnectWithoutKeyFile :: HasTestEnv => Expectation +testNoConnectWithoutKeyFile = + createAPNSPushClient "localhost" apnsCfgMissingKey `shouldThrow` keyFileError + where + NtfServerConfig {apnsConfig} = ntfServerCfg + apnsCfgMissingKey = apnsConfig {authKeyFileEnv = "APNS_KEY_FILE_MISSING"} + keyFileError (e :: IOError) = "APNS_KEY_FILE_MISSING" `isInfixOf` show e + testSNEWOneDBConnection :: HasTestEnv => ASrvTransport -> Expectation testSNEWOneDBConnection (ATransport (t :: TProxy c 'TServer)) = do g <- C.newRandom