fluttercandies / flutter_photo_manager

A Flutter plugin that provides images, videos, and audio abstraction management APIs without interface integration, available on Android, iOS, macOS and OpenHarmony.
https://pub.dev/packages/photo_manager
Apache License 2.0
688 stars 314 forks source link

[BUG] iOS does not handle detachFromEngine and may cause crashes using the library notifier when the app has been killed. #841

Closed AlessandroToschi closed 1 month ago

AlessandroToschi commented 2 years ago

Describe the bug

We're experiencing a crash when using the library change notifiers and the user quits (force exit) the app on iOS.

Our app is a photo video editor and when users kill the app after export sometimes we receive the library change notification since we've saved a new image within the gallery.

This happens when the flutter engine has already been torn down, and so the method channel is not available anymore.

I'm able to reproduce this crash sometimes as well as Crashlytics reports them happening in background.

Here is the crash:


Fatal Exception: NSInternalInconsistencyException
Sending a message before the FlutterEngine has been run.

Fatal Exception: NSInternalInconsistencyException
0  CoreFoundation                 0x92d1c __exceptionPreprocess
1  libobjc.A.dylib                0x14ee4 objc_exception_throw
2  Foundation                     0x124c80 _userInfoForFileAndLine
3  Flutter                        0x128f4 (Missing UUID 7e68762f5edc377ebd2428e6c1570c2c)
4  photo_manager                  0x12e6c -[PMNotificationManager photoLibraryDidChange:] + 68 (PMNotificationManager.m:68)
5  Photos                         0x1224b0 __40-[PHPhotoLibrary _processPendingChanges]_block_invoke.920
6  Photos                         0x1f8c -[PHChange preloadChangeDetailsForFetchResults:inManagedObjectContext:handler:]
7  Photos                         0x19034 -[PHPhotoLibrary _processPendingChanges]
8  Photos                         0x1226bc __42-[PHPhotoLibrary handleMergeNotification:]_block_invoke.909
9  PhotoLibraryServicesCore       0xae08 __pl_dispatch_after_block_invoke
10 libdispatch.dylib              0x63094 _dispatch_call_block_and_release
11 libdispatch.dylib              0x64094 _dispatch_client_callout
12 libdispatch.dylib              0xa73c _dispatch_lane_serial_drain$VARIANT$mp
13 libdispatch.dylib              0xb1f4 _dispatch_lane_invoke$VARIANT$mp
14 libdispatch.dylib              0x14ec8 _dispatch_workloop_worker_thread
15 libsystem_pthread.dylib        0x1e10 _pthread_wqthread
16 libsystem_pthread.dylib        0x193c start_wqthread

To Reproduce Steps to reproduce the behavior:

  1. Register to the library change notifications.
  2. Save an asset to the library.
  3. Force quit the app just after the save.
  4. Crash happens on background and may be reported in TestFlight or Crashlytics.

Expected behavior The plugin should react to detachFromEngine event and set to nil each method handler on each method channel.

Flutter version 3.3.1

Smartphone (please complete the following information):

AlessandroToschi commented 2 years ago

If PR are welcomed, I opened one #839

anchaoLu commented 1 year ago

Same issue, how can we fixed it?

dodatw commented 1 year ago

Same here, this is my Top-1 crash issue in firebase.

dodatw commented 1 year ago

@CaiJingLong I found a solution here, could you take a look and merge to master ? https://github.com/janeleelee/flutter_photo_manager-2.6.0/commit/e1fb107ee2a300e0634e6b52ef89e9f68c70a37e

Thx

CaiJingLong commented 11 months ago

Can someone provide a complete example?

dodatw commented 11 months ago

For my case, I cannot reproduce from my side. I only see crash in crashlytics. From crashlytics info, it crash when app in background. More like crash when app terminal.

CaiJingLong commented 11 months ago

Is it possible to extract a reproducible Demo?

dodatw commented 2 months ago

Is it possible to extract a reproducible Demo?

Hi @CaiJingLong ,

We cannot reproduce from our side, we only see log than crash in background. We tried this fixing and release to store more than 1 year, https://github.com/janeleelee/flutter_photo_manager-2.6.0/commit/e1fb107ee2a300e0634e6b52ef89e9f68c70a37e This crash not happen any more.

Can you review the fixing and merge in master ? We want to use photo_manager in master in long term.

Thanks,

AlexV525 commented 1 month ago

I've added detaching state awareness implementation in https://github.com/fluttercandies/flutter_photo_manager/pull/1186. It should be available in the next release.