From e64f801b8cfe4a15806f5604079c4ae226315bcf Mon Sep 17 00:00:00 2001 From: David Tarico Date: Tue, 26 Nov 2019 17:48:04 -0800 Subject: [PATCH] Fixed one source of memory leak: When using the Background context, the Done channel is nil --- xray/segment.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/xray/segment.go b/xray/segment.go index 281c62f4..80ba264e 100644 --- a/xray/segment.go +++ b/xray/segment.go @@ -68,13 +68,14 @@ func BeginSegment(ctx context.Context, name string) (context.Context, *Segment) seg.GetService().Version = seg.ParentSegment.GetConfiguration().ServiceVersion } - go func() { - select { - case <-ctx.Done(): - seg.handleContextDone() - } - }() - + if ctx.Done() != nil { + go func() { + select { + case <-ctx.Done(): + seg.handleContextDone() + } + }() + } return context.WithValue(ctx, ContextKey, seg), seg } @@ -247,7 +248,6 @@ func (seg *Segment) Close(err error) { seg.send() } - // CloseAndStream closes a subsegment and sends it. func (subseg *Segment) CloseAndStream(err error) { subseg.Lock()