test: fix racy loop-variable capture in SubscriptionThreadSafety

- the subscriber threads captured the loop counter by reference while
  the spawning loop kept incrementing it: a genuine data race
- depending on timing, threads could also end up with duplicate
  subscriber names; capture the counter by value instead
This commit is contained in:
Dennis Klein
2026-06-10 16:12:35 +02:00
committed by Dennis Klein
parent f08d42fcb8
commit 19e607e486

View File

@@ -156,7 +156,7 @@ TEST_F(PluginServices, SubscriptionThreadSafety)
std::array<std::unique_ptr<std::thread>, subscribers> threads;
auto id = 0;
for (auto& thread : threads) {
thread = std::make_unique<std::thread>([&](){
thread = std::make_unique<std::thread>([&, id](){
auto const subscriber = fair::mq::tools::ToString("subscriber_", id);
for (auto i = 0; i < attempts; ++i) {
mServices.SubscribeToDeviceStateChange(subscriber, [](DeviceState){});