Skip to content

Commit 009ab4a

Browse files
committed
network-transport-quic: clean up the endpoint close process
1 parent 37019d1 commit 009ab4a

8 files changed

Lines changed: 54 additions & 31 deletions

File tree

‎cabal.project‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,8 @@ allow-newer:
99
cborg:base,
1010
serialise:base
1111

12+
-- 2.1.3 is broken: https://github.com/kazu-yamamoto/crypton/issues/270
13+
constraints: cryption <2.1.3
14+
1215
package distributed-process-tests
1316
flags: +tcp

‎packages/network-transport-quic/CHANGELOG.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
2026-09-30 Laurent P. René de Cotret <laurent.decotret@outlook.com> 0.2.0
2+
3+
* Cleaned up the endpoint closing process, which required adding a new configuration to `QUICTransportConfig`.
14

25
2026-04-21 Laurent P. René de Cotret <laurent.decotret@outlook.com> 0.1.2
36

‎packages/network-transport-quic/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ main = do
3131
, serviceName = "https" -- alternatively, some port number
3232
, credentials = NonEmpty.singleton credential
3333
, validateCredentials = True -- should be 'False' for self-signed certificate
34+
, closeEndPointTimeout = 1000000
3435
}
3536
transport <- createTransport config
3637
...

‎packages/network-transport-quic/bench/Bench.hs‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,10 @@ quicConfig =
5959
Left errmsg -> throwIO $ userError errmsg
6060
Right credentials ->
6161
QUIC.createTransport
62-
( QUIC.QUICTransportConfig
63-
{ hostName = "127.0.0.1"
64-
, serviceName = "0"
65-
, credentials = credentials :| []
62+
( (QUIC.defaultQUICTransportConfig "127.0.0.1" (credentials :| []))
63+
{ QUIC.serviceName = "0"
6664
, -- credentials are self-signed
67-
validateCredentials = False
65+
QUIC.validateCredentials = False
6866
}
6967
)
7068
}

‎packages/network-transport-quic/network-transport-quic.cabal‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
cabal-version: 3.0
22
Name: network-transport-quic
3-
Version: 0.1.2
3+
Version: 0.2.0
44
build-Type: Simple
55
License: BSD-3-Clause
66
License-file: LICENSE

‎packages/network-transport-quic/src/Network/Transport/QUIC/Internal.hs‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import Control.Concurrent.STM.TQueue
3030
writeTQueue,
3131
)
3232
import Control.Exception (Exception (displayException), IOException, bracket, throwIO, try)
33-
import Control.Monad (unless, when)
33+
import Control.Monad (forM_, unless, void, when)
3434
import Data.Bifunctor (Bifunctor (first))
3535
import Data.Binary qualified as Binary (decodeOrFail)
3636
import Data.ByteString (ByteString, fromStrict)
@@ -274,11 +274,12 @@ handleIncomingMessages ourEndPoint remoteEndPoint =
274274
-- handleIncomingMessages only runs on incoming remote endpoints, so if
275275
-- the state was still Valid there is exactly one logical connection to
276276
-- surface as closed.
277-
wasValid <- modifyMVar (remoteEndPoint ^. remoteEndPointState) $ \case
278-
RemoteEndPointValid _ -> pure (RemoteEndPointClosed, True)
279-
other -> pure (other, False)
280-
when wasValid $
277+
mIsClosed <- modifyMVar (remoteEndPoint ^. remoteEndPointState) $ \case
278+
RemoteEndPointValid (ValidRemoteEndPointState _ isClosed) -> pure (RemoteEndPointClosed, Just isClosed)
279+
other -> pure (other, Nothing)
280+
forM_ mIsClosed $ \isClosed -> do
281281
atomically $ writeTQueue ourQueue (ConnectionClosed connectionId)
282+
void $ tryPutMVar isClosed ()
282283

283284
handleMessage :: [ByteString] -> IO ()
284285
handleMessage payload =

