Skip to content

fix(storage): keep tasks alive when snapshotEvents listeners cancel, and scope Android task registries per engine - #18839

Open
Lyokone wants to merge 2 commits into
mainfrom
fix/storage-task-lifecycle
Open

Lyokone wants to merge 2 commits into
mainfrom
fix/storage-task-lifecycle

Conversation

@Lyokone

@Lyokone Lyokone commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Description

Fixes two task bugs in firebase_storage.

1. Cancelling the last snapshotEvents listener breaks the task.
When the only listener goes away (snapshotEvents.first, or a progress dialog's StreamBuilder closing), the native event stream is cancelled. On Android this also cancels the upload. On every native platform, a later await task never completes.

Fix: the task keeps its own internal listener, so await task always gets the final state. On Android, cancelling a listener only stops the events. If the native stream ends without a final state, await task now throws instead of hanging.

2. On Android, all engines share one task list.
When a background engine (workmanager, for example) detaches, it cancels the main engine's uploads, and the main engine's await task hangs. Fix: each engine keeps its own tasks and event channels.

Behaviour change (Android): disposing a StreamBuilder no longer cancels the upload or download. Call task.cancel() to stop it, as on iOS and web.

Tests

  • Dart (method_channel_task_test.dart): cancelling the only listener keeps the native stream and onComplete working; onComplete throws if the stream closes early or the task fails to start. These hang or fail on main.
  • Android JUnit (FlutterFirebaseStorageTaskRegistryTest): two engines can use the same task handle without affecting each other.
  • e2e (task_e2e.dart): await task completes after its only listener is cancelled (all platforms), and detaching the plugin from a second engine doesn't stop this engine's upload (Android). Both time out on main.

Related Issues

Follow-up to #18240 / #18255, which only fixed the crash on detach.

Checklist

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • My PR includes unit or integration tests for all changed/updated/fixed behaviors (See Contributor Guide).
  • All existing and new tests are passing.
  • I updated/added relevant documentation (doc comments with ///).
  • The analyzer (melos run analyze) does not report any problems on my PR.
  • I read and followed the Flutter Style Guide.
  • I signed the CLA.
  • I am willing to follow-up on review comments in a timely manner.

Breaking Change

  • No, this is not a breaking change.

@gemini-code-assist

Copy link
Copy Markdown
Contributor
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

…and scope Android task registries per engine

- Dart: one internal subscription now drives `snapshot` and `onComplete`, and
  users get a separate broadcast stream. Cancelling the last `snapshotEvents`
  listener no longer cancels the native event stream (which cancelled the
  upload on Android) and `await task` no longer hangs afterwards. `onComplete`
  also fails instead of hanging when the native stream closes without a final
  state or the task fails to start.
- Android: cancelling a task's event stream only stops sending events, as on
  iOS. In-progress tasks and event channels are tracked per plugin instance,
  so detaching a background engine no longer cancels the main engine's tasks
  or tears down its streams, and same-numbered handles from different engines
  no longer collide.
…the engine-detach upload

- The listener-cancel e2e test now times out its `snapshotEvents.first` wait,
  like the rest of the file.
- The engine-detach e2e test uploads 10 MB instead of 20 MB. On the Android
  emulator it is still uploading about 2 s after the second engine is
  destroyed, and it still fails on the old Android code.
- The Android `onCancel` comment no longer claims parity with iOS.

Android behaviour change (from this PR): disposing a `StreamBuilder`, a
rebuild, or `await for ... break` no longer cancels the upload or download.
The task keeps running until it ends or is cancelled by `task.cancel()`, as on
iOS and web. A paused task the app forgets is kept for the life of the
process.
@Lyokone
Lyokone force-pushed the fix/storage-task-lifecycle branch from de1d1c9 to 001a177 Compare October 29, 2026 13:10
@Lyokone
Lyokone marked this pull request as ready for review October 29, 2026 14:32

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant