Skip to content

Commit ce1f88f

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, use "<nil>" as the GoString.
1 parent ee8ed34 commit ce1f88f

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

transport/transport.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,10 +341,16 @@ func (s *Stream) Read(p []byte) (n int, err error) {
341341
// The key to save transport.Stream in the context.
342342
type streamKey struct{}
343343

344+
// streamCtxGoStringer implements the GoStringer interface for Stream
345+
// so that ctx.String() is thread-safe by hiding the stream information.
346+
type streamCtxGoStringer Stream
347+
348+
func (ss *streamCtxGoStringer) GoString() string { return "<nil>" }
349+
344350
// newContextWithStream creates a new context from ctx and attaches stream
345351
// to it.
346352
func newContextWithStream(ctx context.Context, stream *Stream) context.Context {
347-
return context.WithValue(ctx, streamKey{}, stream)
353+
return context.WithValue(ctx, streamKey{}, (*streamCtxGoStringer)(stream))
348354
}
349355

350356
// StreamFromContext returns the stream saved in ctx.

0 commit comments

Comments
 (0)