Andrea Vezzosi pushed to branch wip/romes/async-rpc at Glasgow Haskell Compiler / GHC Commits: beb35d96 by Andrea Vezzosi at 2026-10-20T13:56:59+02:00 interp: spawn fillPipe thread, writePipe now safely interruptible Previously an async exception during a call to `writePipe` could have caused a partial write, making any further write impossible to parse from the other end. Now `writePipe` merely pushes the message onto a queue, and the actual write happens in a separate `fillPipe` thread. As a result of this and previous changes, `interpCmd` no longer needs `interruptibleMask` to preserve the state of the protocol. `writePipe` waits for the actual write to complete, and rethrows exceptions raised in the write attempt, if any. - - - - - 2 changed files: - compiler/GHC/Runtime/Interpreter.hs - libraries/ghci/GHCi/Message/Async.hs Changes: ===================================== compiler/GHC/Runtime/Interpreter.hs ===================================== @@ -197,9 +197,7 @@ interpCmd interp msg = case interpInstance interp of InternalInterp -> run msg -- Just run it directly #endif ExternalInterp ext -> withExtInterp ext $ \inst -> - uninterruptibleMask_ $ -- Note [uninterruptibleMask_ and interpCmd] - sendMessage inst msg - + sendMessage inst msg withExtInterp :: ExceptionMonad m => ExtInterp -> (forall d. ExtInterpInstance d -> m a) -> m a withExtInterp ext action = case ext of @@ -213,15 +211,6 @@ withExtInterpStatus ext action = case ext of ExtWasm i -> action $ interpStatus i ExtIServ i -> action (interpStatus i) --- Note [uninterruptibleMask_ and interpCmd] --- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --- If we receive an async exception, such as ^C, while communicating --- with the iserv process then we will be out-of-sync and not be able --- to recover. Thus we use uninterruptibleMask_ during --- communication. A ^C will be delivered to the iserv process (because --- signals get sent to the whole process group) which will interrupt --- the running computation and return an EvalException result. - -- | Grab a lock on the 'IServ' and do something with it. -- Overloaded because this is used from TcM as well as IO. withIServ ===================================== libraries/ghci/GHCi/Message/Async.hs ===================================== @@ -77,6 +77,10 @@ data Pipe = Pipe , pipeReadClosed :: !(TVar Bool) -- ^ After the pipe is closed, messages can no longer be received from the -- other end, but the 'AsyncManager' queues might not be empty yet. + , pipeWriteClosed :: !(TVar Bool) + -- ^ After the write end of the pipe is closed we can no longer submit + -- requests or respond to ones we receive, but we might still receive + -- replies. } -- | Make a 'Pipe' from a 'Handle' to read and a 'Handle' to write. @@ -101,6 +105,7 @@ mkPipeFromContinuations getSome putAll pipeCloseRead pipeCloseWrite = mdo pipeLeftovers <- newIORef Nothing pipeLock <- newMVar () pipeReadClosed <- newTVarIO False + pipeWriteClosed <- newTVarIO False mdo pipeAsyncMngr <- newAsyncManager p let p = Pipe { .. } @@ -114,20 +119,34 @@ mkPipeFromContinuations getSome putAll pipeCloseRead pipeCloseWrite = mdo -- write. Only then do we stop the drain thread, which closes the read end -- itself once its read has finished. On Windows its read can't be -- interrupted, so this waits for the other side to close its write end too. +-- Finally we stop the write thread, which could be blocked waiting for more to write. closePipe :: Pipe -> IO () closePipe p@Pipe{..} = do readAlreadyClosed <- atomically $ do closed <- readTVar pipeReadClosed failPendingReplies p (toException PipeReadClosedException) pure closed - withLock p pipeCloseWrite `finally` - unless readAlreadyClosed - (throwTo (asyncDrainTid pipeAsyncMngr) PipeReadClosedException) + + writeAlreadyClosed <- atomically $ do + closed <- readTVar pipeWriteClosed + failPendingWrites p (toException PipeWriteClosedException) + pure closed + + unless writeAlreadyClosed (withLock p pipeCloseWrite) + `finally` do + unless readAlreadyClosed + (throwTo (asyncDrainTid pipeAsyncMngr) PipeReadClosedException) + unless writeAlreadyClosed + (throwTo (asyncFillTid pipeAsyncMngr) PipeWriteClosedException) -- | We've closed the pipe read end on this process data PipeReadClosedException = PipeReadClosedException deriving Show instance Exception PipeReadClosedException +-- | We've closed the pipe write end on this process +data PipeWriteClosedException = PipeWriteClosedException deriving Show +instance Exception PipeWriteClosedException + -------------------------------------------------------------------------------- -- ** Pipe thread-safe read/write -------------------------------------------------------------------------------- @@ -135,7 +154,14 @@ instance Exception PipeReadClosedException -- and identify whether they are requests or replies. writePipe :: Pipe -> IddMsg -> IO () -writePipe p@Pipe{..} = withLock p . putAll . execPut . put +writePipe Pipe{..} msg = do + var <- newEmptyTMVarIO + atomically $ do + readTVar pipeWriteClosed >>= \case + True -> throwSTM PipeWriteClosedException + False -> pure () + writeTQueue (writeQueue pipeAsyncMngr) (msg, var) + either throwIO pure =<< atomically (takeTMVar var) -- | Only the 'drainPipe' thread should read from the pipe directly. Other -- functions should wait for the matching reply to be delivered asynchronously. @@ -229,6 +255,10 @@ handleAsyncReq pipe getit mii k = mask $ \restore -> do -- Block waiting for request mbreq <- atomically $ do + readTVar (pipeWriteClosed pipe) >>= \case + -- We will not be able to write the reply, so no point in continuiting. + True -> throwSTM PipeWriteClosedException + False -> pure () isClosed <- readTVar (pipeReadClosed pipe) taken <- case mii of Nothing -> tryReadTQueue $ requestsQueue am @@ -320,7 +350,10 @@ data AsyncManager = AsyncManager , mailboxQueues :: !(TVar (IntMap (TQueue (CorrelationId, LB.ByteString)))) -- ^ Requests for a specific 'InboxId' that can be read using -- @'handleAsyncReq' p get (Just <mailboxid>)@. + , writeQueue :: !(TQueue (IddMsg, TMVar (Either SomeException ()))) + -- ^ Messages to write on the pipe. , asyncDrainTid :: !ThreadId + , asyncFillTid :: !ThreadId , asyncFreshId :: !(IORef Int) } @@ -363,11 +396,16 @@ newAsyncManager pipe = do pendingReply <- newTVarIO IntMap.empty requestsQueue <- newTQueueIO mailboxQueues <- newTVarIO IntMap.empty + writeQueue <- newTQueueIO -- Fork thread to drain read end. -- (cleaned up on 'closePipe') asyncDrainTid <- forkIOWithUnmask (drainPipe pipe) + -- Fork thread to fill write end. + -- (cleaned up on 'closePipe') + asyncFillTid <- forkIOWithUnmask (fillPipe pipe) + let am = AsyncManager{..} pure am @@ -417,6 +455,30 @@ drainPipe p unmask = catch (unmask $ loop `finally` pipeCloseRead p) replyToPend am = pipeAsyncMngr p + +-- | "Fill"s write end of the Pipe with queues messages. +-- +-- Because Pipe is written to from this thread rather than 'writePipe', calls to +-- the latter can be interrupted without compromising the protocol. +-- +-- Closes the Pipe write end when stopping, notifying any messages still queued to write. +fillPipe :: Pipe -> (forall x. IO x -> IO x) -> IO () +fillPipe p unmask = catch (loop `finally` pipeCloseWrite p) (atomically . failPendingWrites p) where + loop = do + (msg,var) <- atomically $ readTQueue (writeQueue am) + unmask (withLock p (putAll p . execPut . put $ msg)) + `catch` \ e -> atomically (putTMVar var (Left e)) >> throwIO e + atomically $ putTMVar var (Right ()) + loop + + am = pipeAsyncMngr p + +failPendingWrites :: Pipe -> SomeException -> STM () +failPendingWrites p e = do + writeTVar (pipeWriteClosed p) True + writes <- flushTQueue (writeQueue (pipeAsyncMngr p)) + mapM_ (`writeTMVar` Left e) (map snd writes) + failPendingReplies :: Pipe -> SomeException -> STM () failPendingReplies p e = do -- reqs are only added if pipe not closed; View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/beb35d96d1287ae7f15841043d634667... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/beb35d96d1287ae7f15841043d634667... 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)