Andrea Vezzosi pushed to branch wip/romes/async-rpc at Glasgow Haskell Compiler / GHC Commits: 9a27d7f2 by Andrea Vezzosi at 2026-10-21T14:49:34+02:00 fixup! interp: spawn fillPipe thread, writePipe now safely interruptible - - - - - 1 changed file: - libraries/ghci/GHCi/Message/Async.hs Changes: ===================================== libraries/ghci/GHCi/Message/Async.hs ===================================== @@ -95,7 +95,13 @@ mkPipeFromHandles pipeRead pipeWrite = do -- | Make a 'Pipe' from a reader function and a writer function. -- The pipe should be closed with 'closePipe', which will call 'pipeCloseRead' and 'pipeCloseWrite'. -mkPipeFromContinuations :: IO ByteString -> (B.Builder -> IO ()) +-- +-- Arguments 'getSome' and 'putAll' should throw an `IOException` when communication fails +-- or is no longer possible. +mkPipeFromContinuations :: IO ByteString + -- ^ getSome + -> (B.Builder -> IO ()) + -- ^ putAll -> IO () -- ^ pipeCloseRead -> IO () @@ -456,7 +462,7 @@ drainPipe p unmask = catch (unmask $ loop `finally` pipeCloseRead p) replyToPend am = pipeAsyncMngr p --- | "Fill"s write end of the Pipe with queues messages. +-- | "Fill"s write end of the Pipe with queued messages. -- -- Because Pipe is written to from this thread rather than 'writePipe', calls to -- the latter can be interrupted without compromising the protocol. @@ -466,11 +472,28 @@ 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 ()) + success <- handle (\ e -> atomically (putTMVar var (Left e)) >> rethrowIfPipeFatal e >> pure False) $ do + unmask $ withLock p (putAll p . execPut . put $ msg) + pure True + when success $ atomically $ putTMVar var (Right ()) loop + -- `msg` itself might contain exceptions that are only triggered during + -- serialiation. + -- The original sender needs to be able to react to those by sending their own + -- error reply, so we only exit the loop for: + -- - Async exceptions: to respect KillThread etc.. + -- - IOException: these indicate something is wrong with the write end + -- (ideally we'd be more specific but we abstracted over the + -- communication channel). + -- - PipeWriteClosedException: how closePipe shuts down the fillPipe thread. + -- + -- Other exceptions are only rethrown in the original sender thread by 'writePipe'. + rethrowIfPipeFatal e + | Just SomeAsyncException{} <- fromException e = throwIO e + | Just (_ :: IOException) <- fromException e = throwIO e + | Just PipeWriteClosedException <- fromException e = throwIO e + | otherwise = pure () am = pipeAsyncMngr p failPendingWrites :: Pipe -> SomeException -> STM () View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/9a27d7f2611984e42ba95ad265a4c5a4... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/9a27d7f2611984e42ba95ad265a4c5a4... 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