@@ -71,8 +71,9 @@ import Control.Concurrent.Async (forConcurrently_)
7171import Control.Concurrent.MVar (MVar , modifyMVar , modifyMVar_ , newMVar , readMVar , tryPutMVar )
7272import Control.Concurrent.STM.TQueue (TQueue , writeTQueue )
7373import Control.Exception (bracketOnError )
74- import Control.Monad (forM_ )
74+ import Control.Monad (forM_ , void , when )
7575import Control.Monad.STM (atomically )
76+ import Data.Either (isRight )
7677import Data.Function ((&) )
7778import Data.List.NonEmpty (NonEmpty )
7879import Data.List.NonEmpty qualified as NE
@@ -94,6 +95,7 @@ import Network.Transport.QUIC.Internal.Messaging
9495 sendCloseEndPoint ,
9596 )
9697import Network.Transport.QUIC.Internal.QUICAddr (EndPointId , QUICAddr (.. ), encodeQUICAddr )
98+ import System.Timeout (timeout )
9799
98100{- The QUIC transport has three levels of statefullness:
99101
@@ -113,6 +115,9 @@ Finally, each connection between endpoint has some state, needed to receive data
113115-}
114116
115117-- | Represents the configuration used by the entire transport.
118+ --
119+ -- You should use the default values in `defaultQUICTransportConfig`
120+ -- and override the fields you care about.
116121data QUICTransportConfig = QUICTransportConfig
117122 { -- | Host name, which can be an IP address or a domain name.
118123 hostName :: HostName ,
@@ -123,7 +128,13 @@ data QUICTransportConfig = QUICTransportConfig
123128 -- | Note that if your credentials is self-signed, you will have
124129 -- to turn off 'validateCredentials'. This should only be set to 'False'
125130 -- in tests, or in a private network.
126- validateCredentials :: Bool
131+ validateCredentials :: Bool ,
132+ -- | How long, in microseconds, closing an endpoint waits for each peer
133+ -- to acknowledge that it has processed the close of our outgoing
134+ -- connections to it.
135+ --
136+ -- @since 0.2.0
137+ closeEndPointTimeout :: Int
127138 }
128139 deriving (Eq , Show )
129140
@@ -133,7 +144,8 @@ defaultQUICTransportConfig host creds =
133144 { hostName = host,
134145 serviceName = " 443" ,
135146 credentials = creds,
136- validateCredentials = True
147+ validateCredentials = True ,
148+ closeEndPointTimeout = 1000000
137149 }
138150
139151data QUICTransport = QUICTransport
@@ -306,23 +318,30 @@ closeLocalEndpoint quicTransport localEndPoint = do
306318
307319 -- Close outgoing remote endpoints before incoming. The peer's handleIncomingMessages
308320 -- reader writes ConnectionClosed in response to our outgoing close; its listenForClose
309- -- writes ErrorEvent in response to our incoming close. Processing outgoing first gives
310- -- the peer's event queue the expected ConnectionClosed-before-ErrorEvent ordering.
321+ -- writes ErrorEvent in response to our incoming close.
322+ --
323+ -- Each of these is read from a separate QUIC connection, so some synchronization is required.
324+ -- That gives the peer's event queue the expected ConnectionClosed-before-ErrorEvent ordering.
311325 forM_ mPreviousState $ \ vst -> do
312- forConcurrently_ (vst ^. outgoingConnections) tryCloseRemoteStream
313- forConcurrently_ (vst ^. incomingConnections) tryCloseRemoteStream
326+ forConcurrently_ (vst ^. outgoingConnections) ( tryCloseRemoteStream Outgoing )
327+ forConcurrently_ (vst ^. incomingConnections) ( tryCloseRemoteStream Incoming )
314328 atomically $ writeTQueue (localEndPoint ^. localQueue) EndPointClosed
315329 where
316- tryCloseRemoteStream :: RemoteEndPoint -> IO ()
317- tryCloseRemoteStream remoteEndPoint = do
330+ tryCloseRemoteStream :: Direction -> RemoteEndPoint -> IO ()
331+ tryCloseRemoteStream direction remoteEndPoint = do
318332 mCleanup <- modifyMVar (remoteEndPoint ^. remoteEndPointState) $ \ case
319333 RemoteEndPointInit -> pure (RemoteEndPointClosed , Nothing )
320334 RemoteEndPointClosed -> pure (RemoteEndPointClosed , Nothing )
321335 RemoteEndPointValid vst ->
322336 pure
323337 ( RemoteEndPointClosed ,
324338 Just $ do
325- _ <- sendCloseEndPoint (vst ^. remoteStream)
339+ sent <- sendCloseEndPoint (vst ^. remoteStream)
340+ when (direction == Outgoing && isRight sent) $
341+ void $
342+ timeout
343+ (closeEndPointTimeout $ quicTransport ^. transportConfig)
344+ (readMVar (vst ^. remoteStreamIsClosed))
326345 _ <- tryPutMVar (vst ^. remoteStreamIsClosed) ()
327346 pure ()
328347 )
0 commit comments