| ... |
... |
@@ -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.
|
| ... |
... |
@@ -181,6 +213,8 @@ withLock Pipe{..} = bracket (takeMVar pipeLock) (putMVar pipeLock) . const |
|
181
|
213
|
--------------------------------------------------------------------------------
|
|
182
|
214
|
|
|
183
|
215
|
-- | Send an asynchronous request and block waiting for a matching reply
|
|
|
216
|
+--
|
|
|
217
|
+-- See Note [Exceptions during serialization] about exceptions the 'Put' argument might raise.
|
|
184
|
218
|
remoteAsyncReq :: Pipe -> Put -> (Maybe InboxId) -> IO (Either SomeException LB.ByteString)
|
|
185
|
219
|
remoteAsyncReq pipe msg ii = do
|
|
186
|
220
|
|
| ... |
... |
@@ -222,6 +256,8 @@ remoteAsyncReq pipe msg ii = do |
|
222
|
256
|
-- It must be used exactly once (all requests expect a reply, and you can't
|
|
223
|
257
|
-- reply more than once). Linear types enforce the handler replies in every
|
|
224
|
258
|
-- branch, and if the handler otherwise throws, it is handled by 'handleAsyncReq'.
|
|
|
259
|
+--
|
|
|
260
|
+-- See Note [Exceptions during serialization] about exceptions the 'Put' argument might raise.
|
|
225
|
261
|
handleAsyncReq :: Pipe -> Get a -> (Maybe InboxId) -> (WriteReply Put %1 -> a -> IO r) -> IO r
|
|
226
|
262
|
handleAsyncReq pipe getit mii k = mask $ \restore -> do
|
|
227
|
263
|
let am = pipeAsyncMngr pipe
|
| ... |
... |
@@ -229,6 +265,10 @@ handleAsyncReq pipe getit mii k = mask $ \restore -> do |
|
229
|
265
|
|
|
230
|
266
|
-- Block waiting for request
|
|
231
|
267
|
mbreq <- atomically $ do
|
|
|
268
|
+ readTVar (pipeWriteClosed pipe) >>= \case
|
|
|
269
|
+ -- We will not be able to write the reply, so no point in continuiting.
|
|
|
270
|
+ True -> throwSTM PipeWriteClosedException
|
|
|
271
|
+ False -> pure ()
|
|
232
|
272
|
isClosed <- readTVar (pipeReadClosed pipe)
|
|
233
|
273
|
taken <- case mii of
|
|
234
|
274
|
Nothing -> tryReadTQueue $ requestsQueue am
|
| ... |
... |
@@ -320,7 +360,10 @@ data AsyncManager = AsyncManager |
|
320
|
360
|
, mailboxQueues :: !(TVar (IntMap (TQueue (CorrelationId, LB.ByteString))))
|
|
321
|
361
|
-- ^ Requests for a specific 'InboxId' that can be read using
|
|
322
|
362
|
-- @'handleAsyncReq' p get (Just <mailboxid>)@.
|
|
|
363
|
+ , writeQueue :: !(TQueue (IddMsg, TMVar (Either SomeException ())))
|
|
|
364
|
+ -- ^ Messages to write on the pipe.
|
|
323
|
365
|
, asyncDrainTid :: !ThreadId
|
|
|
366
|
+ , asyncFillTid :: !ThreadId
|
|
324
|
367
|
, asyncFreshId :: !(IORef Int)
|
|
325
|
368
|
}
|
|
326
|
369
|
|
| ... |
... |
@@ -363,11 +406,16 @@ newAsyncManager pipe = do |
|
363
|
406
|
pendingReply <- newTVarIO IntMap.empty
|
|
364
|
407
|
requestsQueue <- newTQueueIO
|
|
365
|
408
|
mailboxQueues <- newTVarIO IntMap.empty
|
|
|
409
|
+ writeQueue <- newTQueueIO
|
|
366
|
410
|
|
|
367
|
411
|
-- Fork thread to drain read end.
|
|
368
|
412
|
-- (cleaned up on 'closePipe')
|
|
369
|
413
|
asyncDrainTid <- forkIOWithUnmask (drainPipe pipe)
|
|
370
|
414
|
|
|
|
415
|
+ -- Fork thread to fill write end.
|
|
|
416
|
+ -- (cleaned up on 'closePipe')
|
|
|
417
|
+ asyncFillTid <- forkIOWithUnmask (fillPipe pipe)
|
|
|
418
|
+
|
|
371
|
419
|
let am = AsyncManager{..}
|
|
372
|
420
|
pure am
|
|
373
|
421
|
|
| ... |
... |
@@ -417,6 +465,55 @@ drainPipe p unmask = catch (unmask $ loop `finally` pipeCloseRead p) replyToPend |
|
417
|
465
|
|
|
418
|
466
|
am = pipeAsyncMngr p
|
|
419
|
467
|
|
|
|
468
|
+{-
|
|
|
469
|
+Note [Exceptions during serialization]
|
|
|
470
|
+-----------------------------------------------------------------------------
|
|
|
471
|
+
|
|
|
472
|
+For efficiency reasons, any `IddMsg` to be sent is forced only when already
|
|
|
473
|
+writing onto the Pipe, so we cannot discern whether any raised exception
|
|
|
474
|
+originated due to forcing during serialization or from interacting with the
|
|
|
475
|
+Pipe.
|
|
|
476
|
+
|
|
|
477
|
+If the exceptions happen to be `SomeAsyncException` or `IOException` the write
|
|
|
478
|
+end of the `Pipe` will be closed while rethrowing them, as those are likely to
|
|
|
479
|
+indicate something is wrong with the `Pipe` itself. Same for
|
|
|
480
|
+PipeWriteClosedException, used by `closePipe`.
|
|
|
481
|
+
|
|
|
482
|
+It is recommended to fully force messages at the source, to avoid needlessly
|
|
|
483
|
+interrupting communication.
|
|
|
484
|
+-}
|
|
|
485
|
+
|
|
|
486
|
+
|
|
|
487
|
+-- | "Fill"s write end of the Pipe with queued messages.
|
|
|
488
|
+--
|
|
|
489
|
+-- Because Pipe is written to from this thread rather than 'writePipe', calls to
|
|
|
490
|
+-- the latter can be interrupted without compromising the protocol.
|
|
|
491
|
+--
|
|
|
492
|
+-- Closes the Pipe write end when stopping, notifying any messages still queued to write.
|
|
|
493
|
+fillPipe :: Pipe -> (forall x. IO x -> IO x) -> IO ()
|
|
|
494
|
+fillPipe p unmask = catch (loop `finally` pipeCloseWrite p) (atomically . failPendingWrites p) where
|
|
|
495
|
+ loop = do
|
|
|
496
|
+ (msg,var) <- atomically $ readTQueue (writeQueue am)
|
|
|
497
|
+ success <- handle (\ e -> atomically (putTMVar var (Left e)) >> rethrowIfPipeFatal e >> pure False) $ do
|
|
|
498
|
+ unmask $ withLock p (putAll p . execPut . put $ msg)
|
|
|
499
|
+ pure True
|
|
|
500
|
+ when success $ atomically $ putTMVar var (Right ())
|
|
|
501
|
+ loop
|
|
|
502
|
+
|
|
|
503
|
+ -- See Note [Exceptions during serialization]
|
|
|
504
|
+ rethrowIfPipeFatal e
|
|
|
505
|
+ | Just SomeAsyncException{} <- fromException e = throwIO e
|
|
|
506
|
+ | Just (_ :: IOException) <- fromException e = throwIO e
|
|
|
507
|
+ | Just PipeWriteClosedException <- fromException e = throwIO e
|
|
|
508
|
+ | otherwise = pure ()
|
|
|
509
|
+ am = pipeAsyncMngr p
|
|
|
510
|
+
|
|
|
511
|
+failPendingWrites :: Pipe -> SomeException -> STM ()
|
|
|
512
|
+failPendingWrites p e = do
|
|
|
513
|
+ writeTVar (pipeWriteClosed p) True
|
|
|
514
|
+ writes <- flushTQueue (writeQueue (pipeAsyncMngr p))
|
|
|
515
|
+ mapM_ (`writeTMVar` Left e) (map snd writes)
|
|
|
516
|
+
|
|
420
|
517
|
failPendingReplies :: Pipe -> SomeException -> STM ()
|
|
421
|
518
|
failPendingReplies p e = do
|
|
422
|
519
|
-- reqs are only added if pipe not closed;
|