Closed ERobsham closed 1 year ago
Thanks, I think we need to keep the current lock acquisition to avoid UB but then drop it before acquiring the second one and reacquire it lower down.
Thanks for the speedy response! Im not 100% sure what you mean by "to avoid UB"? So let me know if this update was the correct change you're looking for.
Looks great, thanks!
Reorders how locking is handled in
objc_send_initialize()
to prevent a deadlock. Previously, contention on the low level spinlocks could cause a very intermittent deadlock:objc_send_initialize()
holds the runtime lock, then tries to acquire the object lock on the metaclass, which needs to initialize the mutex for the new metaclass insidereferenceListForObject()
, so it tries to lock thelock_for_pointer()
/lock_spinlock()
Thread B :
referenceListForObject()
holds a spinlock for an unrelated object while runninginitHiddenClassForObject()
->allocateHiddenClass()
, which tries to acquire the runtime lockIf the metaclass object pointer in Thread A hashes to the same spinlock as the object in thread B, the runtime lock ends up deadlocked forever.
Potential fix for this issue