Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-192] Weak properties are not thread safe when reading #42814

Closed
mikeash opened this issue Dec 11, 2015 · 3 comments
Closed

[SR-192] Weak properties are not thread safe when reading #42814

mikeash opened this issue Dec 11, 2015 · 3 comments
Assignees

Comments

@mikeash
Copy link
Contributor

@mikeash mikeash commented Dec 11, 2015

Previous ID SR-192
Radar None
Original Reporter @mikeash
Type Bug
Status Closed
Resolution Done
Additional Detail from JIRA
Votes 8
Component/s
Labels Bug, Runtime
Assignee @glessard
Priority Medium

md5: e84790d9452452d03d108981ba9ce226

Issue Description:

The current implementation of weak references has a race condition when multiple threads read the same weak reference to a deallocating object simultaneously. Since this is semantically read-only access (even if writing occurs under the hood), this should be made safe.

The bug itself lies in the swift_weakLoadStrong function. If two threads simultaneously enter this function with the same weak reference and object->refCount.isDeallocating() is true, the threads will race. This can result in calling swift_unownedRelease() twice resulting in a double-free or worse, or it could result in one thread calling swift_tryRetain() on a deallocated object.

Here is a test case which demonstrates the problem:

import Foundation

class Target {}

class WeakHolder {
   weak var weak: Target?
}

for i in 0..<1000000 {
   print(i)
   let holder = WeakHolder()
   holder.weak = Target()
   dispatch_async(dispatch_get_global_queue(0, 0), {
       let _ = holder.weak
   })
   dispatch_async(dispatch_get_global_queue(0, 0), {
       let _ = holder.weak
   })
}
@glessard
Copy link
Contributor

@glessard glessard commented Feb 26, 2016

This is addressed in #1454

@glessard
Copy link
Contributor

@glessard glessard commented May 4, 2016

PR-1454 has been merged.

@glessard
Copy link
Contributor

@glessard glessard commented May 5, 2016

Resolved by PR-1454

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants