Andrea Vezzosi pushed to branch wip/romes/async-rpc at Glasgow Haskell Compiler / GHC Commits: f161704f by Andrea Vezzosi at 2026-11-30T13:59:56+01:00 interp: dereference RemoteRef's in main iserv thread. RunTH and RunModFinalizers handlers run in forked threads, so potentially racing with free requests on the RemoteRefs they take. The RemoteRef's are now dereferenced before forking off the handlers, so the race is avoided: any later free request can run without conflicts. - - - - - b2f8d4cf by Andrea Vezzosi at 2026-11-30T16:01:47+01:00 iserv: relinquish ownership of ModFinalizer on send The `ref` in `AddModFinalizer ref` requests are to be considered owned by the receiver as soon as the message might have been sent. Only exceptions prior to that trigger a local free of the ref. If the message does not arrive then communication has broken down and `iserv` will be shut down in short time. - - - - - 3 changed files: - libraries/ghci/GHCi/Message.hs - libraries/ghci/GHCi/Server.hs - libraries/ghci/GHCi/TH.hs Changes: ===================================== libraries/ghci/GHCi/Message.hs ===================================== @@ -28,7 +28,7 @@ module GHCi.Message , getMessage, putMessage, getTHMessage, putTHMessage , Pipe(..), mkPipeFromHandles, mkPipeFromContinuations, closePipe, isPipeOpen - , remoteCall, remoteCallInterleaving, remoteTHCall + , remoteCall, remoteCallInterleaving, remoteTHCall, remoteTHCall' , handleRemoteCall, handleRemoteTHCall, writeReply , RemoteProcessException(..), PipeClosedException(..) , disposeTHMessage @@ -803,6 +803,11 @@ handleRemoteCall pipe hook k = remoteTHCall :: Binary a => Pipe -> InboxId -> THMessage a -> IO a remoteTHCall pipe ii msg = either throwIO (pure . runGet get) =<< remoteAsyncReq pipe (putTHMessage msg) (Just ii) (disposeTHMessage msg) +remoteTHCall' :: Binary a => Pipe -> InboxId -> THMessage a -> IO (IO a) +remoteTHCall' pipe ii msg = + remoteAsyncReqWithResponse pipe (putTHMessage msg) (Just ii) >>= \case + NotWritten e -> throwIO e + x -> pure $ mask_ $ either throwIO (pure . runGet get) =<< getAsyncReqReply (pure ()) (disposeTHMessage msg . fmap (runGet get)) x disposeTHMessage :: THMessage a -> Either SomeException LB.ByteString -> IO () -- TODO: setup THMessage cleanups, even though atm there's none. ===================================== libraries/ghci/GHCi/Server.hs ===================================== @@ -10,6 +10,7 @@ module GHCi.Server where import Prelude +import GHCi.RemoteTypes (localRef) import GHCi.Run import GHCi.Signals import GHCi.TH @@ -82,10 +83,12 @@ servWithCustom verbose hook pipe restore customHandler = do Shutdown -> writeIORef doneRef True >> closingPipe pipe >> reply () %>>= \r -> runWriteReply wr (Right r) ignorePipeClosedException - RunTH ii st q ty loc -> handleAsync wr + RunTH ii rst rq ty loc -> localRef rst %>>= \ st -> localRef rq %>>= \ q -> + handleAsync wr (restore $ wrapRunTH ii (runTH pipe ii st q ty loc)) (setCancel . void . forkIO . killThread) - RunModFinalizers ii st qs -> handleAsync wr + RunModFinalizers ii rst rqs -> localRef rst %>>= \ st -> mapM localRef rqs %>>= \ qs -> + handleAsync wr (restore $ wrapRunTH ii (runModFinalizerRefs pipe ii st qs)) (setCancel . void . forkIO . killThread) _other -> run msg >>= reply %>>= writeReply wr ===================================== libraries/ghci/GHCi/TH.hs ===================================== @@ -96,6 +96,7 @@ import GHCi.Message import GHCi.RemoteTypes import GHC.Serialized +import Control.Monad (join) import Control.Exception import Control.Monad.IO.Class (MonadIO (..)) import Data.Binary @@ -154,7 +155,7 @@ noLoc = TH.Loc "<no file>" "<no package>" "<no module>" (0,0) (0,0) -- | Send a 'THMessage' to GHC and return the result. ghcCmd :: Binary a => THMessage (THResult a) -> GHCiQ a -ghcCmd m = GHCiQ $ \ sRef -> handleTHResult sRef =<< ghcCmdResult sRef m +ghcCmd m = GHCiQ $ \ sRef -> handleTHResult sRef =<< join (ghcCmdResult sRef m) -- | Throws 'GHCiQException' on 'THException'. handleTHResult :: IORef QState -> THResult a -> IO a @@ -165,10 +166,10 @@ handleTHResult sRef r = do THComplete res -> return res -- | Like 'ghcCmd' but returns the 'THResult a' as-is. -ghcCmdResult :: Binary a => IORef QState -> THMessage (THResult a) -> IO (THResult a) +ghcCmdResult :: Binary a => IORef QState -> THMessage (THResult a) -> IO (IO (THResult a)) ghcCmdResult sRef m = do s <- readIORef sRef - remoteTHCall (qsPipe s) (qsInboxId s) m + remoteTHCall' (qsPipe s) (qsInboxId s) m instance MonadIO GHCiQ where @@ -223,9 +224,10 @@ metaHandlersGHCiQ runInIO = TH.forceMetaHandlersArgs $ TH.MetaHandlers { , mAddTempFile = \suffix -> runInIO $ ghcCmd (AddTempFile suffix) , mAddTopDecls = \decls -> runInIO $ ghcCmd (AddTopDecls decls) , mAddForeignFilePath = \lang fp -> runInIO $ ghcCmd (AddForeignFilePath lang fp) - , mAddModFinalizer = \fin -> runInIO $ GHCiQ $ \sRef -> handleTHResult sRef =<< do - bracketOnError (mkRemoteRef fin) freeRemoteRef $ + , mAddModFinalizer = \fin -> runInIO $ GHCiQ $ \sRef -> do + waitReply <- bracketOnError (mkRemoteRef fin) freeRemoteRef $ ghcCmdResult sRef . AddModFinalizer + handleTHResult sRef =<< waitReply , mAddCorePlugin = \str -> runInIO $ ghcCmd (AddCorePlugin str) , mGetQ = runInIO $ do s <- getState @@ -251,12 +253,10 @@ startTH = do -- -- The references must be created on the caller process. runModFinalizerRefs :: Pipe -> InboxId - -> RemoteRef (IORef QState) - -> [RemoteRef (TH.Q ())] + -> (IORef QState) + -> [TH.Q ()] -> IO () -runModFinalizerRefs pipe ii rstate qrefs = do - qs <- mapM localRef qrefs - qstateref <- localRef rstate +runModFinalizerRefs pipe ii qstateref qs = do qstate <- readIORef qstateref qstate' <- newIORef $ qstate { qsInboxId = ii, qsPipe = pipe } _ <- runGHCiQ (runQinGHCiQ $ sequence_ qs) qstate' @@ -266,9 +266,9 @@ runModFinalizerRefs pipe ii rstate qrefs = do runTH :: Pipe -> InboxId - -> RemoteRef (IORef QState) + -> (IORef QState) -- ^ The TH state, created by 'startTH' - -> HValueRef + -> HValue -- ^ The splice to run -> THResultType -- ^ What kind of splice it is @@ -277,25 +277,22 @@ runTH -> IO ByteString -- ^ Returns an (encoded) result that depends on the THResultType -runTH pipe ii rstate rhv ty mb_loc = do - hv <- localRef rhv +runTH pipe ii rstate hv ty mb_loc = do case ty of THExp -> runTHQ pipe ii rstate mb_loc (unsafeCoerce hv :: TH.Q TH.Exp) THPat -> runTHQ pipe ii rstate mb_loc (unsafeCoerce hv :: TH.Q TH.Pat) THType -> runTHQ pipe ii rstate mb_loc (unsafeCoerce hv :: TH.Q TH.Type) THDec -> runTHQ pipe ii rstate mb_loc (unsafeCoerce hv :: TH.Q [TH.Dec]) THAnnWrapper -> do - hv <- unsafeCoerce <$> localRef rhv - case hv :: AnnotationWrapper of + case unsafeCoerce hv :: AnnotationWrapper of AnnotationWrapper thing -> return $! LB.toStrict (runPut (put (toSerialized serializeWithData thing))) -- | Run a Q computation. runTHQ - :: Binary a => Pipe -> InboxId -> RemoteRef (IORef QState) -> Maybe TH.Loc -> TH.Q a + :: Binary a => Pipe -> InboxId -> (IORef QState) -> Maybe TH.Loc -> TH.Q a -> IO ByteString -runTHQ pipe ii rstate mb_loc ghciq = do - qstateref <- localRef rstate +runTHQ pipe ii qstateref mb_loc ghciq = do modifyIORef' qstateref (\qstate -> qstate { qsLocation = mb_loc, qsInboxId = ii, qsPipe = pipe }) r <- runGHCiQ (runQinGHCiQ ghciq) qstateref return $! LB.toStrict (runPut (put r)) View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/5f654f2c46935c76e04f9260d3be261... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/5f654f2c46935c76e04f9260d3be261... You're receiving this email because of your account on gitlab.haskell.org. Manage all notifications: https://gitlab.haskell.org/-/profile/notifications | Help: https://gitlab.haskell.org/help
participants (1)
-
Andrea Vezzosi (@trac-Saizan)