Skip to content

Commit 7e616f5

Browse files
author
Anthony Romano
committed
transport: hide Stream string when stored in context
It is not thread-safe to call context.String() on any context with a stream value since valueCtx will use %#v to access all of the Stream fields without holding a lock. Instead, print the Stream's pointer and method for its GoString.
1 parent 9f9c190 commit 7e616f5

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

transport/transport.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,12 @@ func (s *Stream) finish(st *status.Status) {
341341
close(s.done)
342342
}
343343

344+
// GoString is implemented by Stream so context.String() won't
345+
// race when printing %#v.
346+
func (s *Stream) GoString() string {
347+
return fmt.Sprintf("<stream: %p, %v>", s, s.method)
348+
}
349+
344350
// The key to save transport.Stream in the context.
345351
type streamKey struct{}
346352

0 commit comments

Comments
 (0)