You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@brpc.apache.org by "chenBright (via GitHub)" <gi...@apache.org> on 2026/09/15 08:16:49 UTC
[PR] Stabilize timing-sensitive bthread tests and fix the butex interruption race (brpc)
chenBright opened a new pull request, #3545:
URL: https://github.com/apache/brpc/pull/3545
### What problem does this PR solve?
Issue Number: resolve
Problem Summary:
Running unit tests in parallel exposes timing assumptions that can cause intermittent failures:
- Fixed sleeps do not guarantee that workers have entered the expected waiting state.
- Tight elapsed-time assertions fail under normal scheduling delays.
- An increased connection count does not guarantee that an accepted socket has been published to the connection list.
- Some shutdown paths can lose wakeups or leave worker threads unjoined.
- An interruption between pthread butex waiter registration and the underlying wait can return an incorrect error code.
- Multi-tag tests compare prefilled zero values instead of validating the actual tags.
### What is changed and the side effects?
Changed:
- Replace fixed sleeps with explicit synchronization, state observation, and completion acknowledgements.
- Check timeout, interruption, wakeup, and completion semantics instead of narrow scheduling-time windows.
- Retain timeout guards and checks that waits do not complete prematurely.
- Fix the pthread butex interruption race while preserving genuine value-mismatch and timeout errors.
- Wait for accepted sockets to appear in the connection list before checking their buffer settings.
- Improve stop-flag synchronization, thread cleanup, and state reset for repeated test execution.
- Correct the result containers and assertions in multi-tag tests.
- Replace public-network connection dependencies with local TCP listeners and add a Linux-specific connection timeout case.
Side effects:
- Performance effects:
- Breaking backward compatibility:
---
### Check List:
- Please make sure your changes are compilable.
- When providing us with a new feature, it is best to add related tests.
- Please follow [Contributor Covenant Code of Conduct](https://github.com/apache/brpc/blob/master/CODE_OF_CONDUCT.md).
--
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: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] Stabilize timing-sensitive bthread tests and fix the butex interruption race (brpc)
Posted by "chenBright (via GitHub)" <gi...@apache.org>.
chenBright commented on code in PR #3545:
URL: https://github.com/apache/brpc/pull/3545#discussion_r4089278843
##########
test/bthread_timer_thread_unittest.cpp:
##########
@@ -98,59 +114,70 @@ class TimeKeeper {
keeper->run();
}
+ bool wait_finished() {
+ return WaitUntil([this] {
+ return _finished.load(butil::memory_order_acquire);
+ });
+ }
+
+ bool wait_started() {
+ return WaitUntil([this] {
+ return _started.load(butil::memory_order_acquire);
+ });
+ }
+
timespec _expect_run_time;
bthread::TimerThread::TaskId _task_id;
private:
const char* _name;
- int _sleep_ms;
+ butil::atomic<int> _sleep_ms;
+ butil::atomic<bool> _started{false};
+ butil::atomic<bool> _finished{false};
std::vector<timespec> _run_times;
};
TEST(TimerThreadTest, RunTasks) {
bthread::TimerThread timer_thread;
ASSERT_EQ(0, timer_thread.start(nullptr));
- timespec _2s_later = butil::seconds_from_now(2);
+ timespec _2s_later = butil::milliseconds_from_now(20);
TimeKeeper keeper1(_2s_later, "keeper1");
keeper1.schedule(&timer_thread);
- TimeKeeper keeper2(_2s_later, "keeper2"); // same time with keeper1
+ TimeKeeper keeper2(butil::seconds_from_now(3600), "keeper2");
keeper2.schedule(&timer_thread);
- timespec _1s_later = butil::seconds_from_now(1);
+ timespec _1s_later = butil::milliseconds_from_now(10);
TimeKeeper keeper3(_1s_later, "keeper3");
keeper3.schedule(&timer_thread);
- timespec _10s_later = butil::seconds_from_now(10);
+ timespec _10s_later = butil::seconds_from_now(3600);
Review Comment:
Same as above.
--
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: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] Stabilize timing-sensitive bthread tests and fix the butex interruption race (brpc)
Posted by "wasphin (via GitHub)" <gi...@apache.org>.
wasphin commented on code in PR #3545:
URL: https://github.com/apache/brpc/pull/3545#discussion_r4053417067
##########
test/bthread_timer_thread_unittest.cpp:
##########
@@ -98,59 +114,70 @@ class TimeKeeper {
keeper->run();
}
+ bool wait_finished() {
+ return WaitUntil([this] {
+ return _finished.load(butil::memory_order_acquire);
+ });
+ }
+
+ bool wait_started() {
+ return WaitUntil([this] {
+ return _started.load(butil::memory_order_acquire);
+ });
+ }
+
timespec _expect_run_time;
bthread::TimerThread::TaskId _task_id;
private:
const char* _name;
- int _sleep_ms;
+ butil::atomic<int> _sleep_ms;
+ butil::atomic<bool> _started{false};
+ butil::atomic<bool> _finished{false};
std::vector<timespec> _run_times;
};
TEST(TimerThreadTest, RunTasks) {
bthread::TimerThread timer_thread;
ASSERT_EQ(0, timer_thread.start(nullptr));
- timespec _2s_later = butil::seconds_from_now(2);
+ timespec _2s_later = butil::milliseconds_from_now(20);
TimeKeeper keeper1(_2s_later, "keeper1");
keeper1.schedule(&timer_thread);
- TimeKeeper keeper2(_2s_later, "keeper2"); // same time with keeper1
+ TimeKeeper keeper2(butil::seconds_from_now(3600), "keeper2");
keeper2.schedule(&timer_thread);
- timespec _1s_later = butil::seconds_from_now(1);
+ timespec _1s_later = butil::milliseconds_from_now(10);
TimeKeeper keeper3(_1s_later, "keeper3");
keeper3.schedule(&timer_thread);
- timespec _10s_later = butil::seconds_from_now(10);
+ timespec _10s_later = butil::seconds_from_now(3600);
TimeKeeper keeper4(_10s_later, "keeper4");
keeper4.schedule(&timer_thread);
TimeKeeper keeper5(_10s_later, "keeper5");
keeper5.schedule(&timer_thread);
- // sleep 1 second, and unschedule task2
- LOG(INFO) << "Sleep 1s";
- sleep(1);
- timer_thread.unschedule(keeper2._task_id);
- timer_thread.unschedule(keeper4._task_id);
+ ASSERT_EQ(0, timer_thread.unschedule(keeper2._task_id));
+ ASSERT_EQ(0, timer_thread.unschedule(keeper4._task_id));
timespec old_time = { 0, 0 };
TimeKeeper keeper6(old_time, "keeper6");
+ timespec keeper6_addtime = butil::seconds_from_now(0);
keeper6.schedule(&timer_thread);
- const timespec keeper6_addtime = butil::seconds_from_now(0);
- // sleep 10 seconds and stop.
- LOG(INFO) << "Sleep 2s";
- sleep(2);
+ ASSERT_TRUE(keeper1.wait_started());
Review Comment:
Please make this and the following `wait_started` checks non-fatal so the explicit `stop_and_join()` is always reached. `timer_thread` was declared before the `TimeKeeper` objects, so an early return destroys the callback arguments first and only then invokes `TimerThread::~TimerThread()`. A callback may start or still access a keeper during that window. `EXPECT_TRUE` preserves the failure while ensuring the timer thread is joined before the keepers are destroyed.
--
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: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] Stabilize timing-sensitive bthread tests and fix the butex interruption race (brpc)
Posted by "chenBright (via GitHub)" <gi...@apache.org>.
chenBright commented on code in PR #3545:
URL: https://github.com/apache/brpc/pull/3545#discussion_r4089275249
##########
test/bthread_fd_unittest.cpp:
##########
@@ -566,73 +621,74 @@ TEST(FDTest, double_close) {
ASSERT_EQ(ec, errno);
}
-const char* g_hostname1 = "github.com";
-const char* g_hostname2 = "baidu.com";
-TEST(FDTest, bthread_connect) {
- butil::EndPoint ep1;
- butil::EndPoint ep2;
- ASSERT_EQ(0, butil::hostname2endpoint(g_hostname1, 80, &ep1));
- ASSERT_EQ(0, butil::hostname2endpoint(g_hostname2, 80, &ep2));
-
- {
- struct sockaddr_storage serv_addr{};
- socklen_t serv_addr_size = 0;
- ASSERT_EQ(0, endpoint2sockaddr(ep1, &serv_addr, &serv_addr_size));
- butil::fd_guard sockfd(socket(serv_addr.ss_family, SOCK_STREAM, 0));
- ASSERT_LE(0, sockfd);
- bool is_blocking = butil::is_blocking(sockfd);
- ASSERT_LE(0, sockfd);
- ASSERT_EQ(0, bthread_connect(sockfd, (struct sockaddr*) &serv_addr, serv_addr_size));
- ASSERT_EQ(is_blocking, butil::is_blocking(sockfd));
+// Local listeners keep connect tests independent of DNS, Internet latency,
+// and the assumption that a connection cannot complete within one millisecond.
+void TestLocalConnect(bool timed) {
+ butil::EndPoint endpoint;
+ ASSERT_EQ(0, butil::str2endpoint("127.0.0.1:0", &endpoint));
+ butil::fd_guard listener(butil::tcp_listen(endpoint));
+ ASSERT_GE(listener, 0);
+ ASSERT_EQ(0, butil::get_local_side(listener, &endpoint));
+ struct sockaddr_storage address{};
+ socklen_t length = 0;
+ ASSERT_EQ(0, endpoint2sockaddr(endpoint, &address, &length));
+ butil::fd_guard client(socket(address.ss_family, SOCK_STREAM, 0));
+ ASSERT_GE(client, 0);
+ bool was_blocking = butil::is_blocking(client);
+ timespec deadline = butil::seconds_from_now(10);
+ int rc = bthread_timed_connect(
+ client, reinterpret_cast<sockaddr*>(&address), length,
+ timed ? &deadline : nullptr);
+ ASSERT_EQ(0, rc) << "errno=" << errno;
+ ASSERT_EQ(was_blocking, butil::is_blocking(client));
+ ASSERT_EQ(0, butil::is_connected(client));
+ // The handshake does not require a concurrent accept thread.
+ butil::fd_guard accepted(accept(listener, nullptr, nullptr));
+ ASSERT_GE(accepted, 0);
+}
- }
+TEST(FDTest, bthread_connect) {
+ TestLocalConnect(false);
+ TestLocalConnect(true);
+}
- {
- struct sockaddr_storage serv_addr{};
- socklen_t serv_addr_size = 0;
- ASSERT_EQ(0, endpoint2sockaddr(ep2, &serv_addr, &serv_addr_size));
- butil::fd_guard sockfd(socket(serv_addr.ss_family, SOCK_STREAM, 0));
- ASSERT_LE(0, sockfd);
- bool is_blocking = butil::is_blocking(sockfd);
- // In most cases, 1 millisecond will result in a connection timeout.
- timespec abstime = butil::milliseconds_from_now(1);
- const int rc = bthread_timed_connect(
- sockfd, (struct sockaddr*) &serv_addr,
- serv_addr_size, &abstime);
- ASSERT_EQ(-1, rc);
- ASSERT_EQ(ETIMEDOUT, errno);
- ASSERT_EQ(is_blocking, butil::is_blocking(sockfd));
- }
+#if defined(OS_LINUX)
+TEST(FDTest, connect_timeout_with_full_accept_queue) {
+ butil::EndPoint endpoint;
+ ASSERT_EQ(0, butil::str2endpoint("127.0.0.1:0", &endpoint));
+ butil::fd_guard listener(butil::tcp_listen(endpoint));
+ ASSERT_GE(listener, 0);
+ // Linux permits one queued connection for backlog=0. Leave it unaccepted
+ // so the next handshake cannot complete; no external network is needed.
+ ASSERT_EQ(0, listen(listener, 0));
+ ASSERT_EQ(0, butil::get_local_side(listener, &endpoint));
+ sockaddr_storage address{};
+ socklen_t length = 0;
+ ASSERT_EQ(0, endpoint2sockaddr(endpoint, &address, &length));
+ butil::fd_guard queued(socket(AF_INET, SOCK_STREAM, 0));
+ ASSERT_GE(queued, 0);
+ timespec setup_deadline = butil::seconds_from_now(5);
+ ASSERT_EQ(0, bthread_timed_connect(queued,
+ reinterpret_cast<sockaddr*>(&address), length, &setup_deadline));
+ butil::fd_guard client(socket(AF_INET, SOCK_STREAM, 0));
+ ASSERT_GE(client, 0);
+ timespec deadline = butil::milliseconds_from_now(50);
+ int rc = bthread_timed_connect(client,
+ reinterpret_cast<sockaddr*>(&address), length, &deadline);
+ int error = errno;
+ ASSERT_EQ(-1, rc);
+ ASSERT_EQ(ETIMEDOUT, error);
Review Comment:
Same as above.
##########
test/bthread_futex_unittest.cpp:
##########
@@ -113,30 +116,55 @@ TEST(FutexTest, futex_wake_before_wait) {
}
void* dummy_waiter(void* lock) {
- bthread::futex_wait_private(lock, 0, nullptr);
+ timespec timeout = butil::seconds_to_timespec(10);
+ int rc;
+ do {
+ rc = bthread::futex_wait_private(lock, 0, &timeout);
+ } while (rc != 0 && errno == EINTR);
+ EXPECT_EQ(0, rc);
return nullptr;
}
TEST(FutexTest, futex_wake_many_waiters_perf) {
- int lock1 = 0;
- size_t N = 0;
- pthread_t th;
- for (; N < 1000 && !pthread_create(&th, nullptr, dummy_waiter, &lock1); ++N) {}
-
- sleep(1);
+ butil::atomic<int> lock1(0);
+ std::vector<pthread_t> threads;
+ for (size_t i = 0; i < 1000; ++i) {
+ pthread_t th;
+ if (pthread_create(&th, nullptr, dummy_waiter, &lock1) != 0) {
+ break;
+ }
+ threads.push_back(th);
+ }
+ ASSERT_FALSE(threads.empty());
+ size_t N = threads.size();
int nwakeup = 0;
+ int64_t wake_ns = 0;
+ int64_t deadline = butil::cpuwide_time_us() + 5000000L;
butil::Timer tm;
- tm.start();
- for (size_t i = 0; i < N; ++i) {
- nwakeup += bthread::futex_wake_private(&lock1, 1);
+ while (static_cast<size_t>(nwakeup) < N &&
+ butil::cpuwide_time_us() < deadline) {
+ tm.start();
+ int rc = bthread::futex_wake_private(&lock1, 1);
+ tm.stop();
+ EXPECT_GE(rc, 0);
+ if (rc > 0) {
+ nwakeup += rc;
+ wake_ns += tm.n_elapsed();
+ } else {
+ usleep(1000);
+ }
}
- tm.stop();
- printf("N=%lu, futex_wake a thread = %" PRId64 "ns\n", N, tm.n_elapsed() / N);
- ASSERT_EQ(N, (size_t)nwakeup);
+ // Also release late waiters on failure; a wake alone is not persistent.
+ lock1.store(1);
+ bthread::futex_wake_private(&lock1, INT_MAX);
+ for (pthread_t th : threads) {
+ EXPECT_EQ(0, pthread_join(th, nullptr));
+ }
+ ASSERT_EQ(N, static_cast<size_t>(nwakeup));
+ printf("N=%lu, futex_wake a thread = %" PRId64 "ns\n", N, wake_ns / N);
Review Comment:
Fixed in 53373a5d52bed4a1f06c00bcc108002c31181818 . The waiter now accepts EWOULDBLOCK only after the controller has published cleanup and changed the futex value. Normal execution still requires a successful wake, so a late waiter no longer adds misleading failures while the registration failure remains reported.
--
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: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] Stabilize timing-sensitive bthread tests and fix the butex interruption race (brpc)
Posted by "chenBright (via GitHub)" <gi...@apache.org>.
chenBright commented on code in PR #3545:
URL: https://github.com/apache/brpc/pull/3545#discussion_r4089559904
##########
test/bthread_butex_multi_tag_unittest.cpp:
##########
@@ -132,15 +157,17 @@ void* butex_requeue_func2(void* arg) {
auto mutex = p->mutex;
auto cond = p->cond;
auto ack = p->ack;
- butex_wake_all_return2.push_back(bthread_self_tag());
+ butex_requeue_return2.push_back(bthread_self_tag());
std::unique_lock<bthread::Mutex> lk(*mutex);
ack->signal();
cond->wait(lk);
- butex_wake_all_return2.push_back(bthread_self_tag());
+ butex_requeue_return2.push_back(bthread_self_tag());
return nullptr;
}
TEST(BthreadButexMultiTest, butex_requeue) {
+ butex_requeue_return1.clear();
+ butex_requeue_return2.clear();
Review Comment:
Fixed in 6cd82a0020134cf368b9fa7dc61f203ffb2f30d0 .
--
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: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] Stabilize timing-sensitive bthread tests and fix the butex interruption race (brpc)
Posted by "chenBright (via GitHub)" <gi...@apache.org>.
chenBright commented on code in PR #3545:
URL: https://github.com/apache/brpc/pull/3545#discussion_r4089274474
##########
test/bthread_fd_unittest.cpp:
##########
@@ -566,73 +621,74 @@ TEST(FDTest, double_close) {
ASSERT_EQ(ec, errno);
}
-const char* g_hostname1 = "github.com";
-const char* g_hostname2 = "baidu.com";
-TEST(FDTest, bthread_connect) {
- butil::EndPoint ep1;
- butil::EndPoint ep2;
- ASSERT_EQ(0, butil::hostname2endpoint(g_hostname1, 80, &ep1));
- ASSERT_EQ(0, butil::hostname2endpoint(g_hostname2, 80, &ep2));
-
- {
- struct sockaddr_storage serv_addr{};
- socklen_t serv_addr_size = 0;
- ASSERT_EQ(0, endpoint2sockaddr(ep1, &serv_addr, &serv_addr_size));
- butil::fd_guard sockfd(socket(serv_addr.ss_family, SOCK_STREAM, 0));
- ASSERT_LE(0, sockfd);
- bool is_blocking = butil::is_blocking(sockfd);
- ASSERT_LE(0, sockfd);
- ASSERT_EQ(0, bthread_connect(sockfd, (struct sockaddr*) &serv_addr, serv_addr_size));
- ASSERT_EQ(is_blocking, butil::is_blocking(sockfd));
+// Local listeners keep connect tests independent of DNS, Internet latency,
+// and the assumption that a connection cannot complete within one millisecond.
+void TestLocalConnect(bool timed) {
+ butil::EndPoint endpoint;
+ ASSERT_EQ(0, butil::str2endpoint("127.0.0.1:0", &endpoint));
+ butil::fd_guard listener(butil::tcp_listen(endpoint));
+ ASSERT_GE(listener, 0);
+ ASSERT_EQ(0, butil::get_local_side(listener, &endpoint));
+ struct sockaddr_storage address{};
+ socklen_t length = 0;
+ ASSERT_EQ(0, endpoint2sockaddr(endpoint, &address, &length));
+ butil::fd_guard client(socket(address.ss_family, SOCK_STREAM, 0));
+ ASSERT_GE(client, 0);
+ bool was_blocking = butil::is_blocking(client);
+ timespec deadline = butil::seconds_from_now(10);
+ int rc = bthread_timed_connect(
+ client, reinterpret_cast<sockaddr*>(&address), length,
+ timed ? &deadline : nullptr);
+ ASSERT_EQ(0, rc) << "errno=" << errno;
+ ASSERT_EQ(was_blocking, butil::is_blocking(client));
+ ASSERT_EQ(0, butil::is_connected(client));
+ // The handshake does not require a concurrent accept thread.
+ butil::fd_guard accepted(accept(listener, nullptr, nullptr));
+ ASSERT_GE(accepted, 0);
+}
- }
+TEST(FDTest, bthread_connect) {
+ TestLocalConnect(false);
+ TestLocalConnect(true);
+}
- {
- struct sockaddr_storage serv_addr{};
- socklen_t serv_addr_size = 0;
- ASSERT_EQ(0, endpoint2sockaddr(ep2, &serv_addr, &serv_addr_size));
- butil::fd_guard sockfd(socket(serv_addr.ss_family, SOCK_STREAM, 0));
- ASSERT_LE(0, sockfd);
- bool is_blocking = butil::is_blocking(sockfd);
- // In most cases, 1 millisecond will result in a connection timeout.
- timespec abstime = butil::milliseconds_from_now(1);
- const int rc = bthread_timed_connect(
- sockfd, (struct sockaddr*) &serv_addr,
- serv_addr_size, &abstime);
- ASSERT_EQ(-1, rc);
- ASSERT_EQ(ETIMEDOUT, errno);
- ASSERT_EQ(is_blocking, butil::is_blocking(sockfd));
- }
+#if defined(OS_LINUX)
+TEST(FDTest, connect_timeout_with_full_accept_queue) {
Review Comment:
Fixed in 53373a5d52bed4a1f06c00bcc108002c31181818 . The full-accept-queue test now accepts both ETIMEDOUT and ECONNREFUSED. ECONNREFUSED is valid when Linux is configured with tcp_abort_on_overflow=1; the test still verifies that the second connection does not succeed and that the socket blocking mode is preserved.
##########
test/bthread_fd_unittest.cpp:
##########
@@ -566,73 +621,74 @@ TEST(FDTest, double_close) {
ASSERT_EQ(ec, errno);
}
-const char* g_hostname1 = "github.com";
-const char* g_hostname2 = "baidu.com";
-TEST(FDTest, bthread_connect) {
- butil::EndPoint ep1;
- butil::EndPoint ep2;
- ASSERT_EQ(0, butil::hostname2endpoint(g_hostname1, 80, &ep1));
- ASSERT_EQ(0, butil::hostname2endpoint(g_hostname2, 80, &ep2));
-
- {
- struct sockaddr_storage serv_addr{};
- socklen_t serv_addr_size = 0;
- ASSERT_EQ(0, endpoint2sockaddr(ep1, &serv_addr, &serv_addr_size));
- butil::fd_guard sockfd(socket(serv_addr.ss_family, SOCK_STREAM, 0));
- ASSERT_LE(0, sockfd);
- bool is_blocking = butil::is_blocking(sockfd);
- ASSERT_LE(0, sockfd);
- ASSERT_EQ(0, bthread_connect(sockfd, (struct sockaddr*) &serv_addr, serv_addr_size));
- ASSERT_EQ(is_blocking, butil::is_blocking(sockfd));
+// Local listeners keep connect tests independent of DNS, Internet latency,
+// and the assumption that a connection cannot complete within one millisecond.
+void TestLocalConnect(bool timed) {
+ butil::EndPoint endpoint;
+ ASSERT_EQ(0, butil::str2endpoint("127.0.0.1:0", &endpoint));
+ butil::fd_guard listener(butil::tcp_listen(endpoint));
+ ASSERT_GE(listener, 0);
+ ASSERT_EQ(0, butil::get_local_side(listener, &endpoint));
+ struct sockaddr_storage address{};
+ socklen_t length = 0;
+ ASSERT_EQ(0, endpoint2sockaddr(endpoint, &address, &length));
+ butil::fd_guard client(socket(address.ss_family, SOCK_STREAM, 0));
+ ASSERT_GE(client, 0);
+ bool was_blocking = butil::is_blocking(client);
+ timespec deadline = butil::seconds_from_now(10);
+ int rc = bthread_timed_connect(
+ client, reinterpret_cast<sockaddr*>(&address), length,
+ timed ? &deadline : nullptr);
+ ASSERT_EQ(0, rc) << "errno=" << errno;
+ ASSERT_EQ(was_blocking, butil::is_blocking(client));
+ ASSERT_EQ(0, butil::is_connected(client));
+ // The handshake does not require a concurrent accept thread.
+ butil::fd_guard accepted(accept(listener, nullptr, nullptr));
+ ASSERT_GE(accepted, 0);
+}
- }
+TEST(FDTest, bthread_connect) {
+ TestLocalConnect(false);
+ TestLocalConnect(true);
+}
- {
- struct sockaddr_storage serv_addr{};
- socklen_t serv_addr_size = 0;
- ASSERT_EQ(0, endpoint2sockaddr(ep2, &serv_addr, &serv_addr_size));
- butil::fd_guard sockfd(socket(serv_addr.ss_family, SOCK_STREAM, 0));
- ASSERT_LE(0, sockfd);
- bool is_blocking = butil::is_blocking(sockfd);
- // In most cases, 1 millisecond will result in a connection timeout.
- timespec abstime = butil::milliseconds_from_now(1);
- const int rc = bthread_timed_connect(
- sockfd, (struct sockaddr*) &serv_addr,
- serv_addr_size, &abstime);
- ASSERT_EQ(-1, rc);
- ASSERT_EQ(ETIMEDOUT, errno);
- ASSERT_EQ(is_blocking, butil::is_blocking(sockfd));
- }
+#if defined(OS_LINUX)
+TEST(FDTest, connect_timeout_with_full_accept_queue) {
+ butil::EndPoint endpoint;
+ ASSERT_EQ(0, butil::str2endpoint("127.0.0.1:0", &endpoint));
+ butil::fd_guard listener(butil::tcp_listen(endpoint));
+ ASSERT_GE(listener, 0);
+ // Linux permits one queued connection for backlog=0. Leave it unaccepted
+ // so the next handshake cannot complete; no external network is needed.
+ ASSERT_EQ(0, listen(listener, 0));
Review Comment:
Same as above.
--
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: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] Stabilize timing-sensitive bthread tests and fix the butex interruption race (brpc)
Posted by "wasphin (via GitHub)" <gi...@apache.org>.
wasphin commented on PR #3545:
URL: https://github.com/apache/brpc/pull/3545#issuecomment-5742341124
A general note for future PRs: since this project uses squash merges, please try to avoid combining multiple independent changes in a single PR. Keeping each PR focused and atomic makes the resulting commit easier to review, revert, backport, and maintain.
--
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: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] Stabilize timing-sensitive bthread tests and fix the butex interruption race (brpc)
Posted by "wasphin (via GitHub)" <gi...@apache.org>.
wasphin commented on code in PR #3545:
URL: https://github.com/apache/brpc/pull/3545#discussion_r4053422203
##########
test/bthread_timer_thread_unittest.cpp:
##########
@@ -98,59 +114,70 @@ class TimeKeeper {
keeper->run();
}
+ bool wait_finished() {
+ return WaitUntil([this] {
+ return _finished.load(butil::memory_order_acquire);
+ });
+ }
+
+ bool wait_started() {
+ return WaitUntil([this] {
+ return _started.load(butil::memory_order_acquire);
+ });
+ }
+
timespec _expect_run_time;
bthread::TimerThread::TaskId _task_id;
private:
const char* _name;
- int _sleep_ms;
+ butil::atomic<int> _sleep_ms;
+ butil::atomic<bool> _started{false};
+ butil::atomic<bool> _finished{false};
std::vector<timespec> _run_times;
};
TEST(TimerThreadTest, RunTasks) {
bthread::TimerThread timer_thread;
ASSERT_EQ(0, timer_thread.start(nullptr));
- timespec _2s_later = butil::seconds_from_now(2);
+ timespec _2s_later = butil::milliseconds_from_now(20);
TimeKeeper keeper1(_2s_later, "keeper1");
keeper1.schedule(&timer_thread);
- TimeKeeper keeper2(_2s_later, "keeper2"); // same time with keeper1
+ TimeKeeper keeper2(butil::seconds_from_now(3600), "keeper2");
keeper2.schedule(&timer_thread);
- timespec _1s_later = butil::seconds_from_now(1);
+ timespec _1s_later = butil::milliseconds_from_now(10);
TimeKeeper keeper3(_1s_later, "keeper3");
keeper3.schedule(&timer_thread);
- timespec _10s_later = butil::seconds_from_now(10);
+ timespec _10s_later = butil::seconds_from_now(3600);
TimeKeeper keeper4(_10s_later, "keeper4");
keeper4.schedule(&timer_thread);
TimeKeeper keeper5(_10s_later, "keeper5");
keeper5.schedule(&timer_thread);
- // sleep 1 second, and unschedule task2
- LOG(INFO) << "Sleep 1s";
- sleep(1);
- timer_thread.unschedule(keeper2._task_id);
- timer_thread.unschedule(keeper4._task_id);
+ ASSERT_EQ(0, timer_thread.unschedule(keeper2._task_id));
+ ASSERT_EQ(0, timer_thread.unschedule(keeper4._task_id));
timespec old_time = { 0, 0 };
TimeKeeper keeper6(old_time, "keeper6");
+ timespec keeper6_addtime = butil::seconds_from_now(0);
keeper6.schedule(&timer_thread);
- const timespec keeper6_addtime = butil::seconds_from_now(0);
- // sleep 10 seconds and stop.
- LOG(INFO) << "Sleep 2s";
- sleep(2);
+ ASSERT_TRUE(keeper1.wait_started());
+ ASSERT_TRUE(keeper3.wait_started());
+ ASSERT_TRUE(keeper6.wait_started());
Review Comment:
I agree that the remaining assertions are not meaningful after this timeout, but the concern is not merely a messy teardown. A fatal assertion starts stack unwinding immediately, and `timer_thread` was constructed before the `TimeKeeper` objects, so the keepers are destroyed before `TimerThread::~TimerThread()` joins the callback. A callback preempted before publishing `_started` can therefore resume with a dangling pointer. We can preserve fail-fast behavior by calling `timer_thread.stop_and_join()` first and then using `FAIL()`/`ASSERT_TRUE`, rather than continuing the rest of the test.
--
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: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] Stabilize timing-sensitive bthread tests and fix the butex interruption race (brpc)
Posted by "wwbmmm (via GitHub)" <gi...@apache.org>.
wwbmmm commented on PR #3545:
URL: https://github.com/apache/brpc/pull/3545#issuecomment-5810988139
LGTM
--
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: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] Stabilize timing-sensitive bthread tests and fix the butex interruption race (brpc)
Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #3545:
URL: https://github.com/apache/brpc/pull/3545#discussion_r4089303564
##########
src/bthread/butex.cpp:
##########
@@ -731,7 +731,13 @@ static int butex_wait_from_pthread(TaskGroup* g, Butex* b, int expected_value,
30/*nops before sched_yield*/);
if (task->interrupted) {
task->interrupted = false;
- if (rc == 0) {
+ // If interrupted after enqueueing but before futex_wait_private,
+ // pw.sig is already signalled and futex_wait_private may report
+ // EWOULDBLOCK. This is an interruption, not a value mismatch on
+ // the user's butex. Preserve other errors (notably ETIMEDOUT).
+ if (rc == 0 || (errno == EWOULDBLOCK &&
+ pw.sig.load(butil::memory_order_acquire) ==
+ PTHREAD_SIGNALLED)) {
Review Comment:
The new classification still reads and clears `task->interrupted` outside `task->version_lock`, while `TaskGroup::interrupt()` updates that field under the same lock. An interruption racing this epilogue is therefore a data race, and the clear can also consume an interruption being published concurrently. Hold `task->version_lock` across the check/clear and the `pw.sig` classification, as is already done during waiter publication above.
##########
test/bthread_butex_unittest.cpp:
##########
@@ -149,27 +163,27 @@ void* waiter(void* arg) {
TEST(ButexTest, sanity) {
const size_t N = 5;
WaiterArg args[N * 4];
- pthread_t t1, t2;
- butil::atomic<int>* b1 =
- bthread::butex_create_checked<butil::atomic<int> >();
+ pthread_t pthreads[2 * N];
+ bthread_t bthreads[2 * N];
+ butil::atomic<int>* b1 = bthread::butex_create_checked<butil::atomic<int> >();
ASSERT_TRUE(b1);
bthread::butex_destroy(b1);
Review Comment:
`b1` is returned to the butex object pool here, but the test immediately dereferences it and passes it to 20 waiters below. That is a use-after-destroy and can make the new waiter setup corrupt or hang; keep the butex alive until all joins complete, then destroy it at the end of the test.
--
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: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] Stabilize timing-sensitive bthread tests and fix the butex interruption race (brpc)
Posted by "chenBright (via GitHub)" <gi...@apache.org>.
chenBright commented on code in PR #3545:
URL: https://github.com/apache/brpc/pull/3545#discussion_r4082010648
##########
test/bthread_timer_thread_unittest.cpp:
##########
@@ -98,59 +114,70 @@ class TimeKeeper {
keeper->run();
}
+ bool wait_finished() {
+ return WaitUntil([this] {
+ return _finished.load(butil::memory_order_acquire);
+ });
+ }
+
+ bool wait_started() {
+ return WaitUntil([this] {
+ return _started.load(butil::memory_order_acquire);
+ });
+ }
+
timespec _expect_run_time;
bthread::TimerThread::TaskId _task_id;
private:
const char* _name;
- int _sleep_ms;
+ butil::atomic<int> _sleep_ms;
+ butil::atomic<bool> _started{false};
+ butil::atomic<bool> _finished{false};
std::vector<timespec> _run_times;
};
TEST(TimerThreadTest, RunTasks) {
bthread::TimerThread timer_thread;
ASSERT_EQ(0, timer_thread.start(nullptr));
- timespec _2s_later = butil::seconds_from_now(2);
+ timespec _2s_later = butil::milliseconds_from_now(20);
TimeKeeper keeper1(_2s_later, "keeper1");
keeper1.schedule(&timer_thread);
- TimeKeeper keeper2(_2s_later, "keeper2"); // same time with keeper1
+ TimeKeeper keeper2(butil::seconds_from_now(3600), "keeper2");
keeper2.schedule(&timer_thread);
- timespec _1s_later = butil::seconds_from_now(1);
+ timespec _1s_later = butil::milliseconds_from_now(10);
TimeKeeper keeper3(_1s_later, "keeper3");
keeper3.schedule(&timer_thread);
- timespec _10s_later = butil::seconds_from_now(10);
+ timespec _10s_later = butil::seconds_from_now(3600);
TimeKeeper keeper4(_10s_later, "keeper4");
keeper4.schedule(&timer_thread);
TimeKeeper keeper5(_10s_later, "keeper5");
keeper5.schedule(&timer_thread);
- // sleep 1 second, and unschedule task2
- LOG(INFO) << "Sleep 1s";
- sleep(1);
- timer_thread.unschedule(keeper2._task_id);
- timer_thread.unschedule(keeper4._task_id);
+ ASSERT_EQ(0, timer_thread.unschedule(keeper2._task_id));
+ ASSERT_EQ(0, timer_thread.unschedule(keeper4._task_id));
timespec old_time = { 0, 0 };
TimeKeeper keeper6(old_time, "keeper6");
+ timespec keeper6_addtime = butil::seconds_from_now(0);
keeper6.schedule(&timer_thread);
- const timespec keeper6_addtime = butil::seconds_from_now(0);
- // sleep 10 seconds and stop.
- LOG(INFO) << "Sleep 2s";
- sleep(2);
+ ASSERT_TRUE(keeper1.wait_started());
Review Comment:
Done.
--
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: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] Stabilize timing-sensitive bthread tests and fix the butex interruption race (brpc)
Posted by "chenBright (via GitHub)" <gi...@apache.org>.
chenBright commented on code in PR #3545:
URL: https://github.com/apache/brpc/pull/3545#discussion_r4089276723
##########
test/bthread_timer_thread_unittest.cpp:
##########
@@ -85,11 +101,11 @@ class TimeKeeper {
{
ASSERT_TRUE(!_run_times.empty());
long diff = timespec_diff_us(_run_times[0], expect_run_time);
- EXPECT_LE(labs(diff), 50000);
+ EXPECT_GE(diff, 0);
}
Review Comment:
Fixed in 53373a5d52bed4a1f06c00bcc108002c31181818 . expect_first_run() now keeps the no-early-run check and also applies a deliberately generous 10-second upper bound. This detects a stalled timer without restoring the former narrow scheduling-latency assertion.
##########
test/bthread_futex_unittest.cpp:
##########
@@ -113,30 +116,55 @@ TEST(FutexTest, futex_wake_before_wait) {
}
void* dummy_waiter(void* lock) {
- bthread::futex_wait_private(lock, 0, nullptr);
+ timespec timeout = butil::seconds_to_timespec(10);
+ int rc;
+ do {
+ rc = bthread::futex_wait_private(lock, 0, &timeout);
+ } while (rc != 0 && errno == EINTR);
+ EXPECT_EQ(0, rc);
return nullptr;
}
TEST(FutexTest, futex_wake_many_waiters_perf) {
- int lock1 = 0;
- size_t N = 0;
- pthread_t th;
- for (; N < 1000 && !pthread_create(&th, nullptr, dummy_waiter, &lock1); ++N) {}
-
- sleep(1);
+ butil::atomic<int> lock1(0);
+ std::vector<pthread_t> threads;
+ for (size_t i = 0; i < 1000; ++i) {
+ pthread_t th;
+ if (pthread_create(&th, nullptr, dummy_waiter, &lock1) != 0) {
+ break;
+ }
+ threads.push_back(th);
+ }
+ ASSERT_FALSE(threads.empty());
+ size_t N = threads.size();
int nwakeup = 0;
+ int64_t wake_ns = 0;
+ int64_t deadline = butil::cpuwide_time_us() + 5000000L;
butil::Timer tm;
- tm.start();
- for (size_t i = 0; i < N; ++i) {
- nwakeup += bthread::futex_wake_private(&lock1, 1);
+ while (static_cast<size_t>(nwakeup) < N &&
+ butil::cpuwide_time_us() < deadline) {
+ tm.start();
+ int rc = bthread::futex_wake_private(&lock1, 1);
+ tm.stop();
+ EXPECT_GE(rc, 0);
+ if (rc > 0) {
+ nwakeup += rc;
+ wake_ns += tm.n_elapsed();
+ } else {
+ usleep(1000);
+ }
}
- tm.stop();
- printf("N=%lu, futex_wake a thread = %" PRId64 "ns\n", N, tm.n_elapsed() / N);
- ASSERT_EQ(N, (size_t)nwakeup);
+ // Also release late waiters on failure; a wake alone is not persistent.
+ lock1.store(1);
+ bthread::futex_wake_private(&lock1, INT_MAX);
+ for (pthread_t th : threads) {
+ EXPECT_EQ(0, pthread_join(th, nullptr));
+ }
+ ASSERT_EQ(N, static_cast<size_t>(nwakeup));
+ printf("N=%lu, futex_wake a thread = %" PRId64 "ns\n", N, wake_ns / N);
Review Comment:
Fixed in 53373a5d52bed4a1f06c00bcc108002c31181818 . Each waiter now publishes a registration acknowledgement before entering futex_wait_private. The test waits for every created waiter before starting the wake measurement, has a bounded wake loop, and retains cleanup/join handling if either precondition times out.
--
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: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] Stabilize timing-sensitive bthread tests and fix the butex interruption race (brpc)
Posted by "chenBright (via GitHub)" <gi...@apache.org>.
chenBright merged PR #3545:
URL: https://github.com/apache/brpc/pull/3545
--
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: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] Stabilize timing-sensitive bthread tests and fix the butex interruption race (brpc)
Posted by "chenBright (via GitHub)" <gi...@apache.org>.
chenBright commented on code in PR #3545:
URL: https://github.com/apache/brpc/pull/3545#discussion_r4082006092
##########
test/bthread_fd_unittest.cpp:
##########
@@ -484,49 +514,73 @@ TEST(FDTest, invalid_epoll_events) {
ASSERT_EQ(0, bthread_fd_wait(fds[0], EVFILT_READ));
#endif
tm.stop();
- ASSERT_LT(tm.m_elapsed(), 20);
+ // Successful readiness, not scheduler latency, is the contract.
ASSERT_EQ(0, bthread_join(th, nullptr));
ASSERT_EQ(0, bthread_close(fds[0]));
}
+struct FDWaitArg {
+ int fd;
+ int timeout_ms;
+ int result = 0;
+ int error = 0;
+};
+
void* wait_for_the_fd(void* arg) {
- timespec ts = butil::milliseconds_from_now(50);
+ FDWaitArg* a = static_cast<FDWaitArg*>(arg);
+ timespec ts = butil::milliseconds_from_now(a->timeout_ms);
#if defined(OS_LINUX)
- bthread_fd_timedwait(*(int*)arg, EPOLLIN, &ts);
+ a->result = bthread_fd_timedwait(a->fd, EPOLLIN, &ts);
#elif defined(OS_MACOSX)
- bthread_fd_timedwait(*(int*)arg, EVFILT_READ, &ts);
+ a->result = bthread_fd_timedwait(a->fd, EVFILT_READ, &ts);
#endif
+ a->error = errno;
+ if (a->result == -1 && a->error == ETIMEDOUT) {
+ EXPECT_GE(butil::gettimeofday_us(), butil::timespec_to_microseconds(ts));
+ }
return nullptr;
}
TEST(FDTest, timeout) {
int fds[2];
ASSERT_EQ(0, pipe(fds));
+ FDWaitArg args[2];
+ for (auto& arg : args) {
+ arg.fd = fds[0];
+ arg.timeout_ms = 50;
+ }
pthread_t th;
- ASSERT_EQ(0, pthread_create(&th, nullptr, wait_for_the_fd, &fds[0]));
+ ASSERT_EQ(0, pthread_create(&th, nullptr, wait_for_the_fd, &args[0]));
bthread_t bth;
- ASSERT_EQ(0, bthread_start_urgent(&bth, nullptr, wait_for_the_fd, &fds[0]));
- butil::Timer tm;
- tm.start();
+ ASSERT_EQ(0, bthread_start_urgent(&bth, nullptr, wait_for_the_fd, &args[1]));
ASSERT_EQ(0, pthread_join(th, nullptr));
ASSERT_EQ(0, bthread_join(bth, nullptr));
- tm.stop();
- ASSERT_LT(tm.m_elapsed(), 80);
ASSERT_EQ(0, bthread_close(fds[0]));
ASSERT_EQ(0, bthread_close(fds[1]));
+ for (auto& arg : args) {
+ ASSERT_EQ(-1, arg.result);
+ ASSERT_EQ(ETIMEDOUT, arg.error);
+ }
}
TEST(FDTest, close_should_wakeup_waiter) {
int fds[2];
ASSERT_EQ(0, pipe(fds));
+ FDWaitArg arg;
+ arg.fd = fds[0];
+ arg.timeout_ms = 10000;
bthread_t bth;
- ASSERT_EQ(0, bthread_start_urgent(&bth, nullptr, wait_for_the_fd, &fds[0]));
- butil::Timer tm;
- tm.start();
+ ASSERT_EQ(0, bthread_start_urgent(&bth, nullptr, wait_for_the_fd, &arg));
+ auto* meta = bthread::TaskGroup::address_meta(bth);
+ int64_t deadline = butil::cpuwide_time_us() + 5000000L;
+ while (meta->current_waiter.load(butil::memory_order_acquire) == nullptr &&
+ butil::cpuwide_time_us() < deadline) {
+ bthread_usleep(1000);
+ }
+ ASSERT_NE(nullptr, meta->current_waiter.load(butil::memory_order_acquire));
Review Comment:
Done.
##########
test/bthread_unittest.cpp:
##########
@@ -501,21 +501,32 @@ TEST_F(BthreadTest, start_latency_when_high_idle) {
}
void* sleep_for_awhile_with_sleep(void* arg) {
- bthread_usleep((intptr_t)arg);
+ int rc = bthread_usleep((intptr_t)arg);
+ int error = errno;
+ EXPECT_EQ(-1, rc);
+ EXPECT_EQ(ESTOP, error);
return nullptr;
}
TEST_F(BthreadTest, stop_sleep) {
bthread_t th;
ASSERT_EQ(0, bthread_start_urgent(
- &th, nullptr, sleep_for_awhile_with_sleep, (void*)1000000L));
- butil::Timer tm;
- tm.start();
- bthread_usleep(10000);
+ &th, nullptr, sleep_for_awhile_with_sleep, (void*)60000000L));
+ auto* meta = bthread::TaskGroup::address_meta(th);
+ int64_t deadline = butil::cpuwide_time_us() + 5000000L;
+ bool sleeping = false;
+ do {
+ pthread_spin_lock(&meta->version_lock);
+ sleeping = (meta->current_sleep != 0);
+ pthread_spin_unlock(&meta->version_lock);
+ if (sleeping) {
+ break;
+ }
+ bthread_usleep(1000);
+ } while (butil::cpuwide_time_us() < deadline);
+ ASSERT_TRUE(sleeping);
Review Comment:
Done.
--
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: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] Stabilize timing-sensitive bthread tests and fix the butex interruption race (brpc)
Posted by "chenBright (via GitHub)" <gi...@apache.org>.
chenBright commented on code in PR #3545:
URL: https://github.com/apache/brpc/pull/3545#discussion_r4029016575
##########
test/bthread_timer_thread_unittest.cpp:
##########
@@ -98,59 +114,70 @@ class TimeKeeper {
keeper->run();
}
+ bool wait_finished() {
+ return WaitUntil([this] {
+ return _finished.load(butil::memory_order_acquire);
+ });
+ }
+
+ bool wait_started() {
+ return WaitUntil([this] {
+ return _started.load(butil::memory_order_acquire);
+ });
+ }
+
timespec _expect_run_time;
bthread::TimerThread::TaskId _task_id;
private:
const char* _name;
- int _sleep_ms;
+ butil::atomic<int> _sleep_ms;
+ butil::atomic<bool> _started{false};
+ butil::atomic<bool> _finished{false};
std::vector<timespec> _run_times;
};
TEST(TimerThreadTest, RunTasks) {
bthread::TimerThread timer_thread;
ASSERT_EQ(0, timer_thread.start(nullptr));
- timespec _2s_later = butil::seconds_from_now(2);
+ timespec _2s_later = butil::milliseconds_from_now(20);
TimeKeeper keeper1(_2s_later, "keeper1");
keeper1.schedule(&timer_thread);
- TimeKeeper keeper2(_2s_later, "keeper2"); // same time with keeper1
+ TimeKeeper keeper2(butil::seconds_from_now(3600), "keeper2");
keeper2.schedule(&timer_thread);
- timespec _1s_later = butil::seconds_from_now(1);
+ timespec _1s_later = butil::milliseconds_from_now(10);
TimeKeeper keeper3(_1s_later, "keeper3");
keeper3.schedule(&timer_thread);
- timespec _10s_later = butil::seconds_from_now(10);
+ timespec _10s_later = butil::seconds_from_now(3600);
TimeKeeper keeper4(_10s_later, "keeper4");
keeper4.schedule(&timer_thread);
TimeKeeper keeper5(_10s_later, "keeper5");
keeper5.schedule(&timer_thread);
- // sleep 1 second, and unschedule task2
- LOG(INFO) << "Sleep 1s";
- sleep(1);
- timer_thread.unschedule(keeper2._task_id);
- timer_thread.unschedule(keeper4._task_id);
+ ASSERT_EQ(0, timer_thread.unschedule(keeper2._task_id));
+ ASSERT_EQ(0, timer_thread.unschedule(keeper4._task_id));
timespec old_time = { 0, 0 };
TimeKeeper keeper6(old_time, "keeper6");
+ timespec keeper6_addtime = butil::seconds_from_now(0);
keeper6.schedule(&timer_thread);
- const timespec keeper6_addtime = butil::seconds_from_now(0);
- // sleep 10 seconds and stop.
- LOG(INFO) << "Sleep 2s";
- sleep(2);
+ ASSERT_TRUE(keeper1.wait_started());
+ ASSERT_TRUE(keeper3.wait_started());
+ ASSERT_TRUE(keeper6.wait_started());
Review Comment:
This only fires when the timer thread failed to run an already-due task within a 10s budget.
At that point the test has failed for a real reason and the remaining assertions are meaningless.
I'd rather keep it fatal and accept a messier teardown on an already-red run than let the test
continue in a known-broken state.
--
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: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] Stabilize timing-sensitive bthread tests and fix the butex interruption race (brpc)
Posted by "wasphin (via GitHub)" <gi...@apache.org>.
wasphin commented on code in PR #3545:
URL: https://github.com/apache/brpc/pull/3545#discussion_r4053404934
##########
test/bthread_fd_unittest.cpp:
##########
@@ -484,49 +514,73 @@ TEST(FDTest, invalid_epoll_events) {
ASSERT_EQ(0, bthread_fd_wait(fds[0], EVFILT_READ));
#endif
tm.stop();
- ASSERT_LT(tm.m_elapsed(), 20);
+ // Successful readiness, not scheduler latency, is the contract.
ASSERT_EQ(0, bthread_join(th, nullptr));
ASSERT_EQ(0, bthread_close(fds[0]));
}
+struct FDWaitArg {
+ int fd;
+ int timeout_ms;
+ int result = 0;
+ int error = 0;
+};
+
void* wait_for_the_fd(void* arg) {
- timespec ts = butil::milliseconds_from_now(50);
+ FDWaitArg* a = static_cast<FDWaitArg*>(arg);
+ timespec ts = butil::milliseconds_from_now(a->timeout_ms);
#if defined(OS_LINUX)
- bthread_fd_timedwait(*(int*)arg, EPOLLIN, &ts);
+ a->result = bthread_fd_timedwait(a->fd, EPOLLIN, &ts);
#elif defined(OS_MACOSX)
- bthread_fd_timedwait(*(int*)arg, EVFILT_READ, &ts);
+ a->result = bthread_fd_timedwait(a->fd, EVFILT_READ, &ts);
#endif
+ a->error = errno;
+ if (a->result == -1 && a->error == ETIMEDOUT) {
+ EXPECT_GE(butil::gettimeofday_us(), butil::timespec_to_microseconds(ts));
+ }
return nullptr;
}
TEST(FDTest, timeout) {
int fds[2];
ASSERT_EQ(0, pipe(fds));
+ FDWaitArg args[2];
+ for (auto& arg : args) {
+ arg.fd = fds[0];
+ arg.timeout_ms = 50;
+ }
pthread_t th;
- ASSERT_EQ(0, pthread_create(&th, nullptr, wait_for_the_fd, &fds[0]));
+ ASSERT_EQ(0, pthread_create(&th, nullptr, wait_for_the_fd, &args[0]));
bthread_t bth;
- ASSERT_EQ(0, bthread_start_urgent(&bth, nullptr, wait_for_the_fd, &fds[0]));
- butil::Timer tm;
- tm.start();
+ ASSERT_EQ(0, bthread_start_urgent(&bth, nullptr, wait_for_the_fd, &args[1]));
ASSERT_EQ(0, pthread_join(th, nullptr));
ASSERT_EQ(0, bthread_join(bth, nullptr));
- tm.stop();
- ASSERT_LT(tm.m_elapsed(), 80);
ASSERT_EQ(0, bthread_close(fds[0]));
ASSERT_EQ(0, bthread_close(fds[1]));
+ for (auto& arg : args) {
+ ASSERT_EQ(-1, arg.result);
+ ASSERT_EQ(ETIMEDOUT, arg.error);
+ }
}
TEST(FDTest, close_should_wakeup_waiter) {
int fds[2];
ASSERT_EQ(0, pipe(fds));
+ FDWaitArg arg;
+ arg.fd = fds[0];
+ arg.timeout_ms = 10000;
bthread_t bth;
- ASSERT_EQ(0, bthread_start_urgent(&bth, nullptr, wait_for_the_fd, &fds[0]));
- butil::Timer tm;
- tm.start();
+ ASSERT_EQ(0, bthread_start_urgent(&bth, nullptr, wait_for_the_fd, &arg));
+ auto* meta = bthread::TaskGroup::address_meta(bth);
+ int64_t deadline = butil::cpuwide_time_us() + 5000000L;
+ while (meta->current_waiter.load(butil::memory_order_acquire) == nullptr &&
+ butil::cpuwide_time_us() < deadline) {
+ bthread_usleep(1000);
+ }
+ ASSERT_NE(nullptr, meta->current_waiter.load(butil::memory_order_acquire));
Review Comment:
Please make this observation non-fatal so cleanup remains reachable. If registration is not observed before the deadline, `ASSERT_NE` skips `bthread_close`, `bthread_join`, and closing the other pipe end. The worker may still reference the stack-allocated `arg` after the test returns. Using `EXPECT_NE` allows the close/join path to run and the later result assertion to report any behavioral failure safely.
##########
test/bthread_mutex_unittest.cpp:
##########
@@ -52,8 +52,14 @@ TEST(MutexTest, sanity) {
ASSERT_EQ(1u, *get_butex(m));
bthread_t th1;
ASSERT_EQ(0, bthread_start_urgent(&th1, nullptr, locker, &m));
- usleep(5000); // wait for locker to run.
- ASSERT_EQ(257u, *get_butex(m)); // contention
+ auto* state = reinterpret_cast<butil::atomic<unsigned>*>(get_butex(m));
+ int64_t deadline = butil::cpuwide_time_us() + 5000000L;
+ while (state->load(butil::memory_order_relaxed) != 257u &&
+ butil::cpuwide_time_us() < deadline) {
+ usleep(1000);
+ }
+ // Keep cleanup reachable even if the worker did not run in time.
+ ASSERT_EQ(257u, state->load(butil::memory_order_relaxed));
Review Comment:
Please use `EXPECT_EQ` here instead of `ASSERT_EQ`. If the contended state is not observed before the deadline, this fatal assertion returns from the test with the mutex still locked and `th1` still blocked while referencing the stack-allocated mutex. A non-fatal assertion lets the existing unlock, join, and destroy cleanup run before the test reports the failure.
--
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: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] Stabilize timing-sensitive bthread tests and fix the butex interruption race (brpc)
Posted by "wasphin (via GitHub)" <gi...@apache.org>.
wasphin commented on code in PR #3545:
URL: https://github.com/apache/brpc/pull/3545#discussion_r4053411507
##########
test/bthread_unittest.cpp:
##########
@@ -501,21 +501,32 @@ TEST_F(BthreadTest, start_latency_when_high_idle) {
}
void* sleep_for_awhile_with_sleep(void* arg) {
- bthread_usleep((intptr_t)arg);
+ int rc = bthread_usleep((intptr_t)arg);
+ int error = errno;
+ EXPECT_EQ(-1, rc);
+ EXPECT_EQ(ESTOP, error);
return nullptr;
}
TEST_F(BthreadTest, stop_sleep) {
bthread_t th;
ASSERT_EQ(0, bthread_start_urgent(
- &th, nullptr, sleep_for_awhile_with_sleep, (void*)1000000L));
- butil::Timer tm;
- tm.start();
- bthread_usleep(10000);
+ &th, nullptr, sleep_for_awhile_with_sleep, (void*)60000000L));
+ auto* meta = bthread::TaskGroup::address_meta(th);
+ int64_t deadline = butil::cpuwide_time_us() + 5000000L;
+ bool sleeping = false;
+ do {
+ pthread_spin_lock(&meta->version_lock);
+ sleeping = (meta->current_sleep != 0);
+ pthread_spin_unlock(&meta->version_lock);
+ if (sleeping) {
+ break;
+ }
+ bthread_usleep(1000);
+ } while (butil::cpuwide_time_us() < deadline);
+ ASSERT_TRUE(sleeping);
Review Comment:
Please use a non-fatal assertion here and always execute `bthread_stop` and `bthread_join`. If registration is not observed before the deadline, this `ASSERT_TRUE` returns immediately and leaves the 60-second sleeper running. Stopping is still safe before `current_sleep` is published because bthread interruption is persistent and `_add_sleep_event` handles an already-interrupted task.
--
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: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] Stabilize timing-sensitive bthread tests and fix the butex interruption race (brpc)
Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #3545:
URL: https://github.com/apache/brpc/pull/3545#discussion_r4089497896
##########
test/bthread_butex_multi_tag_unittest.cpp:
##########
@@ -132,15 +157,17 @@ void* butex_requeue_func2(void* arg) {
auto mutex = p->mutex;
auto cond = p->cond;
auto ack = p->ack;
- butex_wake_all_return2.push_back(bthread_self_tag());
+ butex_requeue_return2.push_back(bthread_self_tag());
std::unique_lock<bthread::Mutex> lk(*mutex);
ack->signal();
cond->wait(lk);
- butex_wake_all_return2.push_back(bthread_self_tag());
+ butex_requeue_return2.push_back(bthread_self_tag());
return nullptr;
}
TEST(BthreadButexMultiTest, butex_requeue) {
+ butex_requeue_return1.clear();
+ butex_requeue_return2.clear();
Review Comment:
The requeue test still notifies immediately after `ack.wait()`, but both workers signal `ack` before calling `cond->wait()`. If either worker has not yet captured the condition sequence and registered its butex waiter, these notifications can be lost and the later `bthread_join` calls can block indefinitely. Add `WaitForWaiter(tid1)` and `WaitForWaiter(tid2)` after `ack.wait()` and before the first `notify_all()`, as done in the other multi-tag cases.
--
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: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] Stabilize timing-sensitive bthread tests and fix the butex interruption race (brpc)
Posted by "chenBright (via GitHub)" <gi...@apache.org>.
chenBright commented on code in PR #3545:
URL: https://github.com/apache/brpc/pull/3545#discussion_r4089277738
##########
test/bthread_timer_thread_unittest.cpp:
##########
@@ -98,59 +114,70 @@ class TimeKeeper {
keeper->run();
}
+ bool wait_finished() {
+ return WaitUntil([this] {
+ return _finished.load(butil::memory_order_acquire);
+ });
+ }
+
+ bool wait_started() {
+ return WaitUntil([this] {
+ return _started.load(butil::memory_order_acquire);
+ });
+ }
+
timespec _expect_run_time;
bthread::TimerThread::TaskId _task_id;
private:
const char* _name;
- int _sleep_ms;
+ butil::atomic<int> _sleep_ms;
+ butil::atomic<bool> _started{false};
+ butil::atomic<bool> _finished{false};
std::vector<timespec> _run_times;
};
TEST(TimerThreadTest, RunTasks) {
bthread::TimerThread timer_thread;
ASSERT_EQ(0, timer_thread.start(nullptr));
- timespec _2s_later = butil::seconds_from_now(2);
+ timespec _2s_later = butil::milliseconds_from_now(20);
Review Comment:
Fixed in 53373a5d52bed4a1f06c00bcc108002c31181818 . Renamed the RunTasks deadline variables so their names match the actual durations.
##########
test/bthread_timer_thread_unittest.cpp:
##########
@@ -98,59 +114,70 @@ class TimeKeeper {
keeper->run();
}
+ bool wait_finished() {
+ return WaitUntil([this] {
+ return _finished.load(butil::memory_order_acquire);
+ });
+ }
+
+ bool wait_started() {
+ return WaitUntil([this] {
+ return _started.load(butil::memory_order_acquire);
+ });
+ }
+
timespec _expect_run_time;
bthread::TimerThread::TaskId _task_id;
private:
const char* _name;
- int _sleep_ms;
+ butil::atomic<int> _sleep_ms;
+ butil::atomic<bool> _started{false};
+ butil::atomic<bool> _finished{false};
std::vector<timespec> _run_times;
};
TEST(TimerThreadTest, RunTasks) {
bthread::TimerThread timer_thread;
ASSERT_EQ(0, timer_thread.start(nullptr));
- timespec _2s_later = butil::seconds_from_now(2);
+ timespec _2s_later = butil::milliseconds_from_now(20);
TimeKeeper keeper1(_2s_later, "keeper1");
keeper1.schedule(&timer_thread);
- TimeKeeper keeper2(_2s_later, "keeper2"); // same time with keeper1
+ TimeKeeper keeper2(butil::seconds_from_now(3600), "keeper2");
keeper2.schedule(&timer_thread);
- timespec _1s_later = butil::seconds_from_now(1);
+ timespec _1s_later = butil::milliseconds_from_now(10);
Review Comment:
Same as above.
--
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: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] Stabilize timing-sensitive bthread tests and fix the butex interruption race (brpc)
Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #3545:
URL: https://github.com/apache/brpc/pull/3545#discussion_r4026191450
##########
test/bthread_timer_thread_unittest.cpp:
##########
@@ -98,59 +114,70 @@ class TimeKeeper {
keeper->run();
}
+ bool wait_finished() {
+ return WaitUntil([this] {
+ return _finished.load(butil::memory_order_acquire);
+ });
+ }
+
+ bool wait_started() {
+ return WaitUntil([this] {
+ return _started.load(butil::memory_order_acquire);
+ });
+ }
+
timespec _expect_run_time;
bthread::TimerThread::TaskId _task_id;
private:
const char* _name;
- int _sleep_ms;
+ butil::atomic<int> _sleep_ms;
+ butil::atomic<bool> _started{false};
+ butil::atomic<bool> _finished{false};
std::vector<timespec> _run_times;
};
TEST(TimerThreadTest, RunTasks) {
bthread::TimerThread timer_thread;
ASSERT_EQ(0, timer_thread.start(nullptr));
- timespec _2s_later = butil::seconds_from_now(2);
+ timespec _2s_later = butil::milliseconds_from_now(20);
TimeKeeper keeper1(_2s_later, "keeper1");
keeper1.schedule(&timer_thread);
- TimeKeeper keeper2(_2s_later, "keeper2"); // same time with keeper1
+ TimeKeeper keeper2(butil::seconds_from_now(3600), "keeper2");
keeper2.schedule(&timer_thread);
- timespec _1s_later = butil::seconds_from_now(1);
+ timespec _1s_later = butil::milliseconds_from_now(10);
TimeKeeper keeper3(_1s_later, "keeper3");
keeper3.schedule(&timer_thread);
- timespec _10s_later = butil::seconds_from_now(10);
+ timespec _10s_later = butil::seconds_from_now(3600);
TimeKeeper keeper4(_10s_later, "keeper4");
keeper4.schedule(&timer_thread);
TimeKeeper keeper5(_10s_later, "keeper5");
keeper5.schedule(&timer_thread);
- // sleep 1 second, and unschedule task2
- LOG(INFO) << "Sleep 1s";
- sleep(1);
- timer_thread.unschedule(keeper2._task_id);
- timer_thread.unschedule(keeper4._task_id);
+ ASSERT_EQ(0, timer_thread.unschedule(keeper2._task_id));
+ ASSERT_EQ(0, timer_thread.unschedule(keeper4._task_id));
timespec old_time = { 0, 0 };
TimeKeeper keeper6(old_time, "keeper6");
+ timespec keeper6_addtime = butil::seconds_from_now(0);
keeper6.schedule(&timer_thread);
- const timespec keeper6_addtime = butil::seconds_from_now(0);
- // sleep 10 seconds and stop.
- LOG(INFO) << "Sleep 2s";
- sleep(2);
+ ASSERT_TRUE(keeper1.wait_started());
+ ASSERT_TRUE(keeper3.wait_started());
+ ASSERT_TRUE(keeper6.wait_started());
Review Comment:
These fatal assertions can return before `timer_thread.stop_and_join()` runs. If a callback is preempted after starting but before publishing its `_started` flag, the 10-second wait can fail while it still holds a pointer to a stack `TimeKeeper`; destruction order destroys the keepers before `TimerThread` joins the callback. Use non-fatal checks here (or an earlier cleanup guard) so the timer is joined before the keepers leave scope.
##########
test/bthread_fd_unittest.cpp:
##########
@@ -411,39 +411,62 @@ TEST(FDTest, add_existing_fd) {
#endif
}
+struct EpollWaitArg {
+ int epfd;
+ butil::atomic<bool> done{false};
+ int result = 0;
+ int error = 0;
+};
+
void* epoll_waiter(void* arg) {
+ EpollWaitArg* a = static_cast<EpollWaitArg*>(arg);
#if defined(OS_LINUX)
epoll_event e;
- if (1 == epoll_wait((int)(intptr_t)arg, &e, 1, -1)) {
- std::cout << e.events << std::endl;
- }
+ a->result = epoll_wait(a->epfd, &e, 1, 10000);
#elif defined(OS_MACOSX)
struct kevent e;
- if (1 == kevent((int)(intptr_t)arg, nullptr, 0, &e, 1, nullptr)) {
- std::cout << e.flags << std::endl;
- }
+ timespec timeout = {10, 0};
+ a->result = kevent(a->epfd, nullptr, 0, &e, 1, &timeout);
#endif
- std::cout << pthread_self() << " quits" << std::endl;
+ a->error = errno;
+ a->done.store(true, butil::memory_order_release);
return nullptr;
}
TEST(FDTest, interrupt_pthread) {
#if defined(OS_LINUX)
- const int epfd = epoll_create(1024);
+ butil::fd_guard epfd(epoll_create(1024));
#elif defined(OS_MACOSX)
- const int epfd = kqueue();
+ butil::fd_guard epfd(kqueue());
#endif
- pthread_t th, th2;
- ASSERT_EQ(0, pthread_create(&th, nullptr, epoll_waiter, (void*)(intptr_t)epfd));
- ASSERT_EQ(0, pthread_create(&th2, nullptr, epoll_waiter, (void*)(intptr_t)epfd));
- bthread_usleep(100000L);
- std::cout << "wake up " << th << std::endl;
- bthread::interrupt_pthread(th);
- bthread_usleep(100000L);
- std::cout << "wake up " << th2 << std::endl;
- bthread::interrupt_pthread(th2);
- pthread_join(th, nullptr);
- pthread_join(th2, nullptr);
+ ASSERT_GE(epfd, 0);
+ EpollWaitArg args[2];
+ pthread_t threads[2];
+ size_t started = 0;
+ for (; started < ARRAY_SIZE(threads); ++started) {
+ args[started].epfd = epfd;
+ int rc = pthread_create(&threads[started], nullptr,
+ epoll_waiter, &args[started]);
+ EXPECT_EQ(0, rc);
+ if (rc != 0) {
+ break;
+ }
+ }
+ int64_t deadline = butil::cpuwide_time_us() + 15000000L;
+ for (size_t i = 0; i < started; ++i) {
+ // Signals are not persistent. Retry until the syscall observes one;
+ // keep the pthread joinable until all signalling is finished.
+ while (!args[i].done.load(butil::memory_order_acquire) &&
+ butil::cpuwide_time_us() < deadline) {
+ EXPECT_EQ(0, bthread::interrupt_pthread(threads[i]));
+ bthread_usleep(1000);
Review Comment:
`done` is checked before this call, so the waiter can finish and exit in that window; `pthread_kill` then returns `ESRCH` even though the interruption succeeded by no longer being necessary, and this `EXPECT_EQ` records a flaky failure. Treat `ESRCH` as the terminal completion race and let the final result assertions validate why the waiter exited.
--
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: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] Stabilize timing-sensitive bthread tests and fix the butex interruption race (brpc)
Posted by "chenBright (via GitHub)" <gi...@apache.org>.
chenBright commented on code in PR #3545:
URL: https://github.com/apache/brpc/pull/3545#discussion_r4029004081
##########
test/bthread_fd_unittest.cpp:
##########
@@ -411,39 +411,62 @@ TEST(FDTest, add_existing_fd) {
#endif
}
+struct EpollWaitArg {
+ int epfd;
+ butil::atomic<bool> done{false};
+ int result = 0;
+ int error = 0;
+};
+
void* epoll_waiter(void* arg) {
+ EpollWaitArg* a = static_cast<EpollWaitArg*>(arg);
#if defined(OS_LINUX)
epoll_event e;
- if (1 == epoll_wait((int)(intptr_t)arg, &e, 1, -1)) {
- std::cout << e.events << std::endl;
- }
+ a->result = epoll_wait(a->epfd, &e, 1, 10000);
#elif defined(OS_MACOSX)
struct kevent e;
- if (1 == kevent((int)(intptr_t)arg, nullptr, 0, &e, 1, nullptr)) {
- std::cout << e.flags << std::endl;
- }
+ timespec timeout = {10, 0};
+ a->result = kevent(a->epfd, nullptr, 0, &e, 1, &timeout);
#endif
- std::cout << pthread_self() << " quits" << std::endl;
+ a->error = errno;
+ a->done.store(true, butil::memory_order_release);
return nullptr;
}
TEST(FDTest, interrupt_pthread) {
#if defined(OS_LINUX)
- const int epfd = epoll_create(1024);
+ butil::fd_guard epfd(epoll_create(1024));
#elif defined(OS_MACOSX)
- const int epfd = kqueue();
+ butil::fd_guard epfd(kqueue());
#endif
- pthread_t th, th2;
- ASSERT_EQ(0, pthread_create(&th, nullptr, epoll_waiter, (void*)(intptr_t)epfd));
- ASSERT_EQ(0, pthread_create(&th2, nullptr, epoll_waiter, (void*)(intptr_t)epfd));
- bthread_usleep(100000L);
- std::cout << "wake up " << th << std::endl;
- bthread::interrupt_pthread(th);
- bthread_usleep(100000L);
- std::cout << "wake up " << th2 << std::endl;
- bthread::interrupt_pthread(th2);
- pthread_join(th, nullptr);
- pthread_join(th2, nullptr);
+ ASSERT_GE(epfd, 0);
+ EpollWaitArg args[2];
+ pthread_t threads[2];
+ size_t started = 0;
+ for (; started < ARRAY_SIZE(threads); ++started) {
+ args[started].epfd = epfd;
+ int rc = pthread_create(&threads[started], nullptr,
+ epoll_waiter, &args[started]);
+ EXPECT_EQ(0, rc);
+ if (rc != 0) {
+ break;
+ }
+ }
+ int64_t deadline = butil::cpuwide_time_us() + 15000000L;
+ for (size_t i = 0; i < started; ++i) {
+ // Signals are not persistent. Retry until the syscall observes one;
+ // keep the pthread joinable until all signalling is finished.
+ while (!args[i].done.load(butil::memory_order_acquire) &&
+ butil::cpuwide_time_us() < deadline) {
+ EXPECT_EQ(0, bthread::interrupt_pthread(threads[i]));
+ bthread_usleep(1000);
Review Comment:
Fixed in a62c47d217b023f12b75bf2f9319c16da98f7d11.
--
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: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] Stabilize timing-sensitive bthread tests and fix the butex interruption race (brpc)
Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #3545:
URL: https://github.com/apache/brpc/pull/3545#discussion_r4083039457
##########
test/bthread_fd_unittest.cpp:
##########
@@ -566,73 +621,74 @@ TEST(FDTest, double_close) {
ASSERT_EQ(ec, errno);
}
-const char* g_hostname1 = "github.com";
-const char* g_hostname2 = "baidu.com";
-TEST(FDTest, bthread_connect) {
- butil::EndPoint ep1;
- butil::EndPoint ep2;
- ASSERT_EQ(0, butil::hostname2endpoint(g_hostname1, 80, &ep1));
- ASSERT_EQ(0, butil::hostname2endpoint(g_hostname2, 80, &ep2));
-
- {
- struct sockaddr_storage serv_addr{};
- socklen_t serv_addr_size = 0;
- ASSERT_EQ(0, endpoint2sockaddr(ep1, &serv_addr, &serv_addr_size));
- butil::fd_guard sockfd(socket(serv_addr.ss_family, SOCK_STREAM, 0));
- ASSERT_LE(0, sockfd);
- bool is_blocking = butil::is_blocking(sockfd);
- ASSERT_LE(0, sockfd);
- ASSERT_EQ(0, bthread_connect(sockfd, (struct sockaddr*) &serv_addr, serv_addr_size));
- ASSERT_EQ(is_blocking, butil::is_blocking(sockfd));
+// Local listeners keep connect tests independent of DNS, Internet latency,
+// and the assumption that a connection cannot complete within one millisecond.
+void TestLocalConnect(bool timed) {
+ butil::EndPoint endpoint;
+ ASSERT_EQ(0, butil::str2endpoint("127.0.0.1:0", &endpoint));
+ butil::fd_guard listener(butil::tcp_listen(endpoint));
+ ASSERT_GE(listener, 0);
+ ASSERT_EQ(0, butil::get_local_side(listener, &endpoint));
+ struct sockaddr_storage address{};
+ socklen_t length = 0;
+ ASSERT_EQ(0, endpoint2sockaddr(endpoint, &address, &length));
+ butil::fd_guard client(socket(address.ss_family, SOCK_STREAM, 0));
+ ASSERT_GE(client, 0);
+ bool was_blocking = butil::is_blocking(client);
+ timespec deadline = butil::seconds_from_now(10);
+ int rc = bthread_timed_connect(
+ client, reinterpret_cast<sockaddr*>(&address), length,
+ timed ? &deadline : nullptr);
+ ASSERT_EQ(0, rc) << "errno=" << errno;
+ ASSERT_EQ(was_blocking, butil::is_blocking(client));
+ ASSERT_EQ(0, butil::is_connected(client));
+ // The handshake does not require a concurrent accept thread.
+ butil::fd_guard accepted(accept(listener, nullptr, nullptr));
+ ASSERT_GE(accepted, 0);
+}
- }
+TEST(FDTest, bthread_connect) {
+ TestLocalConnect(false);
+ TestLocalConnect(true);
+}
- {
- struct sockaddr_storage serv_addr{};
- socklen_t serv_addr_size = 0;
- ASSERT_EQ(0, endpoint2sockaddr(ep2, &serv_addr, &serv_addr_size));
- butil::fd_guard sockfd(socket(serv_addr.ss_family, SOCK_STREAM, 0));
- ASSERT_LE(0, sockfd);
- bool is_blocking = butil::is_blocking(sockfd);
- // In most cases, 1 millisecond will result in a connection timeout.
- timespec abstime = butil::milliseconds_from_now(1);
- const int rc = bthread_timed_connect(
- sockfd, (struct sockaddr*) &serv_addr,
- serv_addr_size, &abstime);
- ASSERT_EQ(-1, rc);
- ASSERT_EQ(ETIMEDOUT, errno);
- ASSERT_EQ(is_blocking, butil::is_blocking(sockfd));
- }
+#if defined(OS_LINUX)
+TEST(FDTest, connect_timeout_with_full_accept_queue) {
Review Comment:
Asserting strictly `ETIMEDOUT` can be environment-dependent on Linux. For example, if `net.ipv4.tcp_abort_on_overflow=1`, overflowing the accept queue may produce an immediate `ECONNREFUSED` (RST) instead of a timeout. To keep this test portable across CI hosts, consider accepting either `ETIMEDOUT` or `ECONNREFUSED` (or reading the sysctl and skipping/branching accordingly).
##########
test/bthread_fd_unittest.cpp:
##########
@@ -566,73 +621,74 @@ TEST(FDTest, double_close) {
ASSERT_EQ(ec, errno);
}
-const char* g_hostname1 = "github.com";
-const char* g_hostname2 = "baidu.com";
-TEST(FDTest, bthread_connect) {
- butil::EndPoint ep1;
- butil::EndPoint ep2;
- ASSERT_EQ(0, butil::hostname2endpoint(g_hostname1, 80, &ep1));
- ASSERT_EQ(0, butil::hostname2endpoint(g_hostname2, 80, &ep2));
-
- {
- struct sockaddr_storage serv_addr{};
- socklen_t serv_addr_size = 0;
- ASSERT_EQ(0, endpoint2sockaddr(ep1, &serv_addr, &serv_addr_size));
- butil::fd_guard sockfd(socket(serv_addr.ss_family, SOCK_STREAM, 0));
- ASSERT_LE(0, sockfd);
- bool is_blocking = butil::is_blocking(sockfd);
- ASSERT_LE(0, sockfd);
- ASSERT_EQ(0, bthread_connect(sockfd, (struct sockaddr*) &serv_addr, serv_addr_size));
- ASSERT_EQ(is_blocking, butil::is_blocking(sockfd));
+// Local listeners keep connect tests independent of DNS, Internet latency,
+// and the assumption that a connection cannot complete within one millisecond.
+void TestLocalConnect(bool timed) {
+ butil::EndPoint endpoint;
+ ASSERT_EQ(0, butil::str2endpoint("127.0.0.1:0", &endpoint));
+ butil::fd_guard listener(butil::tcp_listen(endpoint));
+ ASSERT_GE(listener, 0);
+ ASSERT_EQ(0, butil::get_local_side(listener, &endpoint));
+ struct sockaddr_storage address{};
+ socklen_t length = 0;
+ ASSERT_EQ(0, endpoint2sockaddr(endpoint, &address, &length));
+ butil::fd_guard client(socket(address.ss_family, SOCK_STREAM, 0));
+ ASSERT_GE(client, 0);
+ bool was_blocking = butil::is_blocking(client);
+ timespec deadline = butil::seconds_from_now(10);
+ int rc = bthread_timed_connect(
+ client, reinterpret_cast<sockaddr*>(&address), length,
+ timed ? &deadline : nullptr);
+ ASSERT_EQ(0, rc) << "errno=" << errno;
+ ASSERT_EQ(was_blocking, butil::is_blocking(client));
+ ASSERT_EQ(0, butil::is_connected(client));
+ // The handshake does not require a concurrent accept thread.
+ butil::fd_guard accepted(accept(listener, nullptr, nullptr));
+ ASSERT_GE(accepted, 0);
+}
- }
+TEST(FDTest, bthread_connect) {
+ TestLocalConnect(false);
+ TestLocalConnect(true);
+}
- {
- struct sockaddr_storage serv_addr{};
- socklen_t serv_addr_size = 0;
- ASSERT_EQ(0, endpoint2sockaddr(ep2, &serv_addr, &serv_addr_size));
- butil::fd_guard sockfd(socket(serv_addr.ss_family, SOCK_STREAM, 0));
- ASSERT_LE(0, sockfd);
- bool is_blocking = butil::is_blocking(sockfd);
- // In most cases, 1 millisecond will result in a connection timeout.
- timespec abstime = butil::milliseconds_from_now(1);
- const int rc = bthread_timed_connect(
- sockfd, (struct sockaddr*) &serv_addr,
- serv_addr_size, &abstime);
- ASSERT_EQ(-1, rc);
- ASSERT_EQ(ETIMEDOUT, errno);
- ASSERT_EQ(is_blocking, butil::is_blocking(sockfd));
- }
+#if defined(OS_LINUX)
+TEST(FDTest, connect_timeout_with_full_accept_queue) {
+ butil::EndPoint endpoint;
+ ASSERT_EQ(0, butil::str2endpoint("127.0.0.1:0", &endpoint));
+ butil::fd_guard listener(butil::tcp_listen(endpoint));
+ ASSERT_GE(listener, 0);
+ // Linux permits one queued connection for backlog=0. Leave it unaccepted
+ // so the next handshake cannot complete; no external network is needed.
+ ASSERT_EQ(0, listen(listener, 0));
+ ASSERT_EQ(0, butil::get_local_side(listener, &endpoint));
+ sockaddr_storage address{};
+ socklen_t length = 0;
+ ASSERT_EQ(0, endpoint2sockaddr(endpoint, &address, &length));
+ butil::fd_guard queued(socket(AF_INET, SOCK_STREAM, 0));
+ ASSERT_GE(queued, 0);
+ timespec setup_deadline = butil::seconds_from_now(5);
+ ASSERT_EQ(0, bthread_timed_connect(queued,
+ reinterpret_cast<sockaddr*>(&address), length, &setup_deadline));
+ butil::fd_guard client(socket(AF_INET, SOCK_STREAM, 0));
+ ASSERT_GE(client, 0);
+ timespec deadline = butil::milliseconds_from_now(50);
+ int rc = bthread_timed_connect(client,
+ reinterpret_cast<sockaddr*>(&address), length, &deadline);
+ int error = errno;
+ ASSERT_EQ(-1, rc);
+ ASSERT_EQ(ETIMEDOUT, error);
Review Comment:
Asserting strictly `ETIMEDOUT` can be environment-dependent on Linux. For example, if `net.ipv4.tcp_abort_on_overflow=1`, overflowing the accept queue may produce an immediate `ECONNREFUSED` (RST) instead of a timeout. To keep this test portable across CI hosts, consider accepting either `ETIMEDOUT` or `ECONNREFUSED` (or reading the sysctl and skipping/branching accordingly).
##########
test/bthread_fd_unittest.cpp:
##########
@@ -566,73 +621,74 @@ TEST(FDTest, double_close) {
ASSERT_EQ(ec, errno);
}
-const char* g_hostname1 = "github.com";
-const char* g_hostname2 = "baidu.com";
-TEST(FDTest, bthread_connect) {
- butil::EndPoint ep1;
- butil::EndPoint ep2;
- ASSERT_EQ(0, butil::hostname2endpoint(g_hostname1, 80, &ep1));
- ASSERT_EQ(0, butil::hostname2endpoint(g_hostname2, 80, &ep2));
-
- {
- struct sockaddr_storage serv_addr{};
- socklen_t serv_addr_size = 0;
- ASSERT_EQ(0, endpoint2sockaddr(ep1, &serv_addr, &serv_addr_size));
- butil::fd_guard sockfd(socket(serv_addr.ss_family, SOCK_STREAM, 0));
- ASSERT_LE(0, sockfd);
- bool is_blocking = butil::is_blocking(sockfd);
- ASSERT_LE(0, sockfd);
- ASSERT_EQ(0, bthread_connect(sockfd, (struct sockaddr*) &serv_addr, serv_addr_size));
- ASSERT_EQ(is_blocking, butil::is_blocking(sockfd));
+// Local listeners keep connect tests independent of DNS, Internet latency,
+// and the assumption that a connection cannot complete within one millisecond.
+void TestLocalConnect(bool timed) {
+ butil::EndPoint endpoint;
+ ASSERT_EQ(0, butil::str2endpoint("127.0.0.1:0", &endpoint));
+ butil::fd_guard listener(butil::tcp_listen(endpoint));
+ ASSERT_GE(listener, 0);
+ ASSERT_EQ(0, butil::get_local_side(listener, &endpoint));
+ struct sockaddr_storage address{};
+ socklen_t length = 0;
+ ASSERT_EQ(0, endpoint2sockaddr(endpoint, &address, &length));
+ butil::fd_guard client(socket(address.ss_family, SOCK_STREAM, 0));
+ ASSERT_GE(client, 0);
+ bool was_blocking = butil::is_blocking(client);
+ timespec deadline = butil::seconds_from_now(10);
+ int rc = bthread_timed_connect(
+ client, reinterpret_cast<sockaddr*>(&address), length,
+ timed ? &deadline : nullptr);
+ ASSERT_EQ(0, rc) << "errno=" << errno;
+ ASSERT_EQ(was_blocking, butil::is_blocking(client));
+ ASSERT_EQ(0, butil::is_connected(client));
+ // The handshake does not require a concurrent accept thread.
+ butil::fd_guard accepted(accept(listener, nullptr, nullptr));
+ ASSERT_GE(accepted, 0);
+}
- }
+TEST(FDTest, bthread_connect) {
+ TestLocalConnect(false);
+ TestLocalConnect(true);
+}
- {
- struct sockaddr_storage serv_addr{};
- socklen_t serv_addr_size = 0;
- ASSERT_EQ(0, endpoint2sockaddr(ep2, &serv_addr, &serv_addr_size));
- butil::fd_guard sockfd(socket(serv_addr.ss_family, SOCK_STREAM, 0));
- ASSERT_LE(0, sockfd);
- bool is_blocking = butil::is_blocking(sockfd);
- // In most cases, 1 millisecond will result in a connection timeout.
- timespec abstime = butil::milliseconds_from_now(1);
- const int rc = bthread_timed_connect(
- sockfd, (struct sockaddr*) &serv_addr,
- serv_addr_size, &abstime);
- ASSERT_EQ(-1, rc);
- ASSERT_EQ(ETIMEDOUT, errno);
- ASSERT_EQ(is_blocking, butil::is_blocking(sockfd));
- }
+#if defined(OS_LINUX)
+TEST(FDTest, connect_timeout_with_full_accept_queue) {
+ butil::EndPoint endpoint;
+ ASSERT_EQ(0, butil::str2endpoint("127.0.0.1:0", &endpoint));
+ butil::fd_guard listener(butil::tcp_listen(endpoint));
+ ASSERT_GE(listener, 0);
+ // Linux permits one queued connection for backlog=0. Leave it unaccepted
+ // so the next handshake cannot complete; no external network is needed.
+ ASSERT_EQ(0, listen(listener, 0));
Review Comment:
Asserting strictly `ETIMEDOUT` can be environment-dependent on Linux. For example, if `net.ipv4.tcp_abort_on_overflow=1`, overflowing the accept queue may produce an immediate `ECONNREFUSED` (RST) instead of a timeout. To keep this test portable across CI hosts, consider accepting either `ETIMEDOUT` or `ECONNREFUSED` (or reading the sysctl and skipping/branching accordingly).
##########
test/bthread_futex_unittest.cpp:
##########
@@ -113,30 +116,55 @@ TEST(FutexTest, futex_wake_before_wait) {
}
void* dummy_waiter(void* lock) {
- bthread::futex_wait_private(lock, 0, nullptr);
+ timespec timeout = butil::seconds_to_timespec(10);
+ int rc;
+ do {
+ rc = bthread::futex_wait_private(lock, 0, &timeout);
+ } while (rc != 0 && errno == EINTR);
+ EXPECT_EQ(0, rc);
return nullptr;
}
TEST(FutexTest, futex_wake_many_waiters_perf) {
- int lock1 = 0;
- size_t N = 0;
- pthread_t th;
- for (; N < 1000 && !pthread_create(&th, nullptr, dummy_waiter, &lock1); ++N) {}
-
- sleep(1);
+ butil::atomic<int> lock1(0);
+ std::vector<pthread_t> threads;
+ for (size_t i = 0; i < 1000; ++i) {
+ pthread_t th;
+ if (pthread_create(&th, nullptr, dummy_waiter, &lock1) != 0) {
+ break;
+ }
+ threads.push_back(th);
+ }
+ ASSERT_FALSE(threads.empty());
+ size_t N = threads.size();
int nwakeup = 0;
+ int64_t wake_ns = 0;
+ int64_t deadline = butil::cpuwide_time_us() + 5000000L;
butil::Timer tm;
- tm.start();
- for (size_t i = 0; i < N; ++i) {
- nwakeup += bthread::futex_wake_private(&lock1, 1);
+ while (static_cast<size_t>(nwakeup) < N &&
+ butil::cpuwide_time_us() < deadline) {
+ tm.start();
+ int rc = bthread::futex_wake_private(&lock1, 1);
+ tm.stop();
+ EXPECT_GE(rc, 0);
+ if (rc > 0) {
+ nwakeup += rc;
+ wake_ns += tm.n_elapsed();
+ } else {
+ usleep(1000);
+ }
}
- tm.stop();
- printf("N=%lu, futex_wake a thread = %" PRId64 "ns\n", N, tm.n_elapsed() / N);
- ASSERT_EQ(N, (size_t)nwakeup);
+ // Also release late waiters on failure; a wake alone is not persistent.
+ lock1.store(1);
+ bthread::futex_wake_private(&lock1, INT_MAX);
+ for (pthread_t th : threads) {
+ EXPECT_EQ(0, pthread_join(th, nullptr));
+ }
+ ASSERT_EQ(N, static_cast<size_t>(nwakeup));
+ printf("N=%lu, futex_wake a thread = %" PRId64 "ns\n", N, wake_ns / N);
Review Comment:
The test assumes all waiters will be registered and individually woken within a fixed 5s deadline, then hard-asserts `nwakeup == N`. On slow/oversubscribed CI, thread creation and waiter registration can legitimately exceed this budget, making the test flaky again. A more robust approach is to add an explicit readiness/registration counter (or barrier) in `dummy_waiter` and wait until all threads have entered the wait before starting the wake timing loop, or to change the post-condition to assert all threads eventually exit (and only report timing for those actually woken).
##########
test/bthread_timer_thread_unittest.cpp:
##########
@@ -85,11 +101,11 @@ class TimeKeeper {
{
ASSERT_TRUE(!_run_times.empty());
long diff = timespec_diff_us(_run_times[0], expect_run_time);
- EXPECT_LE(labs(diff), 50000);
+ EXPECT_GE(diff, 0);
}
Review Comment:
This assertion now only checks that the task did not run early, but it no longer bounds how late it may run. That can mask real timer regressions (e.g., tasks firing seconds/minutes late) while still passing. Consider keeping the anti-early check while also adding a generous upper bound (large enough for CI variance) so the test still detects stalls.
##########
test/bthread_timer_thread_unittest.cpp:
##########
@@ -98,59 +114,70 @@ class TimeKeeper {
keeper->run();
}
+ bool wait_finished() {
+ return WaitUntil([this] {
+ return _finished.load(butil::memory_order_acquire);
+ });
+ }
+
+ bool wait_started() {
+ return WaitUntil([this] {
+ return _started.load(butil::memory_order_acquire);
+ });
+ }
+
timespec _expect_run_time;
bthread::TimerThread::TaskId _task_id;
private:
const char* _name;
- int _sleep_ms;
+ butil::atomic<int> _sleep_ms;
+ butil::atomic<bool> _started{false};
+ butil::atomic<bool> _finished{false};
std::vector<timespec> _run_times;
};
TEST(TimerThreadTest, RunTasks) {
bthread::TimerThread timer_thread;
ASSERT_EQ(0, timer_thread.start(nullptr));
- timespec _2s_later = butil::seconds_from_now(2);
+ timespec _2s_later = butil::milliseconds_from_now(20);
Review Comment:
Variable names like `_2s_later`, `_1s_later`, and `_10s_later` no longer match their actual values (20ms/10ms/3600s). Renaming them to reflect the real delays (e.g., `_20ms_later`, `_10ms_later`, `_1h_later`) will prevent test logic from being misread and incorrectly modified later.
##########
test/bthread_timer_thread_unittest.cpp:
##########
@@ -98,59 +114,70 @@ class TimeKeeper {
keeper->run();
}
+ bool wait_finished() {
+ return WaitUntil([this] {
+ return _finished.load(butil::memory_order_acquire);
+ });
+ }
+
+ bool wait_started() {
+ return WaitUntil([this] {
+ return _started.load(butil::memory_order_acquire);
+ });
+ }
+
timespec _expect_run_time;
bthread::TimerThread::TaskId _task_id;
private:
const char* _name;
- int _sleep_ms;
+ butil::atomic<int> _sleep_ms;
+ butil::atomic<bool> _started{false};
+ butil::atomic<bool> _finished{false};
std::vector<timespec> _run_times;
};
TEST(TimerThreadTest, RunTasks) {
bthread::TimerThread timer_thread;
ASSERT_EQ(0, timer_thread.start(nullptr));
- timespec _2s_later = butil::seconds_from_now(2);
+ timespec _2s_later = butil::milliseconds_from_now(20);
TimeKeeper keeper1(_2s_later, "keeper1");
keeper1.schedule(&timer_thread);
- TimeKeeper keeper2(_2s_later, "keeper2"); // same time with keeper1
+ TimeKeeper keeper2(butil::seconds_from_now(3600), "keeper2");
keeper2.schedule(&timer_thread);
- timespec _1s_later = butil::seconds_from_now(1);
+ timespec _1s_later = butil::milliseconds_from_now(10);
Review Comment:
Variable names like `_2s_later`, `_1s_later`, and `_10s_later` no longer match their actual values (20ms/10ms/3600s). Renaming them to reflect the real delays (e.g., `_20ms_later`, `_10ms_later`, `_1h_later`) will prevent test logic from being misread and incorrectly modified later.
##########
test/bthread_timer_thread_unittest.cpp:
##########
@@ -98,59 +114,70 @@ class TimeKeeper {
keeper->run();
}
+ bool wait_finished() {
+ return WaitUntil([this] {
+ return _finished.load(butil::memory_order_acquire);
+ });
+ }
+
+ bool wait_started() {
+ return WaitUntil([this] {
+ return _started.load(butil::memory_order_acquire);
+ });
+ }
+
timespec _expect_run_time;
bthread::TimerThread::TaskId _task_id;
private:
const char* _name;
- int _sleep_ms;
+ butil::atomic<int> _sleep_ms;
+ butil::atomic<bool> _started{false};
+ butil::atomic<bool> _finished{false};
std::vector<timespec> _run_times;
};
TEST(TimerThreadTest, RunTasks) {
bthread::TimerThread timer_thread;
ASSERT_EQ(0, timer_thread.start(nullptr));
- timespec _2s_later = butil::seconds_from_now(2);
+ timespec _2s_later = butil::milliseconds_from_now(20);
TimeKeeper keeper1(_2s_later, "keeper1");
keeper1.schedule(&timer_thread);
- TimeKeeper keeper2(_2s_later, "keeper2"); // same time with keeper1
+ TimeKeeper keeper2(butil::seconds_from_now(3600), "keeper2");
keeper2.schedule(&timer_thread);
- timespec _1s_later = butil::seconds_from_now(1);
+ timespec _1s_later = butil::milliseconds_from_now(10);
TimeKeeper keeper3(_1s_later, "keeper3");
keeper3.schedule(&timer_thread);
- timespec _10s_later = butil::seconds_from_now(10);
+ timespec _10s_later = butil::seconds_from_now(3600);
Review Comment:
Variable names like `_2s_later`, `_1s_later`, and `_10s_later` no longer match their actual values (20ms/10ms/3600s). Renaming them to reflect the real delays (e.g., `_20ms_later`, `_10ms_later`, `_1h_later`) will prevent test logic from being misread and incorrectly modified later.
##########
test/bthread_work_stealing_queue_unittest.cpp:
##########
@@ -85,6 +85,7 @@ void* pop_thread(void* arg) {
TEST(WSQTest, sanity) {
+ g_stop = false;
Review Comment:
Since `g_stop` is now an atomic, prefer using `g_stop.store(false, ...)` (and corresponding `load(...)` at read sites) for clarity and to make the intended memory ordering explicit. This also avoids relying on any non-standard implicit conversions/operators on the atomic type.
##########
test/bthread_futex_unittest.cpp:
##########
@@ -113,30 +116,55 @@ TEST(FutexTest, futex_wake_before_wait) {
}
void* dummy_waiter(void* lock) {
- bthread::futex_wait_private(lock, 0, nullptr);
+ timespec timeout = butil::seconds_to_timespec(10);
+ int rc;
+ do {
+ rc = bthread::futex_wait_private(lock, 0, &timeout);
+ } while (rc != 0 && errno == EINTR);
+ EXPECT_EQ(0, rc);
return nullptr;
}
TEST(FutexTest, futex_wake_many_waiters_perf) {
- int lock1 = 0;
- size_t N = 0;
- pthread_t th;
- for (; N < 1000 && !pthread_create(&th, nullptr, dummy_waiter, &lock1); ++N) {}
-
- sleep(1);
+ butil::atomic<int> lock1(0);
+ std::vector<pthread_t> threads;
+ for (size_t i = 0; i < 1000; ++i) {
+ pthread_t th;
+ if (pthread_create(&th, nullptr, dummy_waiter, &lock1) != 0) {
+ break;
+ }
+ threads.push_back(th);
+ }
+ ASSERT_FALSE(threads.empty());
+ size_t N = threads.size();
int nwakeup = 0;
+ int64_t wake_ns = 0;
+ int64_t deadline = butil::cpuwide_time_us() + 5000000L;
butil::Timer tm;
- tm.start();
- for (size_t i = 0; i < N; ++i) {
- nwakeup += bthread::futex_wake_private(&lock1, 1);
+ while (static_cast<size_t>(nwakeup) < N &&
+ butil::cpuwide_time_us() < deadline) {
+ tm.start();
+ int rc = bthread::futex_wake_private(&lock1, 1);
+ tm.stop();
+ EXPECT_GE(rc, 0);
+ if (rc > 0) {
+ nwakeup += rc;
+ wake_ns += tm.n_elapsed();
+ } else {
+ usleep(1000);
+ }
}
- tm.stop();
- printf("N=%lu, futex_wake a thread = %" PRId64 "ns\n", N, tm.n_elapsed() / N);
- ASSERT_EQ(N, (size_t)nwakeup);
+ // Also release late waiters on failure; a wake alone is not persistent.
+ lock1.store(1);
+ bthread::futex_wake_private(&lock1, INT_MAX);
+ for (pthread_t th : threads) {
+ EXPECT_EQ(0, pthread_join(th, nullptr));
+ }
+ ASSERT_EQ(N, static_cast<size_t>(nwakeup));
+ printf("N=%lu, futex_wake a thread = %" PRId64 "ns\n", N, wake_ns / N);
Review Comment:
The test assumes all waiters will be registered and individually woken within a fixed 5s deadline, then hard-asserts `nwakeup == N`. On slow/oversubscribed CI, thread creation and waiter registration can legitimately exceed this budget, making the test flaky again. A more robust approach is to add an explicit readiness/registration counter (or barrier) in `dummy_waiter` and wait until all threads have entered the wait before starting the wake timing loop, or to change the post-condition to assert all threads eventually exit (and only report timing for those actually woken).
--
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: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] Stabilize timing-sensitive bthread tests and fix the butex interruption race (brpc)
Posted by "chenBright (via GitHub)" <gi...@apache.org>.
chenBright commented on code in PR #3545:
URL: https://github.com/apache/brpc/pull/3545#discussion_r4081992906
##########
test/bthread_mutex_unittest.cpp:
##########
@@ -52,8 +52,14 @@ TEST(MutexTest, sanity) {
ASSERT_EQ(1u, *get_butex(m));
bthread_t th1;
ASSERT_EQ(0, bthread_start_urgent(&th1, nullptr, locker, &m));
- usleep(5000); // wait for locker to run.
- ASSERT_EQ(257u, *get_butex(m)); // contention
+ auto* state = reinterpret_cast<butil::atomic<unsigned>*>(get_butex(m));
+ int64_t deadline = butil::cpuwide_time_us() + 5000000L;
+ while (state->load(butil::memory_order_relaxed) != 257u &&
+ butil::cpuwide_time_us() < deadline) {
+ usleep(1000);
+ }
+ // Keep cleanup reachable even if the worker did not run in time.
+ ASSERT_EQ(257u, state->load(butil::memory_order_relaxed));
Review Comment:
Done.
--
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: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] Stabilize timing-sensitive bthread tests and fix the butex interruption race (brpc)
Posted by "chenBright (via GitHub)" <gi...@apache.org>.
chenBright commented on code in PR #3545:
URL: https://github.com/apache/brpc/pull/3545#discussion_r4089448041
##########
src/bthread/butex.cpp:
##########
@@ -731,7 +731,13 @@ static int butex_wait_from_pthread(TaskGroup* g, Butex* b, int expected_value,
30/*nops before sched_yield*/);
if (task->interrupted) {
task->interrupted = false;
- if (rc == 0) {
+ // If interrupted after enqueueing but before futex_wait_private,
+ // pw.sig is already signalled and futex_wait_private may report
+ // EWOULDBLOCK. This is an interruption, not a value mismatch on
+ // the user's butex. Preserve other errors (notably ETIMEDOUT).
+ if (rc == 0 || (errno == EWOULDBLOCK &&
+ pw.sig.load(butil::memory_order_acquire) ==
+ PTHREAD_SIGNALLED)) {
Review Comment:
Fixed in 8c404350748c5134787c813c26c8bf44e4b8733f .
--
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: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org