TheWidlarzGroup / react-native-video

A <Video /> component for react-native
http://thewidlarzgroup.github.io/react-native-video/
MIT License
7.13k stars 2.88k forks source link

iOS app gets killed by Springboard when going in background #1892

Closed bockc closed 1 month ago

bockc commented 4 years ago

Bug

App gets killed by Springboard on iOS when going in background. Stacktrace:

Exception Type:  EXC_CRASH (SIGKILL)
Exception Codes: 0x0000000000000000, 0x0000000000000000
Exception Note:  EXC_CORPSE_NOTIFY
Termination Reason: Namespace SPRINGBOARD, Code 0x8badf00d
Termination Description: SPRINGBOARD, scene-update watchdog transgression: cloud.myapp.myappMobile exhausted real (wall clock) time allowance of 10.00 seconds | ProcessVisibility: Foreground | ProcessState: Running | WatchdogEvent: scene-update | WatchdogVisibility: Foreground | WatchdogCPUStatistics: ( | "Elapsed total CPU time (seconds): 3.140 (user 3.140, system 0.000), 16% CPU", | "Elapsed application CPU time (seconds): 1.223, 6% CPU" | )
Triggered by Thread:  0

Thread 0 name:  Dispatch queue: com.apple.main-thread
Thread 0 Crashed:
0   libsystem_kernel.dylib          0x0000000229290130 semaphore_wait_trap + 8
1   libdispatch.dylib               0x00000002290df7a0 _dispatch_sema4_wait$VARIANT$mp + 24
2   libdispatch.dylib               0x00000002290e0220 _dispatch_semaphore_wait_slow + 140
3   AVFoundation                    0x000000022f773550 -[AVFigAssetInspectorLoader _ensureAllDependenciesOfKeyAreLoaded:] + 176
4   AVFoundation                    0x000000022f680770 -[AVAsset _mediaSelectionGroupDictionaries] + 48
5   AVFoundation                    0x000000022f680bc0 -[AVAsset mediaSelectionGroupForMediaCharacteristic:] + 280
6   myappMobile                         0x0000000100c777ac -[RCTVideo setMediaSelectionTrackForCharacteristic:withCriteria:] + 6764460 (RCTVideo.m:985)
7   myappMobile                         0x0000000100c77e68 -[RCTVideo setSelectedAudioTrack:] + 6766184 (RCTVideo.m:1030)
8   myappMobile                         0x0000000100c7754c -[RCTVideo applyModifiers] + 6763852 (RCTVideo.m:968)
9   myappMobile                         0x0000000100c6fa00 -[RCTVideo applicationWillEnterForeground:] + 6732288 (RCTVideo.m:232)
10  CoreFoundation                  0x000000022966fa28 __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 20
11  CoreFoundation                  0x000000022966f9f4 ___CFXRegistrationPost_block_invoke + 64
12  CoreFoundation                  0x000000022966eee8 _CFXRegistrationPost + 392
13  CoreFoundation                  0x000000022966eb94 ___CFXNotificationPost_block_invoke + 96
14  CoreFoundation                  0x00000002295e8474 -[_CFXNotificationRegistrar find:object:observer:enumerator:] + 1496
15  CoreFoundation                  0x000000022966e644 _CFXNotificationPost + 696
16  Foundation                      0x000000022a0571d4 -[NSNotificationCenter postNotificationName:object:userInfo:] + 68
17  UIKitCore                       0x0000000256026f80 -[UIApplication _sendWillEnterForegroundCallbacks] + 232
18  UIKitCore                       0x00000002558d1be4 -[__UICanvasLifecycleMonitor_Compatability activateEventsOnly:withContext:completion:] + 2036
19  UIKitCore                       0x00000002558cfb64 __82-[_UIApplicationCanvas _transitionLifecycleStateWithTransitionContext:completion:]_block_invoke + 744
20  UIKitCore                       0x00000002558cf82c -[_UIApplicationCanvas _transitionLifecycleStateWithTransitionContext:completion:] + 428
21  UIKitCore                       0x00000002558d436c __125-[_UICanvasLifecycleSettingsDiffAction performActionsForCanvas:withUpdatedScene:settingsDiff:fromSettings:transitionContext:]_block_invoke + 220
22  UIKitCore                       0x00000002558d5150 _performActionsWithDelayForTransitionContext + 112
23  UIKitCore                       0x00000002558d4224 -[_UICanvasLifecycleSettingsDiffAction performActionsForCanvas:withUpdatedScene:settingsDiff:fromSettings:transitionContext:] + 244
24  UIKitCore                       0x00000002558d8f24 -[_UICanvas scene:didUpdateWithDiff:transitionContext:completion:] + 360
25  UIKitCore                       0x0000000255c092b0 -[UIApplicationSceneClientAgent scene:handleEvent:withCompletion:] + 464
26  FrontBoardServices              0x000000022c07b5d8 __80-[FBSSceneImpl updater:didUpdateSettings:withDiff:transitionContext:completion:]_block_invoke_3 + 224
27  libdispatch.dylib               0x000000022913d7d4 _dispatch_client_callout + 16
28  libdispatch.dylib               0x00000002290e25dc _dispatch_block_invoke_direct$VARIANT$mp + 224
29  FrontBoardServices              0x000000022c0b5040 __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 40
30  FrontBoardServices              0x000000022c0b4cdc -[FBSSerialQueue _performNext] + 408
31  FrontBoardServices              0x000000022c0b5294 -[FBSSerialQueue _performNextFromRunLoopSource] + 52
32  CoreFoundation                  0x0000000229690728 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 24
33  CoreFoundation                  0x00000002296906a8 __CFRunLoopDoSource0 + 88
34  CoreFoundation                  0x000000022968ff90 __CFRunLoopDoSources0 + 176
35  CoreFoundation                  0x000000022968aecc __CFRunLoopRun + 1004
36  CoreFoundation                  0x000000022968a7c0 CFRunLoopRunSpecific + 436
37  GraphicsServices                0x000000022b88b79c GSEventRunModal + 104
38  UIKitCore                       0x000000025600fc38 UIApplicationMain + 212
39  myappMobile                     0x000000010060ba98 main + 31384 (main.m:16)
40  libdyld.dylib                   0x000000022914e8e0 start + 4

