From de6489788fd3cac48c982c3d304120bbd9049d6c Mon Sep 17 00:00:00 2001 From: Yukinari Toyota Date: Thu, 4 Apr 2019 13:28:19 +0900 Subject: [PATCH] fix dataraces by #102 --- xray/httptrace.go | 4 ++++ xray/segment.go | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/xray/httptrace.go b/xray/httptrace.go index 6126f093..ac66a851 100644 --- a/xray/httptrace.go +++ b/xray/httptrace.go @@ -47,6 +47,8 @@ func (xt *HTTPSubsegments) GetConn(hostPort string) { // DNSStart begins a dns subsegment if the HTTP operation // subsegment is still in progress. func (xt *HTTPSubsegments) DNSStart(info httptrace.DNSStartInfo) { + xt.mu.Lock() + defer xt.mu.Unlock() if GetSegment(xt.opCtx).safeInProgress() && xt.connCtx != nil { xt.dnsCtx, _ = BeginSubsegment(xt.connCtx, "dns") } @@ -71,6 +73,8 @@ func (xt *HTTPSubsegments) DNSDone(info httptrace.DNSDoneInfo) { // ConnectStart begins a dial subsegment if the HTTP operation // subsegment is still in progress. func (xt *HTTPSubsegments) ConnectStart(network, addr string) { + xt.mu.Lock() + defer xt.mu.Unlock() if GetSegment(xt.opCtx).safeInProgress() && xt.connCtx != nil { xt.connectCtx, _ = BeginSubsegment(xt.connCtx, "dial") } diff --git a/xray/segment.go b/xray/segment.go index ac7e43af..0f358e65 100644 --- a/xray/segment.go +++ b/xray/segment.go @@ -285,6 +285,10 @@ func (seg *Segment) RemoveSubsegment(remove *Segment) bool { seg.rawSubsegments[len(seg.rawSubsegments)-1] = nil seg.rawSubsegments = seg.rawSubsegments[:len(seg.rawSubsegments)-1] + if seg.ParentSegment != seg { + seg.ParentSegment.Lock() + defer seg.ParentSegment.Unlock() + } seg.ParentSegment.totalSubSegments-- seg.openSegments-- return true