Skip to content

[SR-9077] NSKeyValueObservingCustomization is broken with multiple threads #3606

Closed

Description

@lilyball
mannequin
Previous ID SR-9077
Radar rdar://problem/45567020
Original Reporter @lilyball
Type Bug
Status Resolved
Resolution Done
Environment

Latest Swift master (14d4235b571d7a4e2ae51854f0d665f96959d182)

Additional Detail from JIRA
Votes 0
Component/s Foundation
Labels Bug
Assignee @lilyball
Priority Medium

md5: 2b32c8ca7953815296bfa7e8eac77b80

relates to:

  • 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.

Activity

  1. belkadan commented on Oct 25, 2018

    @belkadan
  2. lilyball commented on Oct 25, 2018

    lilyballmannequin
    MannequinAuthor

    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.

  3. lilyball commented on Oct 25, 2018

    lilyballmannequin
    MannequinAuthor

    I filed the issue SR-9081 about how we should just remove the protocol entirely.

  4. millenomi commented on Oct 25, 2018

    @millenomi
    Contributor

    @swift-ci create

  5. millenomi commented on Oct 25, 2018

    @millenomi
    Contributor

    API can't be removed; we have to fix this.

  6. millenomi commented on Oct 25, 2018

    @millenomi
    Contributor

    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.: )

    String(describing: type(of: keyPath).rootType)
  7. lilyball commented on Oct 25, 2018

    lilyballmannequin
    MannequinAuthor

    Type information doesn't work in the face of subclasses. See SR-9081 for more details. The protocol is fundamentally broken.

  8. lilyball commented on Oct 27, 2018

    lilyballmannequin
    MannequinAuthor

    I've submitted PR #20103 for this issue.

  9. lilyball commented on Oct 27, 2018

    lilyballmannequin
    MannequinAuthor

    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.

  10. lilyball commented on Oct 27, 2018

    lilyballmannequin
    MannequinAuthor

    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.

  11. millenomi commented on Jan 30, 2019

    @millenomi
    Contributor
  12. transferred this issue fromswiftlang/swifton May 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions