From 79df6ec38f378a9a25715d7976f5beb57997dfbf Mon Sep 17 00:00:00 2001 From: Liz Fong-Jones Date: Mon, 27 Sep 2021 19:30:12 -0700 Subject: [PATCH 1/3] lock accesses to encoder fixes #2264 --- exporters/stdout/stdouttrace/trace.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/exporters/stdout/stdouttrace/trace.go b/exporters/stdout/stdouttrace/trace.go index ba391851d40..8dd3162acb6 100644 --- a/exporters/stdout/stdouttrace/trace.go +++ b/exporters/stdout/stdouttrace/trace.go @@ -49,6 +49,7 @@ func New(options ...Option) (*Exporter, error) { // Exporter is an implementation of trace.SpanSyncer that writes spans to stdout. type Exporter struct { encoder *json.Encoder + encoderMu sync.Mutex timestamps bool stoppedMu sync.RWMutex @@ -83,6 +84,8 @@ func (e *Exporter) ExportSpans(ctx context.Context, spans []trace.ReadOnlySpan) } // Encode span stubs, one by one + e.encoderMu.Lock() + defer e.encoderMu.Unlock() if err := e.encoder.Encode(stub); err != nil { return err } From 2cf58d64210a360c1c8dcb4934351e3a390548aa Mon Sep 17 00:00:00 2001 From: Liz Fong-Jones Date: Mon, 27 Sep 2021 20:03:27 -0700 Subject: [PATCH 2/3] move locking outside loop to avoid deadlock --- exporters/stdout/stdouttrace/trace.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/exporters/stdout/stdouttrace/trace.go b/exporters/stdout/stdouttrace/trace.go index 8dd3162acb6..f1937a28ebe 100644 --- a/exporters/stdout/stdouttrace/trace.go +++ b/exporters/stdout/stdouttrace/trace.go @@ -71,6 +71,8 @@ func (e *Exporter) ExportSpans(ctx context.Context, spans []trace.ReadOnlySpan) stubs := tracetest.SpanStubsFromReadOnlySpans(spans) + e.encoderMu.Lock() + defer e.encoderMu.Unlock() for i := range stubs { stub := &stubs[i] // Remove timestamps @@ -84,8 +86,6 @@ func (e *Exporter) ExportSpans(ctx context.Context, spans []trace.ReadOnlySpan) } // Encode span stubs, one by one - e.encoderMu.Lock() - defer e.encoderMu.Unlock() if err := e.encoder.Encode(stub); err != nil { return err } From 86e5e1d391107ddbfa6ff361e077d404de8e23d6 Mon Sep 17 00:00:00 2001 From: Liz Fong-Jones Date: Mon, 27 Sep 2021 20:04:21 -0700 Subject: [PATCH 3/3] Update CHANGELOG.md --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 64f6b977fa4..f80aa3ff0f4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm ## [Unreleased] +### Fixed + +- json stdout exporter no longer crashes due to concurrency bug. (#2265) + ### Changed - NoopMeterProvider is now private and NewNoopMeterProvider must be used to obtain a noopMeterProvider. (#2237)