‎packages/network-transport-quic/src/Network/Transport/QUIC/Internal/QUICTransport.hs‎

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,9 @@ import Control.Concurrent.Async (forConcurrently_)
7171
import Control.Concurrent.MVar (MVar, modifyMVar, modifyMVar_, newMVar, readMVar, tryPutMVar)
7272
import Control.Concurrent.STM.TQueue (TQueue, writeTQueue)
7373
import Control.Exception (bracketOnError)
74-
import Control.Monad (forM_)
74+
import Control.Monad (forM_, void, when)
7575
import Control.Monad.STM (atomically)
76+
import Data.Either (isRight)
7677
import Data.Function ((&))
7778
import Data.List.NonEmpty (NonEmpty)
7879
import Data.List.NonEmpty qualified as NE
@@ -94,6 +95,7 @@ import Network.Transport.QUIC.Internal.Messaging
9495
sendCloseEndPoint,
9596
)
9697
import 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.
116121
data 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

139151
data 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
)

‎packages/network-transport-quic/test/Test/Network/Transport/QUIC.hs‎

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,18 @@ tests =
3333
testCaseWithTimeout "connections" $ withQUICTransport $ flip Tests.testConnections 5,
3434
testCaseWithTimeout "closeOneConnection" $ withQUICTransport $ flip Tests.testCloseOneConnection 5,
3535
testCaseWithTimeout "closeOneDirection" $ withQUICTransport $ flip Tests.testCloseOneDirection 5,
36-
flaky $ testCaseWithTimeout "closeReopen" $ withQUICTransport $ flip Tests.testCloseReopen 5,
36+
testCaseWithTimeout "closeReopen" $ withQUICTransport $ flip Tests.testCloseReopen 5,
3737
-- This test is flaky specifically in Github Actions
3838
flaky $ testCaseWithTimeout "parallelConnects" $ withQUICTransport $ flip Tests.testParallelConnects 5,
3939
testCaseWithTimeout "selfSend" $ withQUICTransport Tests.testSelfSend,
40-
flaky $ testCaseWithTimeout "closeTwice" $ withQUICTransport $ flip Tests.testCloseTwice 1,
40+
testCaseWithTimeout "closeTwice" $ withQUICTransport $ flip Tests.testCloseTwice 1,
4141
testCaseWithTimeout "connectToSelf" $ withQUICTransport $ flip Tests.testConnectToSelf 5,
4242
testCaseWithTimeout "connectToSelfTwice" $ withQUICTransport $ flip Tests.testConnectToSelfTwice 5,
4343
testCaseWithTimeout "closeSelf" $ withQUICTransport (Tests.testCloseSelf . pure . Right),
44-
flaky $ testCaseWithTimeout "closeEndPoint" $ withQUICTransport $ flip Tests.testCloseEndPoint 1,
45-
flaky $ testCaseWithTimeout "closeTransport" $ Tests.testCloseTransport mkQUICTransport,
46-
flaky $ testCaseWithTimeout "connectClosedEndPoint" $ withQUICTransport Tests.testConnectClosedEndPoint,
47-
flaky testSendVeryLargeMessages
44+
testCaseWithTimeout "closeEndPoint" $ withQUICTransport $ flip Tests.testCloseEndPoint 1,
45+
testCaseWithTimeout "closeTransport" $ Tests.testCloseTransport mkQUICTransport,
46+
testCaseWithTimeout "connectClosedEndPoint" $ withQUICTransport Tests.testConnectClosedEndPoint,
47+
testSendVeryLargeMessages
4848
]
4949

5050
flaky :: TestTree -> TestTree
@@ -69,10 +69,8 @@ mkQUICTransport = do
6969
Right creds ->
7070
Right
7171
<$> QUIC.createTransport
72-
( QUICTransportConfig
73-
{ hostName = "127.0.0.1",
74-
serviceName = "0",
75-
credentials = creds :| [],
72+
( (QUIC.defaultQUICTransportConfig "127.0.0.1" (creds :| []))
73+
{ serviceName = "0",
7674
-- credentials are self-signed
7775
validateCredentials = False
7876
}

0 commit comments

Comments
 (0)