diff --git a/src/datadog/otel_process_ctx_registration.cpp b/src/datadog/otel_process_ctx_registration.cpp index 4f3cb29b..5eaac9be 100644 --- a/src/datadog/otel_process_ctx_registration.cpp +++ b/src/datadog/otel_process_ctx_registration.cpp @@ -28,7 +28,9 @@ struct StaticOtelCtxState { }; StaticOtelCtxState& get_otel_ctx_state() { - static StaticOtelCtxState state; + // The StaticOtelCtxState singleton is, on purpose, never destroyed, because + // the Tracer's destructor needs it. + static StaticOtelCtxState& state = *new StaticOtelCtxState; return state; } diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 2167866f..86a47e77 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -57,7 +57,7 @@ target_include_directories(tests ${CMAKE_SOURCE_DIR}/include/datadog ) -# Enable Catch2 std stringification +# Enable Catch2 std stringification # target_compile_definitions(tests PUBLIC @@ -73,12 +73,22 @@ target_link_libraries(tests if(DD_TRACE_TRANSPORT STREQUAL "curl") target_sources(tests PRIVATE test_curl.cpp) - target_link_libraries(tests - PRIVATE - # TODO: Remove dependency on libcurl - CURL::libcurl_static - ) + target_link_libraries(tests PRIVATE CURL::libcurl_static) endif() -catch_discover_tests(tests) +# Dedicated executable run by a test in test_otel_process_ctx.cpp +add_executable(exit_with_live_tracer exit_with_live_tracer.cpp) +target_link_libraries(exit_with_live_tracer + PRIVATE + dd-trace-cpp::static + dd-trace-cpp::specs +) + +add_dependencies(tests exit_with_live_tracer) +target_compile_definitions(tests + PRIVATE + EXIT_WITH_LIVE_TRACER_EXECUTABLE="$" +) + +catch_discover_tests(tests) diff --git a/test/exit_with_live_tracer.cpp b/test/exit_with_live_tracer.cpp new file mode 100644 index 00000000..312de979 --- /dev/null +++ b/test/exit_with_live_tracer.cpp @@ -0,0 +1,37 @@ +// Executable that calls exit() while a Tracer, held by a static wrapper, is +// still alive. + +#include +#include + +namespace dd = datadog::tracing; + +namespace { + +class NullCollector : public dd::Collector { + public: + dd::Expected send(std::vector>&&, + const std::shared_ptr&) override { + return dd::nullopt; + } + + std::string config() const override { return R"({"type": "NullCollector"})"; } +}; + +// Created empty before main(), so before the library statics. +std::optional tracer; + +} // namespace + +int main() { + dd::TracerConfig config; + config.collector = std::make_shared(); + config.telemetry.enabled = false; + const dd::Expected finalized_config = + dd::finalize_config(config); + if (!finalized_config) { + return EXIT_FAILURE; + } + tracer.emplace(*finalized_config); + std::exit(EXIT_SUCCESS); +} diff --git a/test/test_otel_process_ctx.cpp b/test/test_otel_process_ctx.cpp index d38aa053..aa6f6404 100644 --- a/test/test_otel_process_ctx.cpp +++ b/test/test_otel_process_ctx.cpp @@ -242,3 +242,17 @@ OTEL_CTX_TEST( } #endif } + +OTEL_CTX_TEST("exit() with a live Tracer in a static wrapper exits cleanly") { +#ifndef __linux__ + SUCCEED("OpenTelemetry process context is Linux-only"); +#else + // Run a dedicated process and check that it exits cleanly. It must be + // independent of other tests, to control the order of statics creation. + const std::string command = + std::string("'") + EXIT_WITH_LIVE_TRACER_EXECUTABLE + "'"; + const int status = std::system(command.c_str()); + REQUIRE(WIFEXITED(status)); + REQUIRE(WEXITSTATUS(status) == EXIT_SUCCESS); +#endif +}