Skip to content

Commit 0efaa5d

Browse files
sherry-yuanpcolberg
authored andcommitted
Fix TSAN issues in thread support library
-------------------------------------------------------- The issue was the test is attempting to check for change in variable through a while loop, but did not protect the data read using mutext lock. This could potentially lead to reading of unexpected data and cause a tsan error. The fix wraps the checks for variable state inside the mutex lock.
1 parent d7ab9be commit 0efaa5d

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

lib/acl_threadsupport/test/acl_threadsupport_test.cpp

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,14 @@ volatile int threadvar = 0;
9595

9696
void *test_thread(void *) {
9797
while (1) {
98-
while (threadtest_state == SERVER_STATE)
98+
while (true) {
99+
acl_mutex_lock(&mymutex);
100+
const bool is_server_state = (threadtest_state == SERVER_STATE);
101+
acl_mutex_unlock(&mymutex);
102+
if (!is_server_state)
103+
break;
99104
acl_thread_yield();
105+
}
100106
if (threadtest_state == END_STATE) {
101107
return 0;
102108
}
@@ -125,16 +131,28 @@ TEST(threadsupport, threads) {
125131
res = acl_mutex_unlock(&mymutex);
126132
CHECK_EQUAL(0, res);
127133

128-
while (threadtest_state != SERVER_STATE)
134+
while (true) {
135+
acl_mutex_lock(&mymutex);
136+
const bool is_server_state = (threadtest_state == SERVER_STATE);
137+
acl_mutex_unlock(&mymutex);
138+
if (is_server_state)
139+
break;
129140
acl_thread_yield();
141+
}
130142
res = acl_mutex_lock(&mymutex);
131143
CHECK_EQUAL(0, res);
132144
CHECK_EQUAL(threadvar, 7 + 13);
133145
threadtest_state = CLIENT_STATE;
134146
res = acl_mutex_unlock(&mymutex);
135147
CHECK_EQUAL(0, res);
136-
while (threadtest_state != SERVER_STATE)
148+
while (true) {
149+
acl_mutex_lock(&mymutex);
150+
const bool is_server_state = (threadtest_state == SERVER_STATE);
151+
acl_mutex_unlock(&mymutex);
152+
if (is_server_state)
153+
break;
137154
acl_thread_yield();
155+
}
138156
res = acl_mutex_lock(&mymutex);
139157
CHECK_EQUAL(0, res);
140158
threadtest_state = END_STATE;

0 commit comments

Comments
 (0)