Skip to content

Commit c17542f

Browse files
authoredDec 13, 2022
Fix infinite wait bug in test_pthread_proxying_cpp.cpp (#18358)
The tests in test_pthread_proxying_cpp proxy functions that increment local variables. For the async proxying functions, the proxying is followed by a wait on a condition variable so the test can assert that the proxied work was completed. However, the incrementing of the local variables was not protected by the lock used with the condition variable, so it was possible for the variable to be incremented and the condition variable notified after checking the wait condition but before waiting. Since the condition variable would not be notified again, the test would wait on the condition variable forever. Fix the bug by taking the lock before incrementing the variable in tests where this could cause problems. Fixes #18353.
1 parent ad41934 commit c17542f

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed
 

‎test/pthread/test_pthread_proxying_cpp.cpp

+16-4
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ void test_proxy_async() {
5656
// Proxy to looper.
5757
{
5858
queue.proxyAsync(looper.native_handle(), [&]() {
59-
i = 2;
59+
{
60+
std::unique_lock<std::mutex> lock(mutex);
61+
i = 2;
62+
}
6063
executor = std::this_thread::get_id();
6164
cond.notify_one();
6265
});
@@ -68,7 +71,10 @@ void test_proxy_async() {
6871
// Proxy to returner.
6972
{
7073
queue.proxyAsync(returner.native_handle(), [&]() {
71-
i = 3;
74+
{
75+
std::unique_lock<std::mutex> lock(mutex);
76+
i = 3;
77+
}
7278
executor = std::this_thread::get_id();
7379
cond.notify_one();
7480
});
@@ -164,7 +170,10 @@ void test_proxy_async_with_callback(void) {
164170
queue.proxyAsyncWithCallback(
165171
looper.native_handle(),
166172
[&]() {
167-
i = 2;
173+
{
174+
std::unique_lock<std::mutex> lock(mutex);
175+
i = 2;
176+
}
168177
executor = std::this_thread::get_id();
169178
cond.notify_one();
170179
},
@@ -184,7 +193,10 @@ void test_proxy_async_with_callback(void) {
184193
queue.proxyAsyncWithCallback(
185194
returner.native_handle(),
186195
[&]() {
187-
i = 3;
196+
{
197+
std::unique_lock<std::mutex> lock(mutex);
198+
i = 3;
199+
}
188200
executor = std::this_thread::get_id();
189201
cond.notify_one();
190202
},

0 commit comments

Comments
 (0)
Failed to load comments.