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

transport: fix handling of header metadata in serverHandler #3484

Merged
merged 4 commits into from Apr 3, 2020

Conversation

misberner
Copy link
Contributor

@misberner misberner commented Mar 29, 2020

This PR changes the serverHandlerTransport code to take header metadata from the stream (set via invocations of SetHeader) into account, both when implicitly sending headers, or when explicitly sending headers via SendHeader (merging any previously set metadata with the additionally supplied one, as per the docs).

It also fixes an issue with SendHeader where modifications to the passed metadata map after the SendHeader call has already returned might still affect the header values that get written to the stream.

Please check out the first commit in this branch (3140f261) for a state that only has the modified test (which of course fails) added.

Fixes #3483.

@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Mar 29, 2020

CLA Check
The committers are authorized under a signed CLA.

ht.rw.Write(hdr)
ht.rw.Write(data)
ht.rw.(http.Flusher).Flush()
})
}

func (ht *serverHandlerTransport) WriteHeader(s *Stream, md metadata.MD) error {
if err := s.SetHeader(md); err != nil {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

tbh I don't really understand why WriteHeader even takes an md parameter - IMHO it would be more elegant to implement (*Stream).SendHeader via

s.SetHeader(md)
s.st.WriteHeader(s)

Since ServerTransport is an internal interface, I don't see any issues, but it would require changing the http2Server implementation as well, which is why I opted for a more local change.

internal/transport/handler_server.go Outdated Show resolved Hide resolved
internal/transport/handler_server.go Show resolved Hide resolved
internal/transport/handler_server_test.go Show resolved Hide resolved
internal/transport/handler_server_test.go Show resolved Hide resolved
@misberner misberner requested a review from dfawley April 3, 2020 19:07
@misberner
Copy link
Contributor Author

CI errors should now be fixed as well.

@dfawley
Copy link
Member

dfawley commented Apr 3, 2020

It also fixes an issue with SendHeader where modifications to the passed metadata map after the SendHeader call has already returned might still affect the header values that get written to the stream.

Can you explain this a little more? What might be modifying the metadata map and why? I don't believe this should be happening as the metadata godoc says modifying metadata stored in contexts is not allowed (e.g.).

@dfawley
Copy link
Member

dfawley commented Apr 3, 2020

Actually, nevermind my last question; I see the problem. I think there's a cleaner design here somewhere, but your changes LGTM.

@misberner
Copy link
Contributor Author

Can you explain this a little more? What might be modifying the metadata map and why? I don't believe this should be happening as the metadata godoc says modifying metadata stored in contexts is not allowed (e.g.).

This was a concurrency error/race condition. SendHeader returns once ht.do(func() { ... }) returns, which however only means that the closure has been pushed to the channel/accepted by the receiver, but not necessarily finished dispatching by the receiver. For this reason, a call

md := metadata.Pairs("foo", "bar")
s.SendHeader(md)
delete(md, "foo")

might non-deterministically result in a header with the "foo" key being written or not, since the dispatching of the closure which reads md happens concurrently with delete(md, "foo") without synchronization. That's why I changed the implementation of SendHeader to call SetHeader first, which ensures that no references to the passed MD map are retained after SendHeader returns.

@dfawley dfawley changed the title Fix handling of header metadata in serverHandlerTransport transport: fix handling of header metadata in serverHandler Apr 3, 2020
@dfawley dfawley merged commit 66e9dfe into grpc:master Apr 3, 2020
@misberner misberner deleted the mi-fix-headers branch April 3, 2020 19:59
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 25, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

serverHandlerTransport (Go HTTP API): SetHeader doesn't work
2 participants