[firebase_messaging]: Android: a burst of background messages starts a FlutterEngine per service start, and can lose all of them
メンテナーはふだん 1 日以内に返信
評価
- 難易度
- 1/5
- 見積もり時間
- 1時間未満
- 初心者へのやさしさ
- 45/100
調査の方向性
Read FlutterFirebaseMessagingBackgroundExecutor.java around line 140 and trace how startBackgroundIsolate() assigns the engine before the queued callback runs. Reproduce with a burst of background data messages on Android, then check that only one engine is created and every queued message is handled. The issue reports a one-line fix and a successful 12-message run, though it does not name a test file.
索引モデルが issue の本文から書いたものです。
説明
Is there an existing issue for this?
- I have searched the existing issues.
Which plugins are affected?
Messaging
Which platforms are affected?
Android
Description
When several data messages reach an app that isn't running (a device switched on, or back online, after missing a few), FlutterFirebaseMessagingBackgroundExecutor creates a background FlutterEngine for every FlutterFirebaseMessagingBackgroundService start that lands before the first engine exists, instead of one.
Expected: one background engine, and every message handled.
Actual: one engine per early service start. On a profile build, 12 messages held while the device was off, then boot: 6 × Creating background FlutterEngine instance. and 6 × FlutterFirebaseMessagingBackgroundService started! in one process. On a debug build, 22 held messages: 22 engines, then the low-memory killer, and no notification shown.
Cause: onCreate calls startBackgroundIsolate(), whose guard is if (backgroundFlutterEngine != null) return;. But the engine is only assigned inside the callback that method posts to the main looper after ensureInitializationCompleteAsync. Every service start before then posts another callback. The callback checks only isNotRunning(), which stays true until Dart sends MessagingBackground#initialized, so each one creates a new engine, overwrites backgroundFlutterEngine and backgroundChannel, and starts another isolate. The queued intents drain only in onInitialized. Their work was already completed, so if the process dies first (memory pressure from N engines) they are lost.
Fix (FlutterFirebaseMessagingBackgroundExecutor.java, L140 on 16.7.0 and on main at 90c22c8dd9):
- if (isNotRunning()) {
+ if (isNotRunning() && backgroundFlutterEngine == null) {
onCreate, the posted runnable and its callback all run on the main looper, so once the first callback assigns the engine, every later one skips. With that change, the same 12-message boot burst makes one engine and shows all 12 notifications.
Reproducing the issue
- Register a handler with
FirebaseMessaging.onBackgroundMessage. - Power the device off.
- Send ≥10 high-priority, data-only messages to it.
- Boot the device without opening the app.
- Count
Creating background FlutterEngine instance.in logcat: one per early service start, not one.
A profile or release build shows the duplicate engines cleanly. On a debug build the first engine's kernel load on the main thread (7–10 s) can also trip a background ANR on the pending FCM broadcast, which is a separate, debug-only effect.
Firebase Core version
4.15.0
Flutter Version
3.47.7
Relevant Log Output
# Profile build, 12 held data messages, device booted without opening the app (one process):
I FLTFireBGExecutor: Creating background FlutterEngine instance. (×6)
I FLTFireMsgService: FlutterFirebaseMessagingBackgroundService started! (×6)
# The same burst with the one-line fix:
I FLTFireBGExecutor: Creating background FlutterEngine instance. (×1)
I FLTFireMsgService: FlutterFirebaseMessagingBackgroundService started! (×1)
Flutter dependencies
firebase_messaging 16.7.0, firebase_core 4.15.0.
Additional context and comments
- Versions: firebase_messaging 16.7.0 and
mainat 90c22c8dd9. Earlier 16.x releases (checked: 16.4.2, 16.4.3, 16.6.0) have the sameisNotRunning()-only check in the callback. - Tested on an Android 16 emulator.
- We're carrying the one-line change above as a build-time patch until a fix ships, and we're happy to open a PR with it if that helps.
- 主要言語
- Dart
- スター
- 9.3k
- フォーク
- 4.1k
- 平均マージ
- 1日 15時間
- マージ済み PR(30日)
- 82
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
firebase/flutterfire のほかの issue
-
[firebase_messaging]: Android ANR during background service destruction — main thread blocked in releaseWakeLock対応中かも @SelaseKay が 4 日前に担当しました。 オープンNeeds Attention platform: android plugin: messaging reproduced type: bug
難易度 4/5 3〜5日 初心者へのやさしさ 35/100
firebase/flutterfire#18866 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
blocked: customer-response Needs Attention platform: ios plugin: messaging Stale type: bug
難易度 4/5 3〜5日 初心者へのやさしさ 48/100
firebase/flutterfire#18776 · コメント 5 件 ·
メンテナーはふだん 1 日以内に返信
-
[firebase_storage] Windows native crash in upload progress callback, sink ownership and thread dispatch再び着手できるかも このイシューのプルリクエストはマージされずにクローズされました。 オープンblocked: flutter platform: windows plugin: storage reproduced type: bug
難易度 5/5 1週間以上 初心者へのやさしさ 25/100
firebase/flutterfire#18664 · コメント 3 件 ·
メンテナーはふだん 1 日以内に返信
-
[core] Decouple platform_interface from Flutter for pure-Dart web再び着手できるかも @Lyokone が 40 日前に担当しましたが、オープン中のプルリクエストはありません。 オープンNeeds Attention platform: all platform: web plugin: core type: enhancement
難易度 5/5 1週間以上 初心者へのやさしさ 35/100
firebase/flutterfire#18645 · コメント 3 件 · リアクション 2 件 · 担当者 1 名 ·
メンテナーはふだん 1 日以内に返信
-
blocked: firebase-sdk platform: android plugin: messaging reproduced type: bug
難易度 3/5 1〜2日 初心者へのやさしさ 56/100
firebase/flutterfire#18644 · コメント 3 件 ·
メンテナーはふだん 1 日以内に返信
firebase/flutterfire の issue をすべて見る
似ている issue
-
good first issue help wanted translation
難易度 2/5 1〜3時間 初心者へのやさしさ 86/100
blankframe-tech/ThrottleIQ#23 ·
メンテナーはふだん 5 日以内に返信
-
good first issue mobile P0 size:S
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 69/100
trufi-association/trufi-core#1024 ·
メンテナーはふだん 14 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 83/100
-
@apps/catalog enhancement
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
ymm-oss/flutter-mobile-project-template#699 ·
メンテナーはふだん 15 日以内に返信