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

Flutter randomly throws "StateError: Bad state: ImageStream has been disposed" on iOS/Android #110953

Closed
stx opened this issue Sep 4, 2022 · 20 comments
Labels
a: images Loading, displaying, rendering images a: production Issues experienced in live production apps c: crash Stack traces logged to the console c: regression It was better in the past than it is now f: scrolling Viewports, list views, slivers, etc. framework flutter/packages/flutter repository. See also f: labels. needs repro info Automated crash report whose cause isn't yet known P2 Important issues not at the top of the work list r: invalid Issue is closed as not valid

Comments

@stx
Copy link

stx commented Sep 4, 2022

We upgraded to Flutter 3.3.0 and we're rarely seeing a new error on production apps with both iOS and Android users. It seems more common on Android. We've seen it be triggered when background the app, when a device is low or out of memory, and while using the app normally.

StateError: Bad state: Stream has been disposed.
An ImageStream is considered disposed once at least one listener has been added and subsequently all listeners have been removed and no handles are outstanding from the keepAlive method.
To resolve this error, maintain at least one listener on the stream, or create an ImageStreamCompleterHandle from the keepAlive method, or create a new stream for the image.
  File "image_stream.dart", line 602, in ImageStreamCompleter._checkDisposed
  File "image_stream.dart", line 742, in ImageStreamCompleter.reportImageChunkEvent
  File "zone.dart", line 1399, in _rootRunUnary
  File "zone.dart", line 1300, in _CustomZone.runUnary
  File "zone.dart", line 1209, in _CustomZone.runUnaryGuarded
  File "stream_impl.dart", line 339, in _BufferingStreamSubscription._sendData
  File "stream_impl.dart", line 515, in _DelayedData.perform
  File "stream_impl.dart", line 620, in _PendingEvents.handleNext
  File "stream_impl.dart", line 591, in _PendingEvents.schedule.<fn>
  File "zone.dart", line 1383, in _rootRun
  File "zone.dart", line 1293, in _CustomZone.run
  File "zone.dart", line 1201, in _CustomZone.runGuarded
  File "zone.dart", line 1241, in _CustomZone.bindCallbackGuarded.<fn>
  File "zone.dart", line 1391, in _rootRun
  File "zone.dart", line 1293, in _CustomZone.run
  File "zone.dart", line 1201, in _CustomZone.runGuarded
  File "zone.dart", line 1241, in _CustomZone.bindCallbackGuarded.<fn>
  File "schedule_microtask.dart", line 40, in _microtaskLoop
  File "schedule_microtask.dart", line 49, in _startMicrotaskLoop

Steps to Reproduce

We're not able to reproduce it.

[✓] Flutter (Channel stable, 3.3.0, on macOS 13.0 22A5331f darwin-arm, locale
    en-US)
    • Flutter version 3.3.0 on channel stable at
      /Users/james/Development/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision ffccd96b62 (6 days ago), 2022-08-29 17:28:57 -0700
    • Engine revision 5e9e0e0aa8
    • Dart version 2.18.0
    • DevTools version 2.15.0

[✓] Android toolchain - develop for Android devices (Android SDK version
    32.1.0-rc1)
    • Android SDK at /Users/james/Library/Android/sdk
    • Platform android-33, build-tools 32.1.0-rc1
    • Java binary at: /Applications/Android
      Studio.app/Contents/jre/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build
      11.0.12+0-b1504.28-7817840)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 14.0)
    • Xcode at /Applications/Xcode-beta.app/Contents/Developer
    • Build 14A5228q
    • CocoaPods version 1.11.3

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2021.2)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build
      11.0.12+0-b1504.28-7817840)

[✓] VS Code (version 1.71.0)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.46.0

[✓] Connected device (3 available)
    • Pixel 6 (mobile) • 18201FDF6007XH • android-arm64  • Android 12 (API 32)
    • macOS (desktop)  • macos          • darwin-arm64   • macOS 13.0 22A5331f
      darwin-arm
    • Chrome (web)     • chrome         • web-javascript • Google Chrome
      104.0.5112.101

[✓] HTTP Host Availability
    • All required HTTP hosts are available

• No issues found!
@huycozy huycozy added the in triage Presently being triaged by the triage team label Sep 5, 2022
@huycozy
Copy link
Member

huycozy commented Sep 5, 2022

Hi @stx, thanks for filing the issue but there is not enough information here so we can verify the issue.
For the issue to be workable, it needs to be reproducible with a completed and minimal reproducible code sample that doesn’t include 3rd party plugins or complex production code. Please provide such a sample.
Thank you!

