Copilot commented on code in PR #3738:
URL: https://github.com/apache/brpc/pull/3738#discussion_r4545939844


##########
test/brpc_event_dispatcher_unittest.cpp:
##########
@@ -130,6 +130,55 @@ TEST_F(EventDispatcherTest, has_epollrdhup) {
     LOG(INFO) << brpc::has_epollrdhup;
 }
 
+TEST_F(EventDispatcherTest, start_stop_updates_running_state) {
+    brpc::EventDispatcher dispatcher;
+    ASSERT_EQ(0, dispatcher.Start(nullptr));
+    ASSERT_TRUE(dispatcher.Running());
+    dispatcher.Stop();
+    dispatcher.Join();
+    ASSERT_FALSE(dispatcher.Running());
+}
+
+struct DispatcherStopRaceArgs {
+    brpc::EventDispatcher* dispatcher;
+    butil::atomic<bool> reader_started;
+    butil::atomic<bool> reader_finished;
+
+    DispatcherStopRaceArgs()
+        : dispatcher(nullptr)
+        , reader_started(false)
+        , reader_finished(false) {}
+};
+
+void* poll_dispatcher_running(void* arg) {
+    DispatcherStopRaceArgs* args = static_cast<DispatcherStopRaceArgs*>(arg);
+    args->reader_started.store(true, butil::memory_order_release);
+    const int kRunningChecks = 100000;
+    for (int i = 0; i < kRunningChecks; ++i) {
+        args->dispatcher->Running();
+    }
+    args->reader_finished.store(true, butil::memory_order_release);
+    return nullptr;
+}
+
+// Under TSAN, this reports the Running()/Stop() race when _stop is volatile.
+TEST_F(EventDispatcherTest, stop_is_safe_with_concurrent_running) {
+    brpc::EventDispatcher dispatcher;
+    ASSERT_EQ(0, dispatcher.Start(nullptr));
+
+    DispatcherStopRaceArgs args;
+    args.dispatcher = &dispatcher;
+    pthread_t tid;
+    ASSERT_EQ(0, pthread_create(&tid, nullptr, poll_dispatcher_running,
+                                &args));
+    while (!args.reader_started.load(butil::memory_order_acquire)) {
+    }
+    dispatcher.Stop();

Review Comment:
   This does not reliably create the concurrent access it claims to regress: 
after `reader_started` is observed, the reader may finish all 100,000 
`Running()` calls before the test thread reaches `Stop()`, so TSAN can see no 
overlap and the old volatile implementation can pass. Keep the reader active 
until the stop operation is initiated (for example with a separate atomic 
stop-request flag/barrier), then join it after `Stop()` so the test 
deterministically exercises the race.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to