Skip to content

Commit

Permalink
fix: data race in gRPC client trailer
Browse files Browse the repository at this point in the history
  • Loading branch information
ppzqh committed Sep 4, 2024
1 parent ebd3dfe commit 6e8bc1c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pkg/remote/trans/nphttp2/grpc/http2_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,9 @@ func (t *http2Client) closeStream(s *Stream, err error, rst bool, rstCode http2.
// only after updating this.
s.status = st
if len(mdata) > 0 {
s.cliTlrMu.Lock()
s.trailer = mdata
s.cliTlrMu.Unlock()
}
if err != nil {
// This will unblock reads eventually.
Expand Down
5 changes: 5 additions & 0 deletions pkg/remote/trans/nphttp2/grpc/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,9 @@ type Stream struct {

// hdrMu protects header and trailer metadata on the server-side.
hdrMu sync.Mutex
// cliTlrMu protects trailer metadata on the client-side
cliTlrMu sync.RWMutex

// On client side, header keeps the received header metadata.
//
// On server side, header keeps the header set by SetHeader(). The complete
Expand Down Expand Up @@ -388,7 +391,9 @@ func (s *Stream) TrailersOnly() bool {
// It can be safely read only after stream has ended that is either read
// or write have returned io.EOF.
func (s *Stream) Trailer() metadata.MD {
s.cliTlrMu.RLock()
c := s.trailer.Copy()
s.cliTlrMu.RUnlock()
return c
}

Expand Down

0 comments on commit 6e8bc1c

Please sign in to comment.