Skip to content

Fix crash when process exits with a live Tracer in a static wrapper - #398

Merged
xlamorlette-datadog merged 3 commits into
mainfrom
xlamorlette/fix-tracer-destruction-bug
Nov 12, 2026
Merged

xlamorlette-datadog merged 3 commits into
mainfrom
xlamorlette/fix-tracer-destruction-bug

Conversation

@xlamorlette-datadog

@xlamorlette-datadog xlamorlette-datadog commented Nov 10, 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, by, on purpose, never destroy the StaticOtelCtxState singleton.

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.

Additional Notes

Detailed Bug Description

The Tracer constructor calls Tracer::store_config() which calls OtelCtxRegistration::publish() which calls get_otel_ctx_state().
get_otel_ctx_state() instantiates a static StaticOtelCtxState.
OtelCtxRegistration::publish() returns a unique_ptr<OtelCtxRegistration>, stored as a member of Tracer.
The OtelCtxRegistration destructor also calls get_otel_ctx_state().

When instantiating a Tracer, the StaticOtelCtxState's construction finishes before the Tracer's one. So, even if the Tracer is static, it is destroyed before the StaticOtelCtxState, which is fine.

But if one creates an empty static optional<Tracer>, and later fills it, the optional<Tracer> finishes construction first. So it will be destroyed after the static StaticOtelCtxState, which causes an issue because the Tracer destructor then tries to access the already destroyed StaticOtelCtxState.

Test

The new test needs a dedicated executable that reproduces the situation described above (a Tracer instance that lives in a static wrapper) and that is independent from other tests.

Without the fix, the new test fails as expected: https://github.com/DataDog/dd-trace-cpp/actions/runs/42879768486/job/130053552161?pr=398.

References

Jira ticket: IDMPL-1237: [C++ Tracer] Tracer - StaticOtelCtxState 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.

@xlamorlette-datadog xlamorlette-datadog changed the title Add test for bug when exit() with a live Tracer in a static wrapper Fix crash when process exits with a live Tracer in a static wrapper Nov 10, 2026
@pr-commenter

ghost commented Nov 10, 2026 •

Copy link
Copy Markdown

Benchmarks

Benchmark execution time: 2026-11-10 20:22:22

Comparing candidate commit 2d90c78 in PR branch xlamorlette/fix-tracer-destruction-bug with baseline commit 63849d6 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-tracer-destruction-bug branch from 17c014e to b5e2652 Compare November 10, 2026 19:28
@xlamorlette-datadog
xlamorlette-datadog force-pushed the xlamorlette/fix-tracer-destruction-bug branch from b5e2652 to a356d8e Compare November 10, 2026 19:30
@xlamorlette-datadog
xlamorlette-datadog marked this pull request as ready for review November 10, 2026 19:55
@xlamorlette-datadog
xlamorlette-datadog requested review from a team as code owners November 10, 2026 19:55
@xlamorlette-datadog
xlamorlette-datadog requested review from zacharycmontoya and removed request for a team November 10, 2026 19:55
@chatgpt-codex-connector

ghost commented Nov 10, 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-10T19:59:02.304029Z a356d8e Draft marked ready
🔒 Security Review ✅ Completed 2026-11-10T19:58:45.689371Z a356d8e 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.

ghost 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a356d8eddc

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread test/test_otel_process_ctx.cpp Resolved

ghost 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: FAIL

The new shutdown regression test fails when the build directory contains spaces because its helper path is passed unquoted to the shell. A localized shell-escaping replacement was verified.

Open Bits AI session

🤖 Bits Code Review · Commit a356d8e

Comment thread test/test_otel_process_ctx.cpp Resolved
@xlamorlette-datadog
xlamorlette-datadog force-pushed the xlamorlette/fix-tracer-destruction-bug branch from a356d8e to 7bcb122 Compare November 10, 2026 20:07

ghost 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: FAIL

The singleton lifetime fix appears sound, but the new shutdown test fails when the build directory contains spaces because its helper path is passed unquoted to a shell.

Open Bits AI session

🤖 Bits Code Review · Commit 7bcb122

Comment thread test/test_otel_process_ctx.cpp Resolved

ghost 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: FAIL

The PR keeps the process-context singleton alive to prevent shutdown crashes with statically wrapped tracers and adds a subprocess regression test. The update fixes helper paths containing spaces, but quoting remains incomplete: an apostrophe in the build path still prevents the helper from running, leaving one open test defect.

Open Bits AI session

🤖 Bits Code Review · Commit 2d90c78

Comment thread test/test_otel_process_ctx.cpp Resolved

ghost left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Some comments spotted by claude, nothing blocking, LGTM

Comment thread test/exit_with_live_tracer.cpp Resolved
Comment thread test/exit_with_live_tracer.cpp Resolved
Comment thread test/CMakeLists.txt Resolved
@xlamorlette-datadog
xlamorlette-datadog merged commit f20b05f into main Nov 12, 2026
@xlamorlette-datadog
xlamorlette-datadog deleted the xlamorlette/fix-tracer-destruction-bug branch November 12, 2026 09:58
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.

2 participants