Fix occasional crashes when running cargo ffi tests in parallel.

test_with_scope would previously spin up one scheduler per
test_with_scope, and destroy them upon test completion.

This meant that sometimes, g_scheduler would be set to null after one
test completes, causing another to fail.

Bug: 528225104
Change-Id: I89839a0ac5968281d33d72f0dd32fd326a6a6964
Reviewed-on: https://gn-review.googlesource.com/c/gn/+/24560
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 0fe2344..eb16e90 100644
--- a/src/gn/ffi/test_with_scope.h
+++ b/src/gn/ffi/test_with_scope.h
@@ -11,17 +11,17 @@
 #include "gn/scheduler.h"
 #include "gn/test_with_scope.h"
 
-struct TestWithScopeAndScheduler : public TestWithScope {
-  Scheduler scheduler;
-};
-
 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);
   }
-  return std::make_unique<TestWithScopeAndScheduler>();
+  // 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.
+  static Scheduler scheduler;
+  return std::make_unique<TestWithScope>();
 }
 
 #endif  // TOOLS_GN_FFI_TEST_WITH_SCOPE_H_