Platform

iOS

Which player are you experiencing the problem on:

Environment info

React native info output:

 System:
    OS: macOS 10.15.2
    CPU: (4) x64 Intel(R) Core(TM) i5-7500 CPU @ 3.40GHz
    Memory: 3.56 GB / 24.00 GB
    Shell: 5.7.1 - /bin/zsh
  Binaries:
    Node: 13.3.0 - /usr/local/bin/node
    Yarn: 1.21.1 - /usr/local/bin/yarn
    npm: 6.13.2 - /usr/local/bin/npm
    Watchman: 4.9.0 - /usr/local/bin/watchman
  SDKs:
    iOS SDK:
      Platforms: iOS 13.2, DriverKit 19.0, macOS 10.15, tvOS 13.2, watchOS 6.1
    Android SDK:
      API Levels: 23, 24, 25, 26, 27, 28, 29
      Build Tools: 23.0.1, 26.0.3, 27.0.3, 28.0.2, 28.0.3
      System Images: android-23 | Intel x86 Atom_64, android-23 | Google APIs Intel x86 Atom_64, android-27 | Intel x86 Atom_64, android-27 | Google APIs Intel x86 Atom
  IDEs:
    Android Studio: 3.4 AI-183.6156.11.34.5522156
    Xcode: 11.3.1/11C504 - /usr/bin/xcodebuild
  npmPackages:
    react: 16.9.0 => 16.9.0
    react-native: 0.61.5 => 0.61.5
  npmGlobalPackages:
    react-native-cli: 2.0.1

