smp-server: hold command slot until completion

This commit is contained in:
shum
2026-09-30 18:18:26 +00:00
parent ad694af988
commit c1c4f44926
2 changed files with 42 additions and 3 deletions
+7 -3
View File
@@ -1476,11 +1476,15 @@ client
-- Run a slow command on a thread
forkCmd :: (ServerConfig s -> Int) -> CorrId -> EntityId -> M s BrokerMsg -> M s (Maybe a)
forkCmd concurrency corrId entId cmdAction = do
bracket_ wait signal . forkClient clnt (B.unpack $ "client $" <> encode sessionId <> " cmd") $
-- commands MUST be processed under a reasonable timeout or the client would halt
cmdAction >>= \t -> atomically $ writeTBQueue sndQ ([(corrId, entId, t)], [])
-- the forked thread releases the slot when the command completes, the caller only if the fork failed
mask $ \restore -> do
wait
forkClient clnt (B.unpack $ "client $" <> encode sessionId <> " cmd") (restore cmd `finally` signal)
`onException` signal
pure Nothing
where
-- commands MUST be processed under a reasonable timeout or the client would halt
cmd = cmdAction >>= \t -> atomically $ writeTBQueue sndQ ([(corrId, entId, t)], [])
wait = do
limit <- asks (concurrency . config)
atomically $ do