From 72c6cd0816b11dd10b6ab1887ee531d5e76913c4 Mon Sep 17 00:00:00 2001 From: Yang Song Date: Wed, 20 Mar 2024 00:30:18 -0400 Subject: [PATCH] [exporter/datadog] Fix data race in metrics exporter shutdown (#31838) **Description:** Fix data race in metrics exporter shutdown. If we do not do `f.wg.Wait()` before closing `statsToAgent`, there can be a race condition when the goroutine still tries to write to `statsToAgent` after it is closed. Fixes https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/31663 --- .chloggen/ddog-data-race.yaml | 27 +++++++++++++++++++++++++++ exporter/datadogexporter/factory.go | 3 ++- 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 .chloggen/ddog-data-race.yaml diff --git a/.chloggen/ddog-data-race.yaml b/.chloggen/ddog-data-race.yaml new file mode 100644 index 0000000000000..ac2ee331a7299 --- /dev/null +++ b/.chloggen/ddog-data-race.yaml @@ -0,0 +1,27 @@ +# Use this changelog template to create an entry for release notes. + +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: bug_fix + +# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) +component: datadogexporter + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Fix data race in metrics exporter shutdown + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [31663] + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: + +# If your change doesn't affect end users or the exported elements of any package, +# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. +# Optional: The change log or logs in which this entry should be included. +# e.g. '[user]' or '[user, api]' +# Include 'user' if the change is relevant to end users. +# Include 'api' if there is a change to a library API. +# Default: '[user]' +change_logs: [] diff --git a/exporter/datadogexporter/factory.go b/exporter/datadogexporter/factory.go index ef9a8bf488a41..4f8d861179925 100644 --- a/exporter/datadogexporter/factory.go +++ b/exporter/datadogexporter/factory.go @@ -355,7 +355,8 @@ func (f *factory) createMetricsExporter( exporterhelper.WithCapabilities(consumer.Capabilities{MutatesData: true}), exporterhelper.WithQueue(cfg.QueueSettings), exporterhelper.WithShutdown(func(context.Context) error { - cancel() + cancel() // first cancel context + f.wg.Wait() // then wait for shutdown f.StopReporter() statsWriter.Stop() if statsIn != nil {