Repository navigation
Conversation
…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.
Using Gemini Code AssistThe 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
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 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. |
…ix/storage-task-lifecycle # Conflicts: # packages/firebase_storage/firebase_storage_platform_interface/lib/src/method_channel/method_channel_task.dart # packages/firebase_storage/firebase_storage_platform_interface/test/method_channel_tests/method_channel_task_test.dart
…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.
Description
Two
firebase_storagetask lifecycle bugs, both reproduced onmain.1. Cancelling the last
snapshotEventslistener cancelled the task, andawait taskthen hung (all native platforms).MethodChannelTaskexposedasBroadcastStream(onCancel: (sub) => sub.cancel()), so when the app's only listener went away (snapshotEvents.first, or aStreamBuilderin a progress dialog that closes), the native event stream was cancelled for good:TaskStateChannelStreamHandler.onCancelthen calledandroidTask.cancel(), so the upload was silently cancelled;await tasklistened to the already-finished stream:.lastthrew "No element", the error was swallowed andonCompletenever completed (on iOS the upload finishes, but Dart never hears about it).It only shows when nothing else is listening at that moment, e.g. when the app awaits the task after the dialog closed.
Fix:
snapshotandonComplete. Users get a separate broadcast stream.onCompletealso fails, instead of hanging, if the native stream closes without a final state or the task fails to start (that error is now aFirebaseException).onCancelonly stops sending events. The task keeps running, can still be paused, resumed or cancelled by handle, and its listeners still release it when it ends.2. Android task and channel registries were static, so engines shared them.
inProgressTasks(inFlutterFirebaseStorageTask) andeventChannels/streamHandlers(inFlutterFirebaseStoragePlugin's companion) were shared by every engine. When a background engine (workmanager and similar) detached, itsonDetachedFromEnginecancelled the main engine's uploads and removed its event channels, so the main engine'sawait taskhung. Handles come from a per-isolate Dart counter that starts at 0, so tasks from two engines also overwrote each other. #18255 fixed only the crash.Fix: each plugin instance owns its registries (
FlutterFirebaseStorageTaskRegistryfor tasks), so detaching an engine only cancels its own tasks and removes its own channels. Removing a finished task doesn't evict a newer task that reused its handle.Behaviour change (Android)
Disposing a
StreamBuilder, a rebuild, orawait for … breakno longer cancels the upload or download. The task runs until it ends ortask.cancel()is called, as on iOS and web. A paused task the app forgets is kept for the life of the process. This is also in the commit body so it reaches the CHANGELOG.Known limitations (not changed here)
FlutterFirebasePluginRegistrykeeps only the last plugin instance. After a background engine attaches, a hot restart of the main engine no longer cancels the main engine's tasks, and may cancel the background engine's instead.TaskState.canceleddoesn't complete a pendingonComplete, and after a failed starttask.snapshot.statestaysrunning. Both predate this PR, and no native platform sendscanceledtoday.Coordination
onCancelto release the task. It doesn't conflict: with this PR the Dart side only cancels the native stream after the final event, and the Android comment no longer claims parity with iOS.android_unit_tests.yamlgradlew line will conflict with fix(remote_config): deliver onConfigUpdated errors on Android and iOS as FirebaseException with stable codes #18840, fix(database,android): stop a pending transaction handler from blocking the database forever #18836, fix(messaging,android): deliver every foreground message to onMessage exactly once #18831 and fix(firestore): chunk large Android query snapshots #18601. Whichever merges later keeps every:<plugin>:testDebugUnitTesttask.Tests
Dart unit tests (
firebase_storage_platform_interface/test/method_channel_tests/method_channel_task_test.dart, 9 tests):onCompleteresolves after that;onCompleteandsnapshotEventsfail when the native stream closes early;onCompletefails when the task fails to start;onComplete, an error with no listener only failsonComplete, and a late listener gets done.The first 6 fail on
main(hang or wrong count), and all 9 pass with this change.Android JUnit (
FlutterFirebaseStorageTaskRegistryTest, 3 tests, added toandroid-unit-tests): the same handle in two engines doesn't collide,removeAllonly affects its own engine, and a staleremovekeeps a newer task with the same handle. They cover the new class, so they can't run onmain; the e2e test below is the before/after check.e2e (
example/integration_test/task_e2e.dart):snapshotEventslistener is cancelled (all platforms):snapshotEvents.first, a short delay, thenawait task, and the uploaded object must have the full size.tests/engineschannel in the example'sMainActivityattaches the plugin to a secondFlutterEngineand destroys it while a 10 MB upload runs.Results on the Android emulator against the storage emulator:
mainThe whole storage e2e suite passes on Android with this PR, using the CI flags (93 passed, 9 skipped). iOS, macOS, Windows and web were not run locally, so CI covers them.
Review fixes
snapshotEvents.firstnow has the file's 60 s timeout.STORAGE_EMULATOR_DEBUG), the second engine is destroyed about 230 ms in and the upload ends at about 2.6 s. On the old Android code the test still fails: the emulator log shows the upload cancelled after its first 256 KB chunk, and the test times out. On CI, the 20 MB upload took about 1.8 s.onCancelcomment no longer says "as on iOS", which fix(storage, apple): release finished tasks instead of keeping them for the life of the process #18747 would make false.Related Issues
Follow-up to #18240 / #18255, which fixed only the crash on detach.
Checklist
///).melos run analyze) does not report any problems on my PR.Breaking Change