@@ -14,7 +14,7 @@ module Kore.JsonRpc.Server (
1414 JsonRpcHandler (.. ),
1515) where
1616
17- import Control.Concurrent (forkIO , throwTo )
17+ import Control.Concurrent (forkIO , forkOS , throwTo )
1818import Control.Concurrent.STM.TChan (newTChan , readTChan , writeTChan )
1919import Control.Exception (Exception (fromException ), catch , mask , throw )
2020import Control.Monad (forever )
@@ -78,11 +78,14 @@ jsonRpcServer ::
7878 (MonadUnliftIO m , FromRequestCancellable q , ToJSON r ) =>
7979 -- | Connection settings
8080 ServerSettings ->
81+ -- | run workers in bound threads (required if worker below uses
82+ -- foreign calls with thread-local state)
83+ Bool ->
8184 -- | Action to perform on connecting client thread
8285 (Request -> Respond q IO r ) ->
8386 [JsonRpcHandler ] ->
8487 m a
85- jsonRpcServer serverSettings respond handlers =
88+ jsonRpcServer serverSettings runBound respond handlers =
8689 runGeneralTCPServer serverSettings $ \ cl ->
8790 Log. runNoLoggingT $
8891 runJSONRPCT
@@ -93,17 +96,18 @@ jsonRpcServer serverSettings respond handlers =
9396 False
9497 (appSink cl)
9598 (appSource cl)
96- (srv respond handlers)
99+ (srv runBound respond handlers)
97100
98101data JsonRpcHandler = forall e . Exception e => JsonRpcHandler (e -> IO ErrorObj )
99102
100103srv ::
101104 forall m q r .
102105 (MonadLoggerIO m , FromRequestCancellable q , ToJSON r ) =>
106+ Bool ->
103107 (Request -> Respond q IO r ) ->
104108 [JsonRpcHandler ] ->
105109 JSONRPCT m ()
106- srv respond handlers = do
110+ srv runBound respond handlers = do
107111 reqQueue <- liftIO $ atomically newTChan
108112 let mainLoop tid =
109113 let loop =
@@ -170,7 +174,8 @@ srv respond handlers = do
170174 restore (thing a) `catch` catchesHandler a
171175
172176 liftIO $
173- forkIO $
177+ -- workers should run in bound threads (to secure foreign calls) when flagged
178+ (if runBound then forkOS else forkIO) $
174179 forever $
175180 bracketOnReqException
176181 (atomically $ readTChan reqQueue)
0 commit comments