@huycozy huycozy added the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label Sep 5, 2022
@huycozy
Copy link
Member

huycozy commented Sep 13, 2022

Hi @stx
As an issue reported recently in #111342 which the OP has given the logs from Crashlytics, it seems that trying to reproduce in the local environment is unachievable.

Also, there was a closed issue #77576 which has been fixed before (a similar log is reported at #77576 (comment)) but this issue is still reported on flutter 3.3.0 in this issue and 3.3.1 in #111342.

Before labeling the issue, please help to clarify the information indicated below:

Thank you!

@stx
Copy link
Author

stx commented Sep 13, 2022

Hi @stx As an issue reported recently in #111342 which the OP has given the logs from Crashlytics, it seems that trying to reproduce in the local environment is unachievable.

Also, there was a closed issue #77576 which has been fixed before (a similar log is reported at #77576 (comment)) but this issue is still reported on flutter 3.3.0 in this issue and 3.3.1 in #111342.

Before labeling the issue, please help to clarify the information indicated below:

Thank you!

Thanks @huycozy!

Images in a stack and in maps (tiles). 3.0.5. Same issue as #111342. Maybe the same as #77576. We don't use evict directly but maps use CachedNetworkImage that might.

SM-S908W
iPhone14,2
iPad Air (3rd generation)
SM-G781B
etc.

@github-actions github-actions bot removed the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label Sep 13, 2022
@huycozy
Copy link
Member

huycozy commented Sep 13, 2022

@stx Thanks for your quick response. Labeling this issue for further investigation from the team.

@huycozy huycozy added c: crash Stack traces logged to the console framework flutter/packages/flutter repository. See also f: labels. f: scrolling Viewports, list views, slivers, etc. a: images Loading, displaying, rendering images a: production Issues experienced in live production apps needs repro info Automated crash report whose cause isn't yet known c: regression It was better in the past than it is now and removed in triage Presently being triaged by the triage team labels Sep 13, 2022
@goderbauer goderbauer added the P2 Important issues not at the top of the work list label Sep 13, 2022
@stx stx changed the title "StateError: Bad state: ImageStream has been disposed" when app is backgrounded Flutter randomly throws "StateError: Bad state: ImageStream has been disposed" on iOS/Android Sep 22, 2022
@PawlikMichal25
Copy link

@huycozy any update?

We're getting quite a lot of this exception right now:
Screenshot 2022-09-26 at 16 03 52
and it's clearly a regression, because it never happened before in our app.

The stacktrace unfortunately doesn't say precisely where the issue is happening:

Stacktrace Non-fatal Exception: io.flutter.plugins.firebase.crashlytics.FlutterError: Bad state: Stream has been disposed. An ImageStream is considered disposed once at least one listener has been added and subsequently all listeners have been removed and no handles are outstanding from the keepAlive method. To resolve this error, maintain at least one listener on the stream, or create an ImageStreamCompleterHandle from the keepAlive method, or create a new stream for the image.. Error thrown null. at ImageStreamCompleter._checkDisposed(image_stream.dart:602) at ImageStreamCompleter.reportImageChunkEvent(image_stream.dart:742)

@huycozy
Copy link
Member

huycozy commented Sep 27, 2022

Hi @PawlikMichal25
This issue has been prioritized with p4 which means the team agrees that it is important to work on. You might want to take a look at this: How issue prioritization works

@avinash-kanjiya
Copy link

Is this issue fixed in version 3.3.3 or not?

@iapicca
Copy link
Contributor

iapicca commented Sep 29, 2022

Steps to Reproduce

We're not able to reproduce it.

Is this issue fixed in version 3.3.3 or not?

@avinash-kanjiya @PawlikMichal25
I think it would be very hard for the team to fix an issue w/o any repro code
a code sample would probably help to speed up the process

@stx
Copy link
Author

stx commented Sep 29, 2022

