| ... |
... |
@@ -128,7 +128,10 @@ closePipe p@Pipe{..} = do |
|
128
|
128
|
failPendingReplies p (toException PipeReadClosedException)
|
|
129
|
129
|
pure closed
|
|
130
|
130
|
|
|
131
|
|
- writeAlreadyClosed <- readTVarIO pipeWriteClosed
|
|
|
131
|
+ writeAlreadyClosed <- atomically $ do
|
|
|
132
|
+ closed <- readTVar pipeWriteClosed
|
|
|
133
|
+ failPendingWrites p (toException PipeWriteClosedException)
|
|
|
134
|
+ pure closed
|
|
132
|
135
|
|
|
133
|
136
|
unless writeAlreadyClosed (withLock p pipeCloseWrite)
|
|
134
|
137
|
`finally` do
|
| ... |
... |
@@ -152,11 +155,14 @@ instance Exception PipeWriteClosedException |
|
152
|
155
|
-- and identify whether they are requests or replies.
|
|
153
|
156
|
|
|
154
|
157
|
writePipe :: Pipe -> IddMsg -> IO ()
|
|
155
|
|
-writePipe Pipe{..} msg = atomically $ do
|
|
156
|
|
- readTVar pipeWriteClosed >>= \case
|
|
157
|
|
- True -> throwSTM PipeWriteClosedException
|
|
158
|
|
- False -> pure ()
|
|
159
|
|
- writeTQueue (writeQueue pipeAsyncMngr) msg
|
|
|
158
|
+writePipe Pipe{..} msg = do
|
|
|
159
|
+ var <- newEmptyTMVarIO
|
|
|
160
|
+ atomically $ do
|
|
|
161
|
+ readTVar pipeWriteClosed >>= \case
|
|
|
162
|
+ True -> throwSTM PipeWriteClosedException
|
|
|
163
|
+ False -> pure ()
|
|
|
164
|
+ writeTQueue (writeQueue pipeAsyncMngr) (msg, var)
|
|
|
165
|
+ either throwIO pure =<< atomically (takeTMVar var)
|
|
160
|
166
|
|
|
161
|
167
|
-- | Only the 'drainPipe' thread should read from the pipe directly. Other
|
|
162
|
168
|
-- functions should wait for the matching reply to be delivered asynchronously.
|
| ... |
... |
@@ -345,7 +351,7 @@ data AsyncManager = AsyncManager |
|
345
|
351
|
, mailboxQueues :: !(TVar (IntMap (TQueue (CorrelationId, LB.ByteString))))
|
|
346
|
352
|
-- ^ Requests for a specific 'InboxId' that can be read using
|
|
347
|
353
|
-- @'handleAsyncReq' p get (Just <mailboxid>)@.
|
|
348
|
|
- , writeQueue :: !(TQueue IddMsg)
|
|
|
354
|
+ , writeQueue :: !(TQueue (IddMsg, TMVar (Either SomeException ())))
|
|
349
|
355
|
-- ^ Messages to write on the pipe.
|
|
350
|
356
|
, asyncDrainTid :: !ThreadId
|
|
351
|
357
|
, asyncFillTid :: !ThreadId
|
| ... |
... |
@@ -453,26 +459,26 @@ drainPipe p unmask = catch (unmask $ loop `finally` pipeCloseRead p) replyToPend |
|
453
|
459
|
|
|
454
|
460
|
-- | "Fill"s write end of the Pipe with queues messages.
|
|
455
|
461
|
--
|
|
456
|
|
--- Closes the Pipe write end when stopping, notifying any requests still queued to write.
|
|
|
462
|
+-- Because Pipe is written to from this thread rather than 'writePipe', calls to
|
|
|
463
|
+-- the latter can be interrupted without compromising the protocol.
|
|
|
464
|
+--
|
|
|
465
|
+-- Closes the Pipe write end when stopping, notifying any messages still queued to write.
|
|
457
|
466
|
fillPipe :: Pipe -> (forall x. IO x -> IO x) -> IO ()
|
|
458
|
|
-fillPipe p unmask = unmask $ catch (loop `finally` pipeCloseWrite p) failPendingWrites where
|
|
|
467
|
+fillPipe p unmask = catch (loop `finally` pipeCloseWrite p) (atomically . failPendingWrites p) where
|
|
459
|
468
|
loop = do
|
|
460
|
|
- msg <- atomically $ readTQueue (writeQueue am)
|
|
461
|
|
- withLock p . putAll p . execPut . put $ msg
|
|
|
469
|
+ (msg,var) <- atomically $ readTQueue (writeQueue am)
|
|
|
470
|
+ unmask (withLock p (putAll p . execPut . put $ msg))
|
|
|
471
|
+ `catch` \ e -> atomically (putTMVar var (Left e)) >> throwIO e
|
|
|
472
|
+ atomically $ putTMVar var (Right ())
|
|
462
|
473
|
loop
|
|
463
|
474
|
|
|
464
|
475
|
am = pipeAsyncMngr p
|
|
465
|
476
|
|
|
466
|
|
- failPendingWrites :: SomeException -> IO ()
|
|
467
|
|
- failPendingWrites e = atomically $ do
|
|
468
|
|
- writeTVar (pipeWriteClosed p) True
|
|
469
|
|
-
|
|
470
|
|
- writes <- flushTQueue (writeQueue am)
|
|
471
|
|
- let uqs = [uq | Request uq _ _ <- writes]
|
|
472
|
|
- pendings <- readTVar (pendingReply (pipeAsyncMngr p))
|
|
473
|
|
- let tmvars = mapMaybe (`IntMap.lookup` pendings) uqs
|
|
474
|
|
- writeTVar (pendingReply (pipeAsyncMngr p)) $! (foldl' (flip IntMap.delete) pendings uqs)
|
|
475
|
|
- mapM_ (`writeTMVar` Left e) tmvars
|
|
|
477
|
+failPendingWrites :: Pipe -> SomeException -> STM ()
|
|
|
478
|
+failPendingWrites p e = do
|
|
|
479
|
+ writeTVar (pipeWriteClosed p) True
|
|
|
480
|
+ writes <- flushTQueue (writeQueue (pipeAsyncMngr p))
|
|
|
481
|
+ mapM_ (`writeTMVar` Left e) (map snd writes)
|
|
476
|
482
|
|
|
477
|
483
|
failPendingReplies :: Pipe -> SomeException -> STM ()
|
|
478
|
484
|
failPendingReplies p e = do
|