| ... |
... |
@@ -95,7 +95,13 @@ mkPipeFromHandles pipeRead pipeWrite = do |
|
95
|
95
|
|
|
96
|
96
|
-- | Make a 'Pipe' from a reader function and a writer function.
|
|
97
|
97
|
-- The pipe should be closed with 'closePipe', which will call 'pipeCloseRead' and 'pipeCloseWrite'.
|
|
98
|
|
-mkPipeFromContinuations :: IO ByteString -> (B.Builder -> IO ())
|
|
|
98
|
+--
|
|
|
99
|
+-- Arguments 'getSome' and 'putAll' should throw an `IOException` when communication fails
|
|
|
100
|
+-- or is no longer possible.
|
|
|
101
|
+mkPipeFromContinuations :: IO ByteString
|
|
|
102
|
+ -- ^ getSome
|
|
|
103
|
+ -> (B.Builder -> IO ())
|
|
|
104
|
+ -- ^ putAll
|
|
99
|
105
|
-> IO ()
|
|
100
|
106
|
-- ^ pipeCloseRead
|
|
101
|
107
|
-> IO ()
|
| ... |
... |
@@ -456,7 +462,7 @@ drainPipe p unmask = catch (unmask $ loop `finally` pipeCloseRead p) replyToPend |
|
456
|
462
|
am = pipeAsyncMngr p
|
|
457
|
463
|
|
|
458
|
464
|
|
|
459
|
|
--- | "Fill"s write end of the Pipe with queues messages.
|
|
|
465
|
+-- | "Fill"s write end of the Pipe with queued messages.
|
|
460
|
466
|
--
|
|
461
|
467
|
-- Because Pipe is written to from this thread rather than 'writePipe', calls to
|
|
462
|
468
|
-- the latter can be interrupted without compromising the protocol.
|
| ... |
... |
@@ -466,11 +472,28 @@ fillPipe :: Pipe -> (forall x. IO x -> IO x) -> IO () |
|
466
|
472
|
fillPipe p unmask = catch (loop `finally` pipeCloseWrite p) (atomically . failPendingWrites p) where
|
|
467
|
473
|
loop = do
|
|
468
|
474
|
(msg,var) <- atomically $ readTQueue (writeQueue am)
|
|
469
|
|
- unmask (withLock p (putAll p . execPut . put $ msg))
|
|
470
|
|
- `catch` \ e -> atomically (putTMVar var (Left e)) >> throwIO e
|
|
471
|
|
- atomically $ putTMVar var (Right ())
|
|
|
475
|
+ success <- handle (\ e -> atomically (putTMVar var (Left e)) >> rethrowIfPipeFatal e >> pure False) $ do
|
|
|
476
|
+ unmask $ withLock p (putAll p . execPut . put $ msg)
|
|
|
477
|
+ pure True
|
|
|
478
|
+ when success $ atomically $ putTMVar var (Right ())
|
|
472
|
479
|
loop
|
|
473
|
480
|
|
|
|
481
|
+ -- `msg` itself might contain exceptions that are only triggered during
|
|
|
482
|
+ -- serialiation.
|
|
|
483
|
+ -- The original sender needs to be able to react to those by sending their own
|
|
|
484
|
+ -- error reply, so we only exit the loop for:
|
|
|
485
|
+ -- - Async exceptions: to respect KillThread etc..
|
|
|
486
|
+ -- - IOException: these indicate something is wrong with the write end
|
|
|
487
|
+ -- (ideally we'd be more specific but we abstracted over the
|
|
|
488
|
+ -- communication channel).
|
|
|
489
|
+ -- - PipeWriteClosedException: how closePipe shuts down the fillPipe thread.
|
|
|
490
|
+ --
|
|
|
491
|
+ -- Other exceptions are only rethrown in the original sender thread by 'writePipe'.
|
|
|
492
|
+ rethrowIfPipeFatal e
|
|
|
493
|
+ | Just SomeAsyncException{} <- fromException e = throwIO e
|
|
|
494
|
+ | Just (_ :: IOException) <- fromException e = throwIO e
|
|
|
495
|
+ | Just PipeWriteClosedException <- fromException e = throwIO e
|
|
|
496
|
+ | otherwise = pure ()
|
|
474
|
497
|
am = pipeAsyncMngr p
|
|
475
|
498
|
|
|
476
|
499
|
failPendingWrites :: Pipe -> SomeException -> STM ()
|