Fix rust_unittests with asan, part 2 I fixed it in an earlier patchset of the previous commit and then accidentally rolled the fix back. Bug: 528225104 Change-Id: Id9fc94fda2ed8c410f8a71db3f6e253c6a6a6964 Reviewed-on: https://gn-review.googlesource.com/c/gn/+/25120 Commit-Queue: Matt Stark <msta@google.com> Reviewed-by: Takuto Ikuta <tikuta@google.com>
diff --git a/src/gn/ffi/test_with_scope.h b/src/gn/ffi/test_with_scope.h index eb16e90..db67e82 100644 --- a/src/gn/ffi/test_with_scope.h +++ b/src/gn/ffi/test_with_scope.h
@@ -5,18 +5,30 @@ #ifndef TOOLS_GN_FFI_TEST_WITH_SCOPE_H_ #define TOOLS_GN_FFI_TEST_WITH_SCOPE_H_ +#include <cstdlib> #include <memory> +#include <mutex> #include "base/command_line.h" #include "gn/scheduler.h" #include "gn/test_with_scope.h" inline std::unique_ptr<TestWithScope> NewTestWithScope() { - if (!base::CommandLine::InitializedForCurrentProcess()) { - int argc = 1; - const char* argv[] = {"gn_rust_tests", nullptr}; - base::CommandLine::Init(argc, argv); - } + static std::once_flag init_flag; + std::call_once(init_flag, []() { + if (!base::CommandLine::InitializedForCurrentProcess()) { + int argc = 1; + const char* argv[] = {"gn_rust_tests", nullptr}; + base::CommandLine::Init(argc, argv); + // Since this is used in rust, rather than trying to attach the lifetime + // to anything, we just attach it to the process itself. + std::atexit([]() { + if (base::CommandLine::InitializedForCurrentProcess()) { + base::CommandLine::Reset(); + } + }); + } + }); // Initialize a single Scheduler instance that lives for the lifetime of the // test process. This ensures g_scheduler is always set and never cleared // between tests.