We identified that this issue was caused by CachedNetworkImage (https://pub.dev/packages/cached_network_image).

We replaced the caching mechanism and the production errors stopped completely.

As a result, it appears that this issue may be identical to #77576.

I suspect it may be due to the evict() call in this deprecated function in CachedNetworkImage:

  @Deprecated(
      '_loadAsync is deprecated, use loadBuffer instead, see https://docs.flutter.dev/release/breaking-changes/image-provider-load-buffer')
  Stream<ui.Codec> _loadAsync(
    image_provider.CachedNetworkImageProvider key,
    StreamController<ImageChunkEvent> chunkEvents,
    DecoderCallback decode,
  ) {
    assert(key == this);
    return ImageLoader().loadAsync(
      url,
      cacheKey,
      chunkEvents,
      decode,
      cacheManager ?? DefaultCacheManager(),
      maxHeight,
      maxWidth,
      headers,
      errorListener,
      imageRenderMethodForWeb,
      () => PaintingBinding.instance.imageCache.evict(key),
    );
  }

@iapicca
Copy link
Contributor

iapicca commented Sep 29, 2022

We identified that this issue was caused by CachedNetworkImage (https://pub.dev/packages/cached_network_image).

We replaced the caching mechanism and the production errors stopped completely.

As a result, it appears that this issue may be identical to #77576.

I suspect it may be due to the evict() call in this deprecated function in CachedNetworkImage:
[...]

@stx
this looks like good work, maybe would be good notify the maintainer of cached_network_image

@luisphp89
Copy link

Identificamos que esse problema foi causado por CachedNetworkImage ( https://pub.dev/packages/cached_network_image ).

Substituímos o mecanismo de cache e os erros de produção pararam completamente.

Como resultado, parece que esse problema pode ser idêntico ao #77576 .

Eu suspeito que pode ser devido à evict()chamada nesta função obsoleta em CachedNetworkImage:

  @Deprecated(
      '_loadAsync is deprecated, use loadBuffer instead, see https://docs.flutter.dev/release/breaking-changes/image-provider-load-buffer')
  Stream<ui.Codec> _loadAsync(
    image_provider.CachedNetworkImageProvider key,
    StreamController<ImageChunkEvent> chunkEvents,
    DecoderCallback decode,
  ) {
    assert(key == this);
    return ImageLoader().loadAsync(
      url,
      cacheKey,
      chunkEvents,
      decode,
      cacheManager ?? DefaultCacheManager(),
      maxHeight,
      maxWidth,
      headers,
      errorListener,
      imageRenderMethodForWeb,
      () => PaintingBinding.instance.imageCache.evict(key),
    );
  }

Can you show the code you used to solve the problem?

@nerder
Copy link

nerder commented Oct 5, 2022

@stx What's the workaround you've used? Should we expect this to be fixed from Flutter or from CachedNetworkImage?

Also besides polluting the logs, what's the actual issue that the user is facing here?

@iapicca
Copy link
Contributor

iapicca commented Oct 5, 2022

@huycozy are you sure it's an issue w/ flutter and not CachedNetworkImage?
see #110953 (comment)

@huycozy
Copy link
Member

huycozy commented Oct 6, 2022

@iapicca thanks for reminding me.

@stx Based on your findings in #110953 (comment), I will close this issue since it's CachedNetworkImage issue as confirmed.

I also see @PawlikMichal25 raised an issue on that repository with Baseflow/flutter_cached_network_image#785. So, please leave there your comment and react an upvote to the original comment will help to raise the issue.

For anyone else facing this issue without using CachedNetworkImage, please file a new issue so that we may verify it. Thanks!

@huycozy huycozy closed this as completed Oct 6, 2022
@huycozy huycozy added r: invalid Issue is closed as not valid r: solved Issue is closed as solved and removed r: solved Issue is closed as solved labels Oct 6, 2022
@dgilperez
Copy link

@stx thanks for identifying this! if you could share some hint on your workaround, it would be very helpful for us

@stx
Copy link
Author

stx commented Oct 6, 2022

@stx thanks for identifying this! if you could share some hint on your workaround, it would be very helpful for us

Deleting the package.

@dgilperez
Copy link

Thanks for that @stx, we just followed your advice and we're a happier team now :)

We jumped to https://github.com/fluttercandies/extended_image in case anyone is interested. We'll be testing it in prod during the following days but it seems to run nice for now.

@micohalvarez-dart
Copy link

@dgilperez hi how's the https://github.com/fluttercandies/extended_image? is it causing same issue as CachedNetworkImage?

@dgilperez
Copy link

dgilperez commented Oct 14, 2022

Flawless so far, in terms of errors. I didn't do any proper benchmark to compare them. It just works for us

@github-actions
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 28, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
a: images Loading, displaying, rendering images a: production Issues experienced in live production apps c: crash Stack traces logged to the console c: regression It was better in the past than it is now f: scrolling Viewports, list views, slivers, etc. framework flutter/packages/flutter repository. See also f: labels. needs repro info Automated crash report whose cause isn't yet known P2 Important issues not at the top of the work list r: invalid Issue is closed as not valid
Projects
None yet
Development

No branches or pull requests

10 participants