Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not run forever in request worker threads #3726

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions kore-rpc-types/src/Kore/JsonRpc/Server.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ module Kore.JsonRpc.Server (
) where

import Control.Concurrent (forkIO, throwTo)
import Control.Concurrent.STM qualified as GHC
import Control.Concurrent.STM.TChan (newTChan, readTChan, writeTChan)
import Control.Exception (Exception (fromException), catch, mask, throw)
import Control.Monad (forM_, forever)
import Control.Monad (forM, forM_)
import Control.Monad.IO.Class (MonadIO (liftIO))
import Control.Monad.Logger (MonadLoggerIO)
import Control.Monad.Logger qualified as Log
Expand All @@ -32,8 +33,10 @@ import Data.Conduit.Network (ServerSettings, appSink, appSource, runGeneralTCPSe
import Data.Conduit.TMChan (closeTBMChan, sinkTBMChan, sourceTBMChan)
import Data.Maybe (catMaybes)
import Data.Text qualified as Text
import GHC.Conc.Sync qualified as GHC
import Kore.JsonRpc.Types (FromRequestCancellable (isCancel), ReqException (..), rpcJsonConfig)
import Network.JSONRPC hiding (encodeConduit, runJSONRPCT)
import System.IO.Unsafe qualified as IO
import UnliftIO (MonadUnliftIO, atomically, wait, withAsync)

-- Conduit to encode JSON to ByteString.
Expand Down Expand Up @@ -97,6 +100,10 @@ jsonRpcServer serverSettings respond handlers =

data JsonRpcHandler = forall e. Exception e => JsonRpcHandler (e -> Log.LoggingT IO ErrorObj)

{-# NOINLINE workerList #-}
workerList :: GHC.TVar [GHC.ThreadId]
workerList = IO.unsafePerformIO $ GHC.newTVarIO []

srv ::
forall m q r.
(MonadLoggerIO m, FromRequestCancellable q, ToJSON r) =>
Expand All @@ -121,7 +128,17 @@ srv respond handlers = do
liftIO $ atomically $ writeTChan reqQueue req
loop
in loop
spawnWorker reqQueue >>= mainLoop
newWorkerTid <- spawnWorker reqQueue
Log.logInfoN $ "Spawned worker with thread id " <> Text.pack (show newWorkerTid)
atomically $ GHC.modifyTVar workerList (newWorkerTid :)
Log.logInfoN "Status of worker threads: "
workerIds <- atomically $ GHC.readTVar workerList
workerStatuses <- forM workerIds $ \tId -> do
status <- liftIO $ GHC.threadStatus tId
pure $ " " <> show tId <> ": " <> show status
Log.logInfoN . Text.pack . unlines $ workerStatuses

mainLoop newWorkerTid
where
isRequest = \case
Request{} -> True
Expand Down Expand Up @@ -188,7 +205,7 @@ srv respond handlers = do

liftIO $
forkIO $
forever $
id $
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this id is here just to preserve formatting, must be removed if we merge this

bracketOnReqException
(atomically $ readTChan reqQueue)
(withLog . processReq)
10 changes: 1 addition & 9 deletions kore/src/Kore/JsonRpc.hs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ import Kore.JsonRpc.Error
import Kore.JsonRpc.Server (
ErrorObj (..),
JsonRpcHandler (JsonRpcHandler),
Request (getReqId),
Respond,
jsonRpcServer,
)
Expand All @@ -69,7 +68,6 @@ import Kore.Log.DecidePredicateUnknown (
srcLoc,
)
import Kore.Log.InfoExecDepth (ExecDepth (..))
import Kore.Log.InfoJsonRpcProcessRequest (InfoJsonRpcProcessRequest (..))
import Kore.Log.JsonRpc (LogJsonRpcServer (..))
import Kore.Parser (parseKoreModule)
import Kore.Reachability.Claim qualified as Claim
Expand Down Expand Up @@ -757,10 +755,7 @@ runServer port serverState mainModule runSMT Log.LoggerEnv{logAction} = do
flip runLoggingT logFun $
jsonRpcServer
srvSettings
( \req parsed ->
log (InfoJsonRpcProcessRequest (getReqId req) parsed)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we already emit a similar log message on receiving requests in Kore.JsonRpc.Server.srv

>> respond serverState mainModule runSMT parsed
)
(const (respond serverState mainModule runSMT))
[ handleDecidePredicateUnknown
, handleErrorCall
, handleSomeException
Expand All @@ -771,9 +766,6 @@ runServer port serverState mainModule runSMT Log.LoggerEnv{logAction} = do
logFun loc src level msg =
Log.logWith logAction $ LogJsonRpcServer{loc, src, level, msg}

log :: MonadIO m => Log.Entry entry => entry -> m ()
log = Log.logWith $ Log.hoistLogAction liftIO logAction

handleDecidePredicateUnknown :: JsonRpcHandler
handleDecidePredicateUnknown = JsonRpcHandler $ \(err :: DecidePredicateUnknown) ->
pure (backendError SmtSolverError $ externaliseDecidePredicateUnknown err)
Loading