Andrea Vezzosi pushed to branch wip/romes/async-rpc at Glasgow Haskell Compiler / GHC Commits: 48d9f5a1 by Andrea Vezzosi at 2026-10-20T11:58:19+02:00 fixup! ignore PipeReadClosedException on Shutdown. - - - - - 1 changed file: - libraries/ghci/GHCi/Message/Async.hs Changes: ===================================== libraries/ghci/GHCi/Message/Async.hs ===================================== @@ -128,7 +128,10 @@ closePipe p@Pipe{..} = do failPendingReplies p (toException PipeReadClosedException) pure closed - writeAlreadyClosed <- readTVarIO pipeWriteClosed + writeAlreadyClosed <- atomically $ do + closed <- readTVar pipeWriteClosed + failPendingWrites p (toException PipeWriteClosedException) + pure closed unless writeAlreadyClosed (withLock p pipeCloseWrite) `finally` do @@ -152,11 +155,14 @@ instance Exception PipeWriteClosedException -- and identify whether they are requests or replies. writePipe :: Pipe -> IddMsg -> IO () -writePipe Pipe{..} msg = atomically $ do - readTVar pipeWriteClosed >>= \case - True -> throwSTM PipeWriteClosedException - False -> pure () - writeTQueue (writeQueue pipeAsyncMngr) msg +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. @@ -345,7 +351,7 @@ 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) + , writeQueue :: !(TQueue (IddMsg, TMVar (Either SomeException ()))) -- ^ Messages to write on the pipe. , asyncDrainTid :: !ThreadId , asyncFillTid :: !ThreadId @@ -453,26 +459,26 @@ drainPipe p unmask = catch (unmask $ loop `finally` pipeCloseRead p) replyToPend -- | "Fill"s write end of the Pipe with queues messages. -- --- Closes the Pipe write end when stopping, notifying any requests still queued to write. +-- 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 = unmask $ catch (loop `finally` pipeCloseWrite p) failPendingWrites where +fillPipe p unmask = catch (loop `finally` pipeCloseWrite p) (atomically . failPendingWrites p) where loop = do - msg <- atomically $ readTQueue (writeQueue am) - withLock p . putAll p . execPut . put $ msg + (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 :: SomeException -> IO () - failPendingWrites e = atomically $ do - writeTVar (pipeWriteClosed p) True - - writes <- flushTQueue (writeQueue am) - let uqs = [uq | Request uq _ _ <- writes] - pendings <- readTVar (pendingReply (pipeAsyncMngr p)) - let tmvars = mapMaybe (`IntMap.lookup` pendings) uqs - writeTVar (pendingReply (pipeAsyncMngr p)) $! (foldl' (flip IntMap.delete) pendings uqs) - mapM_ (`writeTMVar` Left e) tmvars +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 View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/48d9f5a1ed4ebda8c59133a7c259b780... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/48d9f5a1ed4ebda8c59133a7c259b780... 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