Skip to content

Commit

Permalink
Fix potential data race in test_class_release_gil_before_calling_cpp_…
Browse files Browse the repository at this point in the history
…dtor.cpp

The original intent was to let the singleton leak, but making that tread-safe is slightly more involved than this solution. It's totally fine in this case if the RegistryType destructor runs on process teardown.

See also: #5522 (comment)
  • Loading branch information
rwgk committed Feb 15, 2025
1 parent 30bee61 commit ccf0163
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions tests/test_class_release_gil_before_calling_cpp_dtor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ namespace class_release_gil_before_calling_cpp_dtor {

using RegistryType = std::unordered_map<std::string, int>;

RegistryType &PyGILState_Check_Results() {
static auto *singleton = new RegistryType();
return *singleton;
static RegistryType &PyGILState_Check_Results() {
static RegistryType singleton; // Local static variables have thread-safe initialization.
return singleton;
}

template <int> // Using int as a trick to easily generate a series of types.
Expand Down

0 comments on commit ccf0163

Please sign in to comment.