[firebase_database, windows] Listener callbacks are delivered on a non-platform thread and crash the process
Maintainers usually reply within 1 day
Nobody has claimed this yet.
- #18656 by @SelaseKay — closed without merging
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 52/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Domain
- databases, desktop-dev
Research direction
Start in windows/firebase_database_plugin.cpp, focusing on VL, the child listener, and OnCancelInternal. Run the repeated subscribe/unsubscribe reproduction on Windows and trace callback and listener lifetimes. Done means listener events reach EventSink on the platform thread and cancellation cannot free a listener while the SDK may still invoke it.
Written by the indexing model from the issue text.
Description
Bug report
Which plugins are affected?
Realtime Database
Which platforms are affected?
Windows
Description
On Windows, firebase_database delivers Realtime Database listener callbacks straight from the
Firebase C++ SDK's callback thread into the Flutter EventSink. The engine reports the violation on
every event, and the process eventually dies.
[ERROR:flutter/shell/common/shell.cc(1183)] The 'plugins.flutter.io/firebase_database/query/322_...'
channel sent a message from native to Flutter on a non-platform thread. Platform channel messages
must be sent on the platform thread. Failure to do so may result in data loss or crashes, and must
be fixed in the plugin or application code creating that channel.
In windows/firebase_database_plugin.cpp (12.5.0), the value listener and the child listener both
call events_->Success(...) from inside the SDK callback, and there is no PostTask / task-runner
hop anywhere in the file:
class VL : public firebase::database::ValueListener {
void OnValueChanged(const DataSnapshot& snapshot) override {
...
events_->Success(EncodableValue(event)); // SDK callback thread
}
flutter::EventSink<flutter::EncodableValue>* events_;
};
Separately, OnCancelInternal runs on the platform thread and deletes the listener:
if (value_listener_) {
query_.RemoveValueListener(value_listener_);
delete value_listener_; // races with an in-flight OnValueChanged
value_listener_ = nullptr;
}
so cancelling a subscription while a listen response is still being delivered frees the listener out
from under the callback thread.
Steps to reproduce
- Windows desktop app with
firebase_database. - Subscribe and unsubscribe repeatedly so that some cancels land while a listen response is in
flight (this is what screen transitions do in a long-lived session):
var i = 0;
Timer.periodic(const Duration(milliseconds: 40), (_) {
i++;
// vary the path so each listen creates a new query rather than joining an existing one
final sub = FirebaseDatabase.instance.ref('some/path/${i % 64}').onValue.listen((_) {});
Future<void>.delayed(Duration(milliseconds: i * 7 % 100), sub.cancel);
});
- The console fills with the
non-platform threaderror above, and the process dies (flutter run
printsLost connection to device.).
Expected behavior
Listener events are marshalled to the platform thread before touching the EventSink, and
cancelling a subscription does not free a listener that the SDK may still be calling into.
Actual behavior
Every event triggers the engine's non-platform-thread error, and the process terminates. Reproduced
in about 30 seconds, repeatably, on two builds.
Additional context
We hit this in production, in an app where several clients have to keep the same state in sync over
Realtime Database, so a listener stays attached for the whole session. After switching our Windows
Realtime Database usage from a third-party Dart implementation to firebase_database, crashes on
Windows increased. The same app versions on iOS showed no change.
Note this is not the same as the C++ SDK's own use-after-free
(firebase/firebase-cpp-sdk#1881, fixed in v13.11.0). We tested both:
| firebase_core / bundled firebase-cpp-sdk | firebase_database | Result |
|---|---|---|
| 4.10.0 / 13.5.0 | 12.4.2 | crashes in ~30s |
| 4.14.0 / 13.11.0 | 12.5.0 | crashes in ~30s |
Reverting our Windows Realtime Database usage to the third-party Dart implementation survived the
same loop for over 10 minutes with no thread-violation errors, so the remaining defect is in this
plugin's Windows implementation rather than in the C++ SDK.
Related, previously closed for lack of detail: #17800 (firebase_auth), #17801 (cloud_firestore).
Those report the same non-platform-thread violation on Windows in sibling plugins, so the fix may
need to be applied consistently.
Flutter doctor / versions
firebase_core4.14.0,firebase_database12.5.0- Windows Server 2025 x64
- Dominant language
- Dart
- Stars
- 9.3k
- Forks
- 4.1k
- Avg merge
- 1d 22h
- Merged PRs (30d)
- 79
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from firebase/flutterfire
-
blocked: customer-response Needs Attention platform: ios plugin: messaging Stale type: bug
Difficulty 4/5 3-5 days Newbie friendliness 48/100
firebase/flutterfire#18776 · 5 comments ·
Maintainers usually reply within 1 day
-
[firebase_storage] Windows native crash in upload progress callback, sink ownership and thread dispatchMay be free again A pull request for this issue was closed without being merged. Openblocked: flutter platform: windows plugin: storage reproduced type: bug
Difficulty 5/5 Over a week Newbie friendliness 25/100
firebase/flutterfire#18664 · 3 comments ·
Maintainers usually reply within 1 day
-
[core] Decouple platform_interface from Flutter for pure-Dart webMay be free again @Lyokone claimed this 36 days ago, and no pull request is open. OpenNeeds Attention platform: all platform: web plugin: core type: enhancement
Difficulty 5/5 Over a week Newbie friendliness 35/100
firebase/flutterfire#18645 · 3 comments · 2 reactions · 1 assignee ·
Maintainers usually reply within 1 day
-
blocked: firebase-sdk platform: android plugin: messaging reproduced type: bug
Difficulty 3/5 1-2 days Newbie friendliness 56/100
firebase/flutterfire#18644 · 3 comments ·
Maintainers usually reply within 1 day
-
Needs Attention plugin: auth type: enhancement
Difficulty 5/5 Over a week Newbie friendliness 25/100
firebase/flutterfire#18324 · 4 reactions ·
Maintainers usually reply within 1 day
All issues in firebase/flutterfire
Similar issues
-
enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 77/100
atsign-foundation/at_server#2896 ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
lugassawan/swe-workbench#823 ·
Maintainers usually reply within 1 day
-
auto-implement
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
bannzai/kashakeibo#142 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 1/5 1-3 hours Newbie friendliness 82/100
meddouribadis/arclauncher#53 ·
Maintainers usually reply within 1 day
-
p: material_ui package team-design
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
flutter/flutter#195096 · 1 comment ·
Maintainers usually reply within 1 day