@@ -52,10 +52,11 @@ import Data.Maybe (catMaybes)
5252import Data.MessagePack
5353import Data.Traversable (forM )
5454import System.Log.Logger
55- import Text.PrettyPrint.ANSI.Leijen (Doc )
55+ import Text.PrettyPrint.ANSI.Leijen (Doc , (<+>) , Pretty ( .. ), text )
5656import UnliftIO.Exception (SomeException , try )
57- import UnliftIO.Async (Async , async )
57+ import UnliftIO.Async (Async , async , race )
5858import UnliftIO.STM
59+ import UnliftIO.Concurrent (threadDelay )
5960
6061import Prelude
6162
@@ -301,6 +302,14 @@ registerStatefulFunctionality (Plugin { environment = env, exports = fs }) = do
301302 Left e -> return . Left $ show (e :: SomeException )
302303 Right res -> return $ Right res
303304
305+ timeoutAndLog :: Word -> FunctionName -> Neovim anyEnv String
306+ timeoutAndLog seconds functionName = do
307+ threadDelay (fromIntegral seconds * 1000 * 1000 )
308+ return . show $
309+ pretty functionName <+> text " has been aborted after"
310+ <+> text (show seconds) <+> text " seconds"
311+
312+
304313 listeningThread :: TQueue SomeMessage
305314 -> TVar (Map FunctionName ([Object ] -> Neovim env Object ))
306315 -> Neovim env ()
@@ -309,13 +318,24 @@ registerStatefulFunctionality (Plugin { environment = env, exports = fs }) = do
309318
310319 forM_ (fromMessage msg) $ \ req@ Request {.. } -> do
311320 route' <- liftIO $ readTVarIO route
312- forM_ (Map. lookup reqMethod route') $ \ f ->
313- respond req =<< executeFunction f reqArgs
321+ forM_ (Map. lookup reqMethod route') $ \ f -> do
322+ respond req . either Left id =<< race
323+ (timeoutAndLog 10 reqMethod)
324+ (executeFunction f reqArgs)
314325
315326 forM_ (fromMessage msg) $ \ Notification {.. } -> do
316327 route' <- liftIO $ readTVarIO route
317328 forM_ (Map. lookup notMethod route') $ \ f ->
318- void $ executeFunction f notArgs
329+ void . async $ do
330+ result <- either Left id <$> race
331+ (timeoutAndLog 600 notMethod)
332+ (executeFunction f notArgs)
333+ case result of
334+ Left message ->
335+ nvim_err_writeln' message
336+ Right _ ->
337+ return ()
338+
319339
320340 listeningThread q route
321341
0 commit comments