Skip to content

Commit 377fdf6

Browse files
Anthony Romanomenghanl
authored andcommitted
transport: implement GoString on Stream (#1167)
So context.String() won't race when printing %#v. 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 9aa08c5 commit 377fdf6

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
@@ -338,6 +338,12 @@ func (s *Stream) Read(p []byte) (n int, err error) {
338338
return
339339
}
340340

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

0 commit comments

Comments
 (0)