Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix flaky test TestSimpleSpanProcessorShutdownHonorsContextCancel #2290

Merged
merged 9 commits into from Oct 19, 2021
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -26,6 +26,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
### Fixed

- `semconv.NetAttributesFromHTTPRequest()` correctly handles IPv6 addresses. (#2285)
- The simple span processor shutdown method deterministically returns the exporter error status if it simultaneously finishes when the deadline is reached. (#2290, #2289)

## [1.0.1] - 2021-10-01

Expand Down
16 changes: 15 additions & 1 deletion sdk/trace/simple_span_processor.go
Expand Up @@ -88,10 +88,24 @@ func (ssp *simpleSpanProcessor) Shutdown(ctx context.Context) error {

go shutdown()

// Wait for the exporter to shut down or the deadline to expire.
select {
case err = <-done:
case <-ctx.Done():
err = ctx.Err()
// It is possible for the exporter to have immediately shut down
// and the context to be done simultaneously. In that case this
// outer select statement will randomly choose a case. This will
// result in a different returned error for similar scenarios.
// Instead, double check if the exporter shut down at the same
// time and return that error if so. This will ensure consistency
// as well as ensure the caller knows the exporter shut down
// successfully (they can already determine if the deadline is
// expired given they passed the context).
select {
case err = <-done:
default:
err = ctx.Err()
}
}
})
return err
Expand Down
10 changes: 8 additions & 2 deletions sdk/trace/simple_span_processor_test.go
Expand Up @@ -40,9 +40,15 @@ func (t *testExporter) ExportSpans(ctx context.Context, spans []sdktrace.ReadOnl
return nil
}

func (t *testExporter) Shutdown(context.Context) error {
func (t *testExporter) Shutdown(ctx context.Context) error {
t.shutdown = true
return nil
select {
case <-ctx.Done():
// Ensure context deadline tests receive the expected error.
return ctx.Err()
default:
return nil
}
MrAlias marked this conversation as resolved.
Show resolved Hide resolved
}

var _ sdktrace.SpanExporter = (*testExporter)(nil)
Expand Down