Library version: 5.1.0-alpha2 (doesn't work with 5.0.2 either)

Steps To Reproduce

  1. Render a view containing a video component
  2. Put the app in background
  3. Return to foreground, app does not respond for a few seconds, then crashes

Expected behaviour

  1. Don't really know, not crash

I think this happens because of the new Apple's guidelines concerning background activity for an app. Too much resources are used while the app is in background and then Springboard decides to kill the app to free resources. According to the stacktrace, I think that this library is the one responsible for the excess resources used.

rauchp commented 4 years ago

Have you found a fix for this? I'm seeing the same exact thing. You'd think more people would talk about it, considering this is the de facto video library for RN.

bockc commented 4 years ago

@rauchp I started using 5.1.0-alpha versions IIRC, and the issue disappeared. Don't know for sure if it's related, but give it a try. If you confirm me it works I'll close the issue

rauchp commented 4 years ago

@bockc Where are you getting that stacktrace from? I installed 5.1.0-alpha6. Yet I'm still crashing whenever I run in release mode and try using another app. It's soft crashes when I go back to my app, and Xcode doesn't report an error. It just says Send failed with error "Socket is not connected". I don't think it's the same problem you had, but it does smell to me like a Springboard error. Don't have a trace to prove it though.

bockc commented 4 years ago

@rauchp In XCode, go to Window -> Devices and Simulator (Shift+Cmd+2). Find the device you're running the app on, click View device logs and you'll see the device's logs there.

rauchp commented 4 years ago

Thanks! Looks like I'm getting the same thing you were getting so I'd keep the problem open, if you don't mind.

Exception Type:  EXC_CRASH (SIGKILL)
Exception Codes: 0x0000000000000000, 0x0000000000000000
Exception Note:  EXC_CORPSE_NOTIFY
Termination Reason: Namespace SPRINGBOARD, Code 0x8badf00d
Termination Description: SPRINGBOARD, process-exit watchdog transgression: application<co.outlier.RushDraft>:6511 exhausted real (wall clock) time allowance of 5.00 seconds | ProcessVisibility: Background | ProcessState: Running | WatchdogEvent: process-exit | WatchdogVisibility: Background | WatchdogCPUStatistics: ( | "Elapsed total CPU time (seconds): 10.140 (user 10.140, system 0.000), 34% CPU", | "Elapsed application CPU time (seconds): 0.004, 0% CPU" | )
Triggered by Thread:  0
bockc commented 4 years ago

That's strange, because I don't have them anymore with alpha versions. Also maybe you should check in the stacktrace if the issue comes from "AVFoundation" and paste the complete stacktrace. Otherwise it's pretty useless for mainteners :)

rauchp commented 4 years ago

Definitely mentions AVFoundation everywhere on the stack so I'm confident it's this library. Interestingly enough, only happens on one screen. So I'm debugging that screen specifically to see what part of it is causing this, and hope to report back to help the maintainers!

Norcy commented 3 years ago

I also encountered the same problem. Have you found the solution?

marf commented 3 years ago

Hello, we have the same problem here. Any solutions?

geroale commented 2 years ago

Same issue here

zuziaka commented 2 years ago

I have found the cause of the problem, but due to the difficult reproduction of the problem I have no way to verify.

Problem:

It looks like RCTVideo observes applicationWillEnterForeground and performs synchronously:

- (void)setMediaSelectionTrackForCharacteristic:(AVMediaCharacteristic)characteristic
                                   withCriteria:(NSDictionary *)criteria {
                                   ...
          AVMediaSelectionGroup *group = [_player.currentItem.asset mediaSelectionGroupForMediaCharacteristic:characteristic];
                                   ...
}          

before asset loads Audio/Subtitles. This sometimes blocks the main thread long enough for the application to be killed.

Suggested solution:

Wrap above method call with loadValuesAsynchronously:

  [_player.currentItem.asset loadValuesAsynchronouslyForKeys:@[@"allMediaSelections"] completionHandler:^{
      [self setSelectedAudioTrack:self->_selectedAudioTrack];
      [self setSelectedTextTrack:self->_selectedTextTrack];
  }];

In theory, this should unblock main thread in a applicationWillEnterForeground scope.

KrzysztofMoch commented 1 month ago

Can you please test it on latest stable version and check if this issue still persists - If yes I will re-open this