fixed minor possible leak on callService

This commit is contained in:
Dmitry Zuikov 2023-10-29 07:46:15 +03:00
parent 408cfb2c51
commit eab3175d52
4 changed files with 21 additions and 13 deletions

View File

@ -158,6 +158,7 @@ instance ( Serialise (PeerCredentials e)
where
bs = LBS.toStrict $ serialise c
-- FIXME: move-thouse-instances-to-appropriate-place-ASAP
instance Pretty (AsBase58 Sign.PublicKey) where
pretty (AsBase58 pk) = pretty $ B8.unpack $ toBase58 (Crypto.encode pk)

View File

@ -42,6 +42,8 @@ import Crypto.Saltine.Core.Sign qualified as Sign
import Crypto.Saltine.Core.Box qualified as Encrypt
-- FIXME: move-to-types-crypto-ASAP
type instance Encryption L4Proto = HBS2Basic
type instance Encryption UNIX = HBS2Basic

View File

@ -27,7 +27,7 @@ import System.Random (randomIO)
import Data.Word
import Data.HashMap.Strict (HashMap)
import Data.HashMap.Strict qualified as HashMap
import Control.Exception (bracket_)
type family Input a :: Type
type family Output a :: Type
@ -243,21 +243,23 @@ callService caller input = do
resp <- UIO.newTQueueIO
atomically $ do
let addWaiter = atomically $ do
UIO.modifyTVar (callWaiters caller) (HashMap.insert (reqNum req) resp)
UIO.writeTQueue (callInQ caller) req
let removeWaiter = atomically $
UIO.modifyTVar (callWaiters caller) (HashMap.delete (reqNum req))
liftIO $ bracket_ addWaiter removeWaiter $ do
msg <- atomically $ UIO.readTQueue resp
case msg of
ServiceResponse _ (Right bs) -> do
ServiceResponse _ (Right bs) ->
case deserialiseOrFail @(Output method) bs of
Left _ -> pure (Left ErrorInvalidResponse)
Right x -> pure (Right x)
ServiceResponse _ (Left wtf) -> pure (Left wtf)
_ -> pure (Left ErrorInvalidResponse)

View File

@ -46,6 +46,9 @@ import GHC.Generics
import Data.Time.Clock (NominalDiffTime(..))
import Codec.Serialise
import UnliftIO
import Control.Monad.IO.Unlift
none :: forall m . Monad m => m ()
none = pure ()