Thread Sanitizer false positive when deferring mutex unlock

Originator:hoar.ian
Number:rdar://49757291 Date Originated:Apr 9, 2019
Status:Open Resolved:
Product:Xcode Product Version:10.2
Classification:Bug Reproducible:Always
 
Summary:
Thread Sanitizer stops on a false positive when deferring mutex unlock.

Steps to Reproduce:
(run sample project with thread sanitizer enabled)
- init a mutex
- lock the mutex on a non-main thread
- unlock the mutex on a non-main thread
- lock the mutex on the main thread 
- defer unlocking mutex on main thread

Expected Results:
- Thread sanitizer does not pause on valid mutex usage

Actual Results:
- WARNING: ThreadSanitizer: Swift access race

Version/Build:
Xcode 10.2


Configuration:

```
class ViewController: UIViewController {
    var mutex = pthread_mutex_t()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        pthread_mutex_init(&mutex, nil)
        DispatchQueue(label: "NotMainQueue", attributes: []).async {
            pthread_mutex_lock(&self.mutex)
            pthread_mutex_unlock(&self.mutex)
        }
        pthread_mutex_lock(&self.mutex)
        defer { pthread_mutex_unlock(&self.mutex) }
    }
}
```

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!