Skip to content

Fix Tracer - Telemetry destruction order issue - #400

Merged
xlamorlette-datadog merged 9 commits into
mainfrom
xlamorlette/fix-telemetry-destruction-bug
Nov 17, 2026
Merged

xlamorlette-datadog merged 9 commits into
mainfrom
xlamorlette/fix-telemetry-destruction-bug

Conversation

@xlamorlette-datadog

@xlamorlette-datadog xlamorlette-datadog commented Nov 13, 2026 •

Copy link
Copy Markdown
Collaborator

Overview

Fix a (rare) crash when a process exits with a Tracer instance that lives in a static wrapper.

Motivation

This is a hardening following DataDog/nginx-datadog#483. The problem was detected in the Datadog Nginx module, and fixed there by avoiding constructing a Tracer in processes where it is not needed, but the problem can still appear in another context.

Two issues were detected: the first one (StaticOtelCtxState singleton) was fixed by #398; this is the second issue fix.

Additional Notes

Detailed Bug Description

If a program keeps a Tracer in a static wrapper (for example an empty static std::optional<Tracer>, filled later) and calls exit() while the Tracer is still alive, the Tracer's destructor can use the telemetry singleton after exit() has destroyed it.

The telemetry singleton is a classical local static TelemetryProxy, in the TelemetryProxy& instance() function in src/datadog/telemetry/telemetry.cpp.

Tracer construction call chain: Tracer() → telemetry::init() → TelemetryProxy& instance().

Tracer destruction call chain: ~Tracer()=default, Tracer has a shared_ptr<Collector>, Collector default being DatadogAgent → ~DatadogAgent() → DatadogAgent::flush() → (if trace chunks are waiting) telemetry::distribution::add() → TelemetryProxy& instance().

Moreover, agent reply callbacks happening during exit() hit the same problem.

Test

We extend the test created in #398. This test uses a dedicated binary with a Tracer instance that lives in a static wrapper.

The test failed as expected without the fix: CI with only the test commit.

References

Jira ticket: IDMPL-1240: [C++ Tracer] Tracer - Telemetry destruction order issue.

Contributor Checklist

  • I am the author of this pull request and I own it.
  • I reviewed every line of this pull request.
  • This pull request follows the Contributing Guidelines.

@pr-commenter

pr-commenter Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

Benchmarks

Benchmark execution time: 2026-11-17 12:42:21

Comparing candidate commit 56b2b0e in PR branch xlamorlette/fix-telemetry-destruction-bug with baseline commit f20b05f in branch main.

📊 Benchmarking dashboard

Found 0 performance improvements and 0 performance regressions! Performance is the same for 8 metrics, 0 unstable metrics.

Explanation

This is an A/B test comparing a candidate commit's performance against that of a baseline commit. Performance changes are noted in the tables below as:

  • 🟩 = significantly better candidate vs. baseline
  • 🟥 = significantly worse candidate vs. baseline

We compute a confidence interval (CI) over the relative difference of means between metrics from the candidate and baseline commits, considering the baseline as the reference.

If the CI is entirely outside the configured SIGNIFICANT_IMPACT_THRESHOLD (or the deprecated UNCONFIDENCE_THRESHOLD), the change is considered significant.

Feel free to reach out to #apm-benchmarking-platform on Slack if you have any questions.

More details about the CI and significant changes

You can imagine this CI as a range of values that is likely to contain the true difference of means between the candidate and baseline commits.

CIs of the difference of means are often centered around 0%, because often changes are not that big:

---------------------------------(------|---^--------)-------------------------------->
                              -0.6%    0%  0.3%     +1.2%
                                 |          |        |
         lower bound of the CI --'          |        |
sample mean (center of the CI) -------------'        |
         upper bound of the CI ----------------------'

As described above, a change is considered significant if the CI is entirely outside the configured SIGNIFICANT_IMPACT_THRESHOLD (or the deprecated UNCONFIDENCE_THRESHOLD).

For instance, for an execution time metric, this confidence interval indicates a significantly worse performance:

----------------------------------------|---------|---(---------^---------)---------->
                                       0%        1%  1.3%      2.2%      3.1%
                                                  |   |         |         |
       significant impact threshold --------------'   |         |         |
                      lower bound of CI --------------'         |         |
       sample mean (center of the CI) --------------------------'         |
                      upper bound of CI ----------------------------------'

@xlamorlette-datadog
xlamorlette-datadog force-pushed the xlamorlette/fix-telemetry-destruction-bug branch from 38291fc to 666b38f Compare November 13, 2026 19:04
@xlamorlette-datadog xlamorlette-datadog changed the title Test Tracer - Telementry destruction order issue Fix Tracer - Telementry destruction order issue Nov 13, 2026
@xlamorlette-datadog
xlamorlette-datadog marked this pull request as ready for review November 13, 2026 19:15
@xlamorlette-datadog
xlamorlette-datadog requested review from a team as code owners November 13, 2026 19:15
@xlamorlette-datadog
xlamorlette-datadog requested review from tabgok and removed request for a team November 13, 2026 19:15
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-13T19:16:54.750885Z 666b38f Draft marked ready
🔒 Security Review ✅ Completed 2026-11-13T19:18:50.474621Z 666b38f Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@xlamorlette-datadog xlamorlette-datadog changed the title Fix Tracer - Telementry destruction order issue Fix Tracer - Telemetry destruction order issue Nov 13, 2026

@datadog-prod-us1-6 datadog-prod-us1-6 Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bits Code Review: PASS

More details

Static review supports keeping telemetry accessible during late Tracer destruction while sending app-closing only once. Runtime shutdown behavior was not exercised.

Was this helpful? React 👍 or 👎

Open Bits AI session

🤖 Bits Code Review · Commit 666b38f

@zacharycmontoya zacharycmontoya left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, great fix!

Comment thread src/datadog/telemetry/telemetry.cpp Resolved
@xlamorlette-datadog
xlamorlette-datadog merged commit e07276e into main Nov 17, 2026
39 checks passed
@xlamorlette-datadog
xlamorlette-datadog deleted the xlamorlette/fix-telemetry-destruction-bug branch November 17, 2026 14:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants