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-5439] [Beta3] Runtime exclusivity failure for KVO context pointers #48013

Closed
zwaldowski opened this issue Jul 12, 2017 · 3 comments
Closed
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@zwaldowski
Copy link

Previous ID SR-5439
Radar rdar://problem/33265254
Original Reporter @zwaldowski
Type Bug
Status Resolved
Resolution Done
Environment

Xcode version 9.0 beta 3 (9M174d) / swiftlang 900.0.52

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug
Assignee @zwaldowski
Priority Medium

md5: b013264626250a5532385bbccf726cb5

Issue Description:

Up until Swift 4, "Using Swift with Cocoa and Objective-C" recommended "creat[ing] a global context variable" to take the address of to pass to the context parameter of `NSObject.addObserver(_:forKeyPath:options:context🙂`.

As of Beta 3, this practice fails runtime exclusivity checks, producing console logs and warnings in Xcode like:

Simultaneous accesses to 0x10416e278, but modification requires exclusive access.
Previous access (a modification) started at REDACTED`StatusViewController.addViewControllerObservers(_:) + 709 (0x1026521d5).
Current access (a modification) started at:
0    libswiftCore.dylib                 0x000000010d2ec7a0 swift_beginAccess + 505
1    REDACTED                           0x0000000102655ed0 StatusViewController.observeValue(forKeyPath:of:change:context:) + 198
2    REDACTED                           0x0000000102659f30 @objc StatusViewController.observeValue(forKeyPath:of:change:context:) + 648
3    Foundation                         0x0000000106d9ef2b NSKeyValueNotifyObserver + 349
4    Foundation                         0x0000000106d7f469 -[NSObject(NSKeyValueObserverRegistration) _addObserver:forProperty:options:context:] + 255
5    Foundation                         0x0000000106d7e3cb -[NSObject(NSKeyValueObserverRegistration) addObserver:forKeyPath:options:context:] + 103
6    REDACTED                           0x0000000102651f10 StatusViewController.addViewControllerObservers(_:) + 776
7    REDACTED                           0x0000000102651e20 StatusViewController.contentViewController.didset + 199
8    REDACTED                           0x0000000102651900 StatusViewController.contentViewController.setter + 313
9    REDACTED                           0x000000010265a1d0 StatusViewController.showContentViewController(_:) + 250
10   REDACTED                           0x000000010265a370 @objc StatusViewController.showContentViewController(_:) + 60
11   REDACTED                           ...
16   Deferred                           0x0000000106ccd6a0 closure #​1 in Deferred.notify(flags:upon:execute:) + 366
17   Deferred                           0x0000000106cd0150 partial apply for closure #​1 in Deferred.notify(flags:upon:execute:) + 79
18   Deferred                           0x0000000106ca8b90 thunk for @callee_owned () -> () + 41
19   libdispatch.dylib                  0x000000010e0466d6 _dispatch_block_async_invoke2 + 106
20   libdispatch.dylib                  0x000000010e039798 _dispatch_client_callout + 8
21   libdispatch.dylib                  0x000000010e044ec8 _dispatch_main_queue_callback_4CF + 639
22   CoreFoundation                     0x0000000108632de0 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
23   CoreFoundation                     0x00000001085f6e30 __CFRunLoopRun + 2162
24   CoreFoundation                     0x00000001085f6a20 CFRunLoopRunSpecific + 409
25   GraphicsServices                   0x000000010f4f99a4 GSEventRunModal + 62
26   UIKit                              0x000000010ae5cc13 UIApplicationMain + 159
27   REDACTED                           0x00000001023fd7b0 main + 55
28   libdyld.dylib                      0x000000010e0bab94 start + 1

While the Swift 4 materials recommend use of the new `KeyPath`-based overlay, this leaves 3.2 users in a lurch.

Workarounds:

  • Replacing the context with an `unsafeBitCast` of a `Selector`

  • Replacing the context with an `unsafeBitCast` of a `Foo.self` metatype.

  • Removing the context (only applicable to 1-ary observations not involved in a class tree.)

@jckarter
Copy link
Member

@swift-ci create

@belkadan
Copy link
Contributor

Another workaround: avoid using the initial observing option.

@jckarter
Copy link
Member

Looks like @rjmccall came up with a fix in #11150

@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
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself
Projects
None yet
Development

No branches or pull requests

3 participants