Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deal with missing trailers #206

Merged
merged 1 commit into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/demo-client.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ $ cabal run demo-client -- --core sayHelloBidiStream \
{message: "John Ack"}
Disconnected. Reconnecting after 1701081μs
Reconnecting now.
demo-client: CallClosedWithoutTrailers
demo-client: GrpcException {grpcError = GrpcUnknown, grpcErrorMessage = Just "Call closed without trailers", grpcErrorMetadata = []}
```

### Dealing with unterminated streams
Expand Down
20 changes: 10 additions & 10 deletions src/Network/GRPC/Client/Session.hs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,15 @@ instance SupportsClientRpc rpc => IsSession (ClientSession rpc) where
-- for example, perhaps an intermediate cache has dropped the gRPC
-- trailers entirely. We therefore check for this case separately and
-- throw a different error.
throwIO $ CallClosedWithoutTrailers
--
-- We /must/ throw a GrpcException (rather than some kind of custom one)
-- because the spec mandates that we synthesize a status and status
-- message when the peer omits them.
throwIO $ GrpcException {
grpcError = GrpcUnknown
, grpcErrorMessage = Just "Call closed without trailers"
, grpcErrorMetadata = []
}
else
return $ parseProperTrailers' (Proxy @rpc) trailers

Expand All @@ -92,7 +100,7 @@ instance SupportsClientRpc rpc => InitiateSession (ClientSession rpc) where
-- We classify the response as Trailers-Only if the grpc-status header
-- is present, or when the HTTP status is anything other than 200 OK
-- (which we treat, as per the spec, as an implicit grpc-status).
-- The 'CallClosedWithoutTrailers' case is therefore not relevant.
-- The "closed without trailers" case is therefore not relevant.
case verifyAllIf connVerifyHeaders trailersOnly of
Left err -> throwIO $ CallSetupInvalidResponseHeaders err
Right _hdrs -> return $ FlowStartNoMessages trailersOnly
Expand Down Expand Up @@ -160,13 +168,5 @@ data InvalidTrailers =
invalidTrailers :: [HTTP.Header]
, invalidTrailersError :: String
}

-- | The server terminated without sending trailers at all
--
-- This can also happen when a server just suddenly disappears (or the
-- network connection is dropped). We cannot reliably distinguish between
-- "we lost the connection" and "the server closed the connection but did
-- not send us any trailers".
| CallClosedWithoutTrailers
deriving stock (Show)
deriving anyclass (Exception)
77 changes: 77 additions & 0 deletions test-grapesy/Test/Sanity/BrokenDeployments.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Control.Concurrent
import Control.Concurrent.Async
import Control.Exception
import Data.ByteString.Builder qualified as ByteString (Builder)
import Data.Text qualified as Text
import Network.HTTP.Types qualified as HTTP
import Network.HTTP2.Server qualified as HTTP2
import Network.Run.TCP qualified as NetworkRun
Expand All @@ -32,6 +33,11 @@ tests = testGroup "Test.Sanity.BrokenDeployments" [
, testCase "nonGrpcTrailersOnly" test_nonGrpcContentTypeTrailersOnly
, testCase "missingTrailersOnly" test_missingContentTypeTrailersOnly
]
, testGroup "Omit" [
testCase "status" test_omitStatus
, testCase "statusMessage" test_omitStatusMessage
, testCase "allTrailers" test_omitAllTrailers
]
]

{-------------------------------------------------------------------------------
Expand Down Expand Up @@ -99,6 +105,77 @@ test_missingContentTypeTrailersOnly = test_invalidContentType def {
responseHeaders = [ ("grpc-status", "0") ]
}

{-------------------------------------------------------------------------------
Omit trailers
-------------------------------------------------------------------------------}

test_omitStatus :: Assertion
test_omitStatus = respondWith response $ \addr -> do
mResp :: Either GrpcException
(StreamElem NoMetadata (Proto PongMessage)) <- try $
Client.withConnection connParams (Client.ServerInsecure addr) $ \conn ->
Client.withRPC conn def (Proxy @Ping) $ \call -> do
Client.sendFinalInput call defMessage
Client.recvOutput call
case mResp of
Left err
| grpcError err == GrpcUnknown
, Just msg <- grpcErrorMessage err
, "grpc-status" `Text.isInfixOf` msg ->
return ()
_otherwise ->
assertFailure $ "Unexpected response: " ++ show mResp
where
response :: Response
response = def {
responseTrailers = [
("grpc-message", "Message but no status")
]
}

test_omitStatusMessage :: Assertion
test_omitStatusMessage = respondWith response $ \addr -> do
mResp :: Either GrpcException
(StreamElem NoMetadata (Proto PongMessage)) <- try $
Client.withConnection connParams (Client.ServerInsecure addr) $ \conn ->
Client.withRPC conn def (Proxy @Ping) $ \call -> do
Client.sendFinalInput call defMessage
Client.recvOutput call
case mResp of
Right (NoMoreElems _) ->
return ()
_otherwise ->
assertFailure $ "Unexpected response: " ++ show mResp
where
response :: Response
response = def {
responseTrailers = [
("grpc-status", "0")
]
}

test_omitAllTrailers :: Assertion
test_omitAllTrailers = respondWith response $ \addr -> do
mResp :: Either GrpcException
(StreamElem NoMetadata (Proto PongMessage)) <- try $
Client.withConnection connParams (Client.ServerInsecure addr) $ \conn ->
Client.withRPC conn def (Proxy @Ping) $ \call -> do
Client.sendFinalInput call defMessage
Client.recvOutput call
case mResp of
Left err
| grpcError err == GrpcUnknown
, Just msg <- grpcErrorMessage err
, "closed without trailers" `Text.isInfixOf` msg ->
return ()
_otherwise ->
assertFailure $ "Unexpected response: " ++ show mResp
where
response :: Response
response = def {
responseTrailers = []
}

{-------------------------------------------------------------------------------
Test server

Expand Down