You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[SR-9077] NSKeyValueObservingCustomization is broken with multiple threads #3606
SR-9081 NSKeyValueObservingCustomization fundamentally broken, should be removed
Issue Description:
NSKeyValueObservingCustomization relies on a global table to map String key paths back into AnyKeyPath values. However, as the string keypath does not include the root type, this means that observing properties with the same name on separate objects will overwrite each other in the global table. In a single-threaded scenario this is acceptable as the NSKeyValueObservingCustomization methods are invoked synchronously when the observation is created, and the global table is populated immediately prior to creating the observation. However, in a multithreaded scenario, the global keypath table could be overwritten with a different keypath prior to invoking the NSKeyValueObservingCustomization method.
Assuming we can rely on the fact that these methods will be invoked synchronously during the observation and never at any other time, we could fix this by using a per-thread table instead, which we populate prior to the observation and then clear afterwards.
I've got a proof-of-concept patch done already, but I haven't attempt to test it yet. I'm not sure how to even try and write tests for race conditions involving code that is executed once per process.
Also, while writing this, I determined that even the per-thread approach isn't a complete solution. If the implementation of automaticallyNotifiesObservers(for:) itself creates any observations (or NSSortDescriptor or NSExpression using KeyPath) then this can overwrite the per-thread value before keyPathsAffectingValue(for:) is invoked. This is of course a pretty esoteric edge case, but I don't see any way to solve it.
That said, I think the whole protocol is fundamentally flawed and should be removed. I will be filing a separate ticket about that.
lilyball commented on Oct 25, 2018
lilyballmannequin
MannequinAuthor
More actions
I filed the issue SR-9081 about how we should just remove the protocol entirely.
This PR implements the per-thread table approach. It occurs to me just now that the above esoteric edge case could be solved by having the stubs that invoke the NSKeyValueObservingCustomization methods reinstall the key path after invoking the method. I'll probably update the PR to do that.
lilyball commented on Oct 27, 2018
lilyballmannequin
MannequinAuthor
More actions
Upon reflection, reinstalling the key path won’t work if a subclass (or the class itself) overrides the Obj-C method and uses key paths there. This will overwrite the key path table before our stub is even invoked.
I’ve also just realized that the protocol itself is broken in the case where someone uses the Obj-C observation method because we won’t have an opportunity to populate the key path table to begin with.
Activity
belkadan commentedon Oct 25, 2018
cc @millenomi
lilyball commentedon Oct 25, 2018
I've got a proof-of-concept patch done already, but I haven't attempt to test it yet. I'm not sure how to even try and write tests for race conditions involving code that is executed once per process.
Also, while writing this, I determined that even the per-thread approach isn't a complete solution. If the implementation of
automaticallyNotifiesObservers(for:)itself creates any observations (orNSSortDescriptororNSExpressionusingKeyPath) then this can overwrite the per-thread value beforekeyPathsAffectingValue(for:)is invoked. This is of course a pretty esoteric edge case, but I don't see any way to solve it.That said, I think the whole protocol is fundamentally flawed and should be removed. I will be filing a separate ticket about that.
lilyball commentedon Oct 25, 2018
I filed the issue SR-9081 about how we should just remove the protocol entirely.
millenomi commentedon Oct 25, 2018
@swift-ci create
millenomi commentedon Oct 25, 2018
API can't be removed; we have to fix this.
millenomi commentedon Oct 25, 2018
It looks to me like the solution here is either a per-thread table; or just add type information to the global table (e.g.: )
lilyball commentedon Oct 25, 2018
Type information doesn't work in the face of subclasses. See SR-9081 for more details. The protocol is fundamentally broken.
lilyball commentedon Oct 27, 2018
I've submitted PR #20103 for this issue.
lilyball commentedon Oct 27, 2018
This PR implements the per-thread table approach. It occurs to me just now that the above esoteric edge case could be solved by having the stubs that invoke the
NSKeyValueObservingCustomizationmethods reinstall the key path after invoking the method. I'll probably update the PR to do that.lilyball commentedon Oct 27, 2018
Upon reflection, reinstalling the key path won’t work if a subclass (or the class itself) overrides the Obj-C method and uses key paths there. This will overwrite the key path table before our stub is even invoked.
I’ve also just realized that the protocol itself is broken in the case where someone uses the Obj-C observation method because we won’t have an opportunity to populate the key path table to begin with.
millenomi commentedon Jan 30, 2019
Merged: swiftlang/swift@7c51419