| ... |
... |
@@ -77,6 +77,10 @@ data Pipe = Pipe |
|
77
|
77
|
, pipeReadClosed :: !(TVar Bool)
|
|
78
|
78
|
-- ^ After the pipe is closed, messages can no longer be received from the
|
|
79
|
79
|
-- other end, but the 'AsyncManager' queues might not be empty yet.
|
|
|
80
|
+ , pipeWriteClosed :: !(TVar Bool)
|
|
|
81
|
+ -- ^ After the write end of the pipe is closed we can no longer submit
|
|
|
82
|
+ -- requests or respond to ones we receive, but we might still receive
|
|
|
83
|
+ -- replies.
|
|
80
|
84
|
}
|
|
81
|
85
|
|
|
82
|
86
|
-- | Make a 'Pipe' from a 'Handle' to read and a 'Handle' to write.
|
| ... |
... |
@@ -91,7 +95,13 @@ mkPipeFromHandles pipeRead pipeWrite = do |
|
91
|
95
|
|
|
92
|
96
|
-- | Make a 'Pipe' from a reader function and a writer function.
|
|
93
|
97
|
-- The pipe should be closed with 'closePipe', which will call 'pipeCloseRead' and 'pipeCloseWrite'.
|
|
94
|
|
-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
|
|
95
|
105
|
-> IO ()
|
|
96
|
106
|
-- ^ pipeCloseRead
|
|
97
|
107
|
-> IO ()
|
| ... |
... |
@@ -101,6 +111,7 @@ mkPipeFromContinuations getSome putAll pipeCloseRead pipeCloseWrite = mdo |
|
101
|
111
|
pipeLeftovers <- newIORef Nothing
|
|
102
|
112
|
pipeLock <- newMVar ()
|
|
103
|
113
|
pipeReadClosed <- newTVarIO False
|
|
|
114
|
+ pipeWriteClosed <- newTVarIO False
|
|
104
|
115
|
mdo
|
|
105
|
116
|
pipeAsyncMngr <- newAsyncManager p
|
|
106
|
117
|
let p = Pipe { .. }
|
| ... |
... |
@@ -114,20 +125,34 @@ mkPipeFromContinuations getSome putAll pipeCloseRead pipeCloseWrite = mdo |
|
114
|
125
|
-- write. Only then do we stop the drain thread, which closes the read end
|
|
115
|
126
|
-- itself once its read has finished. On Windows its read can't be
|
|
116
|
127
|
-- interrupted, so this waits for the other side to close its write end too.
|
|
|
128
|
+-- Finally we stop the write thread, which could be blocked waiting for more to write.
|
|
117
|
129
|
closePipe :: Pipe -> IO ()
|
|
118
|
130
|
closePipe p@Pipe{..} = do
|
|
119
|
131
|
readAlreadyClosed <- atomically $ do
|
|
120
|
132
|
closed <- readTVar pipeReadClosed
|
|
121
|
133
|
failPendingReplies p (toException PipeReadClosedException)
|
|
122
|
134
|
pure closed
|
|
123
|
|
- withLock p pipeCloseWrite `finally`
|
|
124
|
|
- unless readAlreadyClosed
|
|
125
|
|
- (throwTo (asyncDrainTid pipeAsyncMngr) PipeReadClosedException)
|
|
|
135
|
+
|
|
|
136
|
+ writeAlreadyClosed <- atomically $ do
|
|
|
137
|
+ closed <- readTVar pipeWriteClosed
|
|
|
138
|
+ failPendingWrites p (toException PipeWriteClosedException)
|
|
|
139
|
+ pure closed
|
|
|
140
|
+
|
|
|
141
|
+ unless writeAlreadyClosed (withLock p pipeCloseWrite)
|
|
|
142
|
+ `finally` do
|
|
|
143
|
+ unless readAlreadyClosed
|
|
|
144
|
+ (throwTo (asyncDrainTid pipeAsyncMngr) PipeReadClosedException)
|
|
|
145
|
+ unless writeAlreadyClosed
|
|
|
146
|
+ (throwTo (asyncFillTid pipeAsyncMngr) PipeWriteClosedException)
|
|
126
|
147
|
|
|
127
|
148
|
-- | We've closed the pipe read end on this process
|
|
128
|
149
|
data PipeReadClosedException = PipeReadClosedException deriving Show
|
|
129
|
150
|
instance Exception PipeReadClosedException
|
|
130
|
151
|
|
|
|
152
|
+-- | We've closed the pipe write end on this process
|
|
|
153
|
+data PipeWriteClosedException = PipeWriteClosedException deriving Show
|
|
|
154
|
+instance Exception PipeWriteClosedException
|
|
|
155
|
+
|
|
131
|
156
|
--------------------------------------------------------------------------------
|
|
132
|
157
|
-- ** Pipe thread-safe read/write
|
|
133
|
158
|
--------------------------------------------------------------------------------
|
| ... |
... |
@@ -135,7 +160,14 @@ instance Exception PipeReadClosedException |
|
135
|
160
|
-- and identify whether they are requests or replies.
|
|
136
|
161
|
|
|
137
|
162
|
writePipe :: Pipe -> IddMsg -> IO ()
|
|
138
|
|
-writePipe p@Pipe{..} = withLock p . putAll . execPut . put
|
|
|
163
|
+writePipe Pipe{..} msg = do
|
|
|
164
|
+ var <- newEmptyTMVarIO
|
|
|
165
|
+ atomically $ do
|
|
|
166
|
+ readTVar pipeWriteClosed >>= \case
|
|
|
167
|
+ True -> throwSTM PipeWriteClosedException
|
|
|
168
|
+ False -> pure ()
|
|
|
169
|
+ writeTQueue (writeQueue pipeAsyncMngr) (msg, var)
|
|
|
170
|
+ either throwIO pure =<< atomically (takeTMVar var)
|
|
139
|
171
|
|
|
140
|
172
|
-- | Only the 'drainPipe' thread should read from the pipe directly. Other
|
|
141
|
173
|
-- functions should wait for the matching reply to be delivered asynchronously.
|
| ... |
... |
@@ -229,6 +261,10 @@ handleAsyncReq pipe getit mii k = mask $ \restore -> do |
|
229
|
261
|
|
|
230
|
262
|
-- Block waiting for request
|
|
231
|
263
|
mbreq <- atomically $ do
|
|
|
264
|
+ readTVar (pipeWriteClosed pipe) >>= \case
|
|
|
265
|
+ -- We will not be able to write the reply, so no point in continuiting.
|
|
|
266
|
+ True -> throwSTM PipeWriteClosedException
|
|
|
267
|
+ False -> pure ()
|
|
232
|
268
|
isClosed <- readTVar (pipeReadClosed pipe)
|
|
233
|
269
|
taken <- case mii of
|
|
234
|
270
|
Nothing -> tryReadTQueue $ requestsQueue am
|
| ... |
... |
@@ -320,7 +356,10 @@ data AsyncManager = AsyncManager |
|
320
|
356
|
, mailboxQueues :: !(TVar (IntMap (TQueue (CorrelationId, LB.ByteString))))
|
|
321
|
357
|
-- ^ Requests for a specific 'InboxId' that can be read using
|
|
322
|
358
|
-- @'handleAsyncReq' p get (Just <mailboxid>)@.
|
|
|
359
|
+ , writeQueue :: !(TQueue (IddMsg, TMVar (Either SomeException ())))
|
|
|
360
|
+ -- ^ Messages to write on the pipe.
|
|
323
|
361
|
, asyncDrainTid :: !ThreadId
|
|
|
362
|
+ , asyncFillTid :: !ThreadId
|
|
324
|
363
|
, asyncFreshId :: !(IORef Int)
|
|
325
|
364
|
}
|
|
326
|
365
|
|
| ... |
... |
@@ -363,11 +402,16 @@ newAsyncManager pipe = do |
|
363
|
402
|
pendingReply <- newTVarIO IntMap.empty
|
|
364
|
403
|
requestsQueue <- newTQueueIO
|
|
365
|
404
|
mailboxQueues <- newTVarIO IntMap.empty
|
|
|
405
|
+ writeQueue <- newTQueueIO
|
|
366
|
406
|
|
|
367
|
407
|
-- Fork thread to drain read end.
|
|
368
|
408
|
-- (cleaned up on 'closePipe')
|
|
369
|
409
|
asyncDrainTid <- forkIOWithUnmask (drainPipe pipe)
|
|
370
|
410
|
|
|
|
411
|
+ -- Fork thread to fill write end.
|
|
|
412
|
+ -- (cleaned up on 'closePipe')
|
|
|
413
|
+ asyncFillTid <- forkIOWithUnmask (fillPipe pipe)
|
|
|
414
|
+
|
|
371
|
415
|
let am = AsyncManager{..}
|
|
372
|
416
|
pure am
|
|
373
|
417
|
|
| ... |
... |
@@ -417,6 +461,47 @@ drainPipe p unmask = catch (unmask $ loop `finally` pipeCloseRead p) replyToPend |
|
417
|
461
|
|
|
418
|
462
|
am = pipeAsyncMngr p
|
|
419
|
463
|
|
|
|
464
|
+
|
|
|
465
|
+-- | "Fill"s write end of the Pipe with queued messages.
|
|
|
466
|
+--
|
|
|
467
|
+-- Because Pipe is written to from this thread rather than 'writePipe', calls to
|
|
|
468
|
+-- the latter can be interrupted without compromising the protocol.
|
|
|
469
|
+--
|
|
|
470
|
+-- Closes the Pipe write end when stopping, notifying any messages still queued to write.
|
|
|
471
|
+fillPipe :: Pipe -> (forall x. IO x -> IO x) -> IO ()
|
|
|
472
|
+fillPipe p unmask = catch (loop `finally` pipeCloseWrite p) (atomically . failPendingWrites p) where
|
|
|
473
|
+ loop = do
|
|
|
474
|
+ (msg,var) <- atomically $ readTQueue (writeQueue am)
|
|
|
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 ())
|
|
|
479
|
+ loop
|
|
|
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 ()
|
|
|
497
|
+ am = pipeAsyncMngr p
|
|
|
498
|
+
|
|
|
499
|
+failPendingWrites :: Pipe -> SomeException -> STM ()
|
|
|
500
|
+failPendingWrites p e = do
|
|
|
501
|
+ writeTVar (pipeWriteClosed p) True
|
|
|
502
|
+ writes <- flushTQueue (writeQueue (pipeAsyncMngr p))
|
|
|
503
|
+ mapM_ (`writeTMVar` Left e) (map snd writes)
|
|
|
504
|
+
|
|
420
|
505
|
failPendingReplies :: Pipe -> SomeException -> STM ()
|
|
421
|
506
|
failPendingReplies p e = do
|
|
422
|
507
|
-- reqs are only added if pipe not closed;
|