Bug #106714 A potential data race
Submitted: 12 Mar 2022 8:51 Modified: 14 Mar 2022 14:26
Reporter: Cai Ryan Email Updates:
Status: Not a Bug Impact on me:
None 
Category:MySQL Server Severity:S7 (Test Cases)
Version:8.0.26 OS:Any
Assigned to: CPU Architecture:Any
Tags: bug, data race

[12 Mar 2022 8:51] Cai Ryan
Description:
Hi, in the below code, should the lock be released after the checking of the value of the notified. Otherwise, the value of notified read from notifier_ still has the possibility to be revised due to data races, thus changing the control flows.

void WaitForNotification() {
    for (;;) {
      pthread_mutex_lock(&mutex_);
      const bool notified = notified_;
      pthread_mutex_unlock(&mutex_);
      if (notified)
        break;
      SleepMilliseconds(10);
    }
  }

Location:
https://github.com/mysql/mysql-server/blob/3290a66c89eb1625a7058e0ef732432b6952b435/extra/...

How to repeat:
None

Suggested fix:
void WaitForNotification() {
    for (;;) {
      pthread_mutex_lock(&mutex_);
      const bool notified = notified_;
      if (notified){
           pthread_mutex_unlock(&mutex_);
           break;
      }
      pthread_mutex_unlock(&mutex_);
      SleepMilliseconds(10);
    }
  }
[14 Mar 2022 14:26] MySQL Verification Team
Hi Mr. Yuandao,

Thank you for your bug report.

However, this is not a bug.

Do note that `notified` is not a pointer, but it holds a value. Furthermore, it is a local constant. Hence, according to the latest C standard, C18, it does not have to be protected by a mutex, since it is not shared among the threads.

Not a bug.
[15 Mar 2022 13:15] MySQL Verification Team
We have also noted that this code is not found in our latest 8.0 release.