Skip to content

Commit edb53f3

Browse files
fix: fix race conditions
1 parent 340e063 commit edb53f3

File tree

7 files changed

+53
-9
lines changed

7 files changed

+53
-9
lines changed

delayed_spans.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,20 @@ func (ds *delayedSpans) flush() {
3333
case s := <-ds.spans:
3434
t, ok := s.Tracer().(Tracer)
3535
if !ok {
36-
sensor.logger.Debug("span tracer has unexpected type")
36+
muSensor.Lock()
37+
if sensor != nil {
38+
sensor.logger.Debug("span tracer has unexpected type")
39+
}
40+
muSensor.Unlock()
3741
continue
3842
}
3943

4044
if err := ds.processSpan(s, t.Options()); err != nil {
41-
sensor.logger.Debug("error while processing spans:", err.Error())
45+
muSensor.Lock()
46+
if sensor != nil {
47+
sensor.logger.Debug("error while processing spans:", err.Error())
48+
}
49+
muSensor.Unlock()
4250
continue
4351
}
4452

event.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ func sendEvent(event *EventData) {
8080

8181
// we do fire & forget here, because the whole pid dance isn't necessary to send events
8282
go func() {
83-
_ = sensor.Agent().SendEvent(event)
83+
muSensor.Lock()
84+
if sensor != nil {
85+
_ = sensor.Agent().SendEvent(event)
86+
}
87+
muSensor.Unlock()
8488
}()
8589
}

meter.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,19 @@ func (m *meterS) Run(collectInterval time.Duration) {
4343
case <-m.done:
4444
return
4545
case <-ticker.C:
46-
if sensor.Agent().Ready() {
46+
// Get agent ready status under proper synchronization
47+
muSensor.Lock()
48+
agentReady := sensor != nil && sensor.Agent().Ready()
49+
muSensor.Unlock()
50+
51+
if agentReady {
4752
go func() {
48-
_ = sensor.Agent().SendMetrics(m.collectMetrics())
53+
metrics := m.collectMetrics()
54+
muSensor.Lock()
55+
if sensor != nil {
56+
_ = sensor.Agent().SendMetrics(metrics)
57+
}
58+
muSensor.Unlock()
4959
}()
5060
}
5161
}

recorder.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func (r *Recorder) RecordSpan(span *spanS) {
8181
return
8282
}
8383

84-
agentReady := sensor.Agent().Ready()
84+
agentReady := sensor != nil && sensor.Agent().Ready()
8585
maxBufferedSpans := sensor.options.MaxBufferedSpans
8686
forceTransmissionAt := sensor.options.ForceTransmissionStartingAt
8787
logger := sensor.logger

sensor.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,13 +224,23 @@ func InitSensor(options *Options) {
224224
})
225225

226226
autoprofile.SetSendProfilesFunc(func(profiles []autoprofile.Profile) error {
227-
if !sensor.Agent().Ready() {
227+
// Get agent ready status under proper synchronization
228+
muSensor.Lock()
229+
agentReady := sensor != nil && sensor.Agent().Ready()
230+
muSensor.Unlock()
231+
232+
if !agentReady {
228233
return errors.New("sender not ready")
229234
}
230235

231236
sensor.logger.Debug("sending profiles to agent")
232237

233-
return sensor.Agent().SendProfiles(profiles)
238+
// Use the same lock for sending profiles
239+
muSensor.Lock()
240+
err := sensor.Agent().SendProfiles(profiles)
241+
muSensor.Unlock()
242+
243+
return err
234244
})
235245

236246
if _, ok := os.LookupEnv("INSTANA_AUTO_PROFILE"); ok || options.EnableAutoProfile {

span.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,12 @@ func (r *spanS) FinishWithOptions(opts ot.FinishOptions) {
8686

8787
r.Duration = duration
8888
if r.sendSpanToAgent() {
89-
if sensor.Agent().Ready() {
89+
// Get agent ready status under proper synchronization
90+
muSensor.Lock()
91+
agentReady := sensor != nil && sensor.Agent().Ready()
92+
muSensor.Unlock()
93+
94+
if agentReady {
9095
r.tracer.recorder.RecordSpan(r)
9196
} else {
9297
delayed.append(r)

tracer.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,5 +135,12 @@ func (r *tracerS) Flush(ctx context.Context) error {
135135
return err
136136
}
137137

138+
muSensor.Lock()
139+
defer muSensor.Unlock()
140+
141+
if sensor == nil {
142+
return nil
143+
}
144+
138145
return sensor.Agent().Flush(ctx)
139146
}

0 commit comments

Comments
 (0)