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

grpcweb-transport: fix handling responses with empty body and status in headers #331

Merged
merged 1 commit into from
Jul 7, 2022

Conversation

eKazim
Copy link
Contributor

@eKazim eKazim commented Jun 12, 2022

Fixes #330

Fixed grpcweb-transport to handle responses with empty body and status header.
}
});

it('success if empty response body and OK status in headers', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md#responses

Most responses are expected to have both headers and trailers but Trailers-Only is permitted for calls that produce an immediate error.

Thus I would expect any "Trailers-Only" response to throw an RpcError.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is of course logical for non OK status codes. But in practice, it will be strange if received code OK will be accepted as error on client side.
Maybe initially this case (sending from server only status OK with empty body in stream) is strange, because I think normally server should send something like NOT_FOUND code if there is nothing to send in stream.
But at the moment exactly this handling is implemented in the official implementation of the protocol in https://github.com/grpc/grpc-web

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sentence in the spec is frustratingly ambiguous. And unfortunately none of the official implementation tests actually assert what should happen when getting an OK trailers-only unary RPC response: https://github.com/grpc/grpc-web/blob/master/packages/grpc-web/test/generated_code_test.js

Though searching through issues on grpc/grpc I get the sense that it is not allowed for the server to send an OK status without a message in a unary RPC. From grpc/grpc-java#2785 (comment):

Unary must either error or have a message.

The server does not enforce "unary must have one message." If the application closes with OK but no messages, this should become an error of some sort (sent to the client). This would ideally be done in ServerCallImpl.

From grpc/grpc#8010 (comment):

Is the server calling StreamObserver.onCompleted() (or ServerCall.close(Status.OK,...)) without first sending a message?

[...] no problem. It made us realize we weren't verifying that on server-side.

From grpc/grpc-java#2243:

It appears that a server can respond OK to a unary RPC without any detection on the server-side. It'd be nice to throw an exception if close(OK) is called without first sending a message for a UNARY response, although that may be against our API stability. At the very least, we should convert the OK into a Status saying the server is broken.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For unary RPC, I have not changed the final behavior and in this case there will be DATA_LOSS error code. See test at line 313.

In general, while there is no unambiguous interpretation yet of this case with streams, it is better to have an implementation compatible with other libraries so that it is easier for developers to migrate from one implementation to another.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahhh, my mistake. This makes sense for serverStreaming.

Copy link
Owner

@timostamm timostamm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree we should accept a response with a grpc-status header and empty body as valid for server-streaming. It's a pity that the spec is so vague around this. Thanks you @eKazim for the fix. Thank you @jcready for the review.

@timostamm timostamm merged commit 02c82bd into timostamm:master Jul 7, 2022
@timostamm
Copy link
Owner

Released in v2.8.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

RpcError: missing trailers when empty response
3 participants