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

Regression: Concurrent read from __SwiftDeferredNSArray crashing in Swift 5.7 #60418

Open
matttodd opened this issue Aug 5, 2022 · 9 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior.

Comments

@matttodd
Copy link

matttodd commented Aug 5, 2022

Describe the bug
In my app, we experienced a new EXC_BAD_ACCESS crash from users on iOS 16 (beta 4 - build 20A5328h) when reading from a swift Array bridged to NSArray on multiple threads. The crash can be reproduced by running the function provided below.

Steps To Reproduce

func crash() {
    let arraySize = 10_000 // large size appears to matter, maybe takes longer to load backing buffer for __SwiftDeferredNSArray?
    let array = Array(repeating: 1, count: arraySize)
    let nsArray = array as NSArray
    DispatchQueue.concurrentPerform(iterations: 10) { _ in
        _ = nsArray.object(at: 0)
    }
}

Screen Shot 2022-08-05 at 1 23 13 PM

Expected behavior
Reading from an NSArray should be thread safe.

Environment (please fill out the following information)

  • OS: iOS 16
  • Build: Beta build 4 - 20A5328h
  • Xcode Version/Tag/Branch: xcode 14 beta
  • Swift: 5.7

Additional context
My theory is it has to do with recent changes to _swift_setClassMetadata in this commit

@matttodd matttodd added the bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. label Aug 5, 2022
@matttodd matttodd changed the title Concurrent read from __SwiftDeferredNSArray crashing in Swift 5.7 Regression: Concurrent read from __SwiftDeferredNSArray crashing in Swift 5.7 Aug 5, 2022
@technikyle
Copy link

technikyle commented Aug 6, 2022

Experiencing same crash! Thanks for reporting. Note this affects all Swift arrays accessed in ObjC. Would think that is common.

@tbkka
Copy link
Contributor

tbkka commented Aug 8, 2022

CC: @aschwaighofer @Catfish-Man

@technikyle
Copy link

technikyle commented Aug 8, 2022

I just want to point that for the provided example, the array size required to produce the crash varies by device. For my iPad is around 10,000 and for iOS simulator is about 10,000,000.

In the real world, this our our top crash for users on an iOS 16 device.

@aschwaighofer
Copy link
Contributor

aschwaighofer commented Aug 8, 2022

This example involves types that are not bridged verbatim (Array<Int>, deferred array storage). The _swift_setClassMetadata path is not implicated by this because it happens only in the verbatim case.

@technikyle
Copy link

If this issue is unrelated to the suggested commit, just wondering if this is being investigated to determine the cause.

@brototyp
Copy link

Did anyone figure out something about this? Found this issue on our App in the wild. I am a bit concerned that there will not be a fix before iOS 16 is released and our App is crashing massively. How did you go about this? Is there information somewhere other than this issue? Did any of you file a radar/feedback? Did you implement a workaround instead of waiting? I am curious 😊

@matttodd
Copy link
Author

Did anyone figure out something about this? Found this issue on our App in the wild. I am a bit concerned that there will not be a fix before iOS 16 is released and our App is crashing massively. How did you go about this? Is there information somewhere other than this issue? Did any of you file a radar/feedback? Did you implement a workaround instead of waiting? I am curious 😊

We were able to get a quick-and-dirty workaround by doing an initial read from the array on a single thread. We didn't file on the Feedback Assistant but made a Swift Forums post here.

@brototyp
Copy link

Thanks for the update @matttodd. I guess we'll go for a workaround as well.

@suttiwat
Copy link

Me too. I encounter with this some sort of bug about SwiftArray too. But in my situation, I just use

try? JSONSerialization.data(withJSONObject: containerModel, options: .fragmentsAllowed)

It's crash inside this JSONSerialization.data method even I use optional try.
But this is not happen on all user devices. Just for some specific condition (on some device) which I can't identity that condition exactly.

Attached is stack trace.

Screen Shot 2022-09-15 at 09 56 38

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.
Projects
None yet
Development

No branches or pull requests

6 participants