Crash on [AVPlayer _addLayer] in iOS 16 caused by Pegasus

Hi,

Our apps currently facing a crash that only happens in iPhone with iOS 16. Unfortunately, we could not reproduce this issue, neither from XCode build or App store build.

From third party crash analytics, it happens mostly of background and they highlighted this as the issue.

Fatal Exception: NSInvalidArgumentException
*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil

And the stack trace, it shows:

AVFCore __22-[AVPlayer _addLayer:]_block_invoke
Pegasus
__116-[PGPictureInPictureProxy _endDeactivatingPictureInPictureIfNeededWithAnimationType:stopReason:cleanupHandlerOrNil:]_block_invoke

We also attach the full crash log to give full context of the crash.

Any idea why this crash happens and is there anything i can do to prevent the crashes from happening again?

This looks suspiciously similar to the issue discussed here.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Hi, thank you for responding!

Yes I have read that thread previously, despite that it has kind of similarity (which is caused by Pegasus-PIP), but it seems like that issue has been resolved in iOS 16, while my issue only occurred in iOS 16, I suspect that it's actually just the same issue, but somehow, the stack trace message is just little bit different from iOS 15

I have the same crash and its only in iOS16

I am having the same problem with iOS16. I can't reproduce it at hand, but from looking at user crash logs. It seemed to crash when I re-set the player of a PlayerView that inherited the UIView as shown below.

https://developer.apple.com/documentation/avfoundation/avplayerlayer

Hello, we're having the same problem on iOS 16+.

The crash happens when calling removeAllItems on AVQueuePlayer. We're also using a PlayerView that has an AVPlayerLayer.

We are also seeing this crash exclusively on iOS 16.0 up through iOS 16.1 (latest at time of this comment), starting from when the iOS 16 beta was released. Has anyone found any reproduction steps or work arounds? We are using a base AVPlayer instance, not AVQueuePlayer, in our app.

Still no resolution for this crash? I'm experiencing the same crash (on iOS 16 only) when trying to attach an AVPlayer instance to an AVPlayerLayer.

Hello! I am still experiencing this issue only on iOS 16 devices. Did someone find anything that could help?

There seems to be some kind of a loop going on right after setting the player at line 23. @eskimo is this of any help?

Yes, I have same problem, and I can reproduce it. We have AVPlayerLayer which switch two players content and ads.

  1. I activated PIP mode.
  2. Wait ads started
  3. When ads complete, I catch crash.

I'm getting a crash similar to PaulSarmale

 Fatal Exception: NSInvalidArgumentException
 0 CoreFoundation         0x9e88 __exceptionPreprocess
 1 libobjc.A.dylib        0x178d8 objc_exception_throw
 2 CoreFoundation         0x1af0a8 -[__NSCFString characterAtIndex:].cold.1
 3 CoreFoundation         0x1abad0 -[__NSArrayM insertObject:atIndex:].cold.2
 4 CoreFoundation         0x13f90 -[__NSArrayM insertObject:atIndex:]
 5 AVFCore            0x17e58 __22-[AVPlayer _addLayer:]_block_invoke
 6 libdispatch.dylib       0x3fdc _dispatch_client_callout
 7 libdispatch.dylib       0x13574 _dispatch_lane_barrier_sync_invoke_and_complete
 8 AVFCore            0x17548 -[AVPlayer _addLayer:]
 9 AVFCore            0x17400 -[AVPlayerLayer _setPlayer:forPIP:]
 10 Foundation           0x3f814 -[NSObject(NSKeyValueObservingPrivate) _changeValueForKeys:count:maybeOldValuesDict:maybeNewValuesDict:usingBlock:]
 11 Foundation           0x3f590 -[NSObject(NSKeyValueObservingPrivate) _changeValueForKey:key:key:usingBlock:]
 12 Foundation           0x3eb24 _NSSetObjectValueAndNotify
 13 AVFCore            0x176bc -[AVPlayer _addLayer:]
 14 AVFCore            0x17400 -[AVPlayerLayer _setPlayer:forPIP:]
 15 Foundation           0x3f814 -[NSObject(NSKeyValueObservingPrivate) _changeValueForKeys:count:maybeOldValuesDict:maybeNewValuesDict:usingBlock:]
 16 Foundation           0x3f590 -[NSObject(NSKeyValueObservingPrivate) _changeValueForKey:key:key:usingBlock:]
 17 Foundation           0x3eb24 _NSSetObjectValueAndNotify
 18 AVKit             0x21724 -[__AVPlayerLayerView setPlayerController:]
 19 AVKit             0x1fdac -[AVPlayerViewController setPlayerController:]
 20 Foundation           0x3f814 -[NSObject(NSKeyValueObservingPrivate) _changeValueForKeys:count:maybeOldValuesDict:maybeNewValuesDict:usingBlock:]
 21 Foundation           0x3f590 -[NSObject(NSKeyValueObservingPrivate) _changeValueForKey:key:key:usingBlock:]
 22 Foundation           0x3eb24 _NSSetObjectValueAndNotify
 23 AVKit             0x307f4 -[AVPlayerViewController setPlayer:]
 24 <APPNAME>           0x7b8418 closure #1 in AVPlayerBackgroundAudio.setupBecomeActiveSubscription() (<compiler-generated>)
 25 Combine            0xb754 Subscribers.Sink.receive(_:)
 26 Combine            0xb0a8 protocol witness for Subscriber.receive(_:) in conformance Subscribers.Sink<A, B>
 27 Combine            0x12904 closure #1 in Publishers.ReceiveOn.Inner.receive(_:)
 28 libswiftDispatch.dylib     0xf58 thunk for @escaping @callee_guaranteed () -> ()
 29 libdispatch.dylib       0x24b4 _dispatch_call_block_and_release
 30 libdispatch.dylib       0x3fdc _dispatch_client_callout
 31 libdispatch.dylib       0x127f4 _dispatch_main_queue_drain
 32 libdispatch.dylib       0x12444 _dispatch_main_queue_callback_4CF
 33 CoreFoundation         0x9a6f8 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__
 34 CoreFoundation         0x7c058 __CFRunLoopRun
 35 CoreFoundation         0x80ed4 CFRunLoopRunSpecific
 36 GraphicsServices        0x1368 GSEventRunModal
 37 UIKitCore           0x3a23d0 -[UIApplication _run]
 38 UIKitCore           0x3a2034 UIApplicationMain
 39 <APPNAME>           0x7f74 main + 32 (AppDelegate.swift:32)
 )

My code is trying to re-attach the player when coming back from the background:

NotificationCenter.default.publisher(for: UIApplication.didBecomeActiveNotification)
      .receive(on: DispatchQueue.main)
      .sink { [weak self] _ in
        guard let self else { return }
         
        if self.playerController?.player == nil && self.player != nil {
          self.playerController?.player = self.player
        }
      }
      .store(in: &cancellables)

I even check that the player is not nil directly before the call. Also, it should be valid to assign it to nil anyways.

This crash still happens on iOS 16.3.1, I also use AVPlayerLayer. Is there any workaround to fix this? Or we should wait for fix on next ios update?

Guys try this instead, helped me, using layerClass seems to be an issue, not sure why this works though

We're also having the same issue, and it's still ongoing (about a year later), however the stats tell that this crash only happens to users below iOS 16.4. So it seems fixed (or at least not happening anymore) since iOS 16.4. We also target iPadOS, only 1% of crashes happen there but again no iPadOS crashes since 16.4.

Crash on [AVPlayer _addLayer] in iOS 16 caused by Pegasus
 
 
Q