๐ [cloud_firestore] Still ServerTimestampBehavior crashes
Bug report
Describe the bug Still crashes related to ServerTimestampBehavior after bug fix https://github.com/firebase/flutterfire/pull/10391 and https://github.com/firebase/flutterfire/issues/10314
cloud_firestore: 4.4.5
Exception Type: EXC_BAD_ACCESS (SIGSEGV)
Exception Subtype: KERN_INVALID_ADDRESS at 0x000078facf0484d0
0 libobjc.A.dylib 0x00000001cc0a1c20 objc_msgSend + 32
1 CoreFoundation 0x00000001d2cef994 -[__NSCFNumber compare:] + 64 (CFObject.m:1134)
2 CoreFoundation 0x00000001d2cef938 -[__NSCFNumber isEqualToNumber:] + 24 (CFObject.m:853)
3 CoreFoundation 0x00000001d2cee4c4 -[__NSDictionaryM objectForKeyedSubscript:] + 164 (NSDictionaryM_Common.h:32)
4 Runner 0x0000000100d811ac -[FLTFirebaseFirestoreWriter FIRQuerySnapshot:] + 204 (FLTFirebaseFirestoreWriter.m:199)
5 Runner 0x0000000100d805e8 -[FLTFirebaseFirestoreWriter writeValue:] + 832 (FLTFirebaseFirestoreWriter.m:47)
2.
Exception Type: EXC_BAD_ACCESS (SIGSEGV)
EXC_BAD_ACCESS KERN_INVALID_ADDRESS 0x00004e9d6e9fc9f0
0 libobjc.A.dylib 0x2820 objc_msgSend + 32
1 CoreFoundation 0x32ec -[__NSCFNumber compare:] + 64
2 CoreFoundation 0x3290 -[__NSCFNumber isEqualToNumber:] + 24
3 CoreFoundation 0x1e24 -[__NSDictionaryM objectForKeyedSubscript:] + 164
4 Runner 0x478d64 -[FLTFirebaseFirestoreWriter FIRDocumentSnapshot:] + 153 (FLTFirebaseFirestoreWriter.m:153)
5 Runner 0x478548 -[FLTFirebaseFirestoreWriter writeValue:] + 43 (FLTFirebaseFirestoreWriter.m:43)
3.
EXC_BREAKPOINT 0x0000000196699318
0 CoreFoundation 0x7e318 CFNumberGetValue + 160
1 CoreFoundation 0x64170 -[__NSCFNumber unsignedLongLongValue] + 48
2 CoreFoundation 0x35c0 -[__NSCFNumber compare:] + 124
3 CoreFoundation 0x3528 -[__NSCFNumber isEqualToNumber:] + 24
4 CoreFoundation 0xa0dc mdict_removeObjectForKey + 164
5 Runner 0x478dd8 -[FLTFirebaseFirestoreWriter FIRDocumentSnapshot:] + 159 (FLTFirebaseFirestoreWriter.m:159)
6 Runner 0x478548 -[FLTFirebaseFirestoreWriter writeValue:] + 43 (FLTFirebaseFirestoreWriter.m:43)
4.
Exception Type: EXC_BAD_ACCESS (SIGSEGV)
Exception Subtype: KERN_INVALID_ADDRESS at 0x0000f3b4d44e4bb0 -> 0x00000034d44e4bb0 (possible pointer authentication failure)
0 libobjc.A.dylib 0x00000001bcb0dc20 objc_msgSend + 32 (:-1)
1 CoreFoundation 0x00000001c39950dc mdict_removeObjectForKey + 164 (NSDictionaryM_Common.h:32)
2 Runner 0x0000000104c84dd8 -[FLTFirebaseFirestoreWriter FIRDocumentSnapshot:] + 252 (FLTFirebaseFirestoreWriter.m:159)
3 Runner 0x0000000104c84548 -[FLTFirebaseFirestoreWriter writeValue:] + 672 (FLTFirebaseFirestoreWriter.m:43)
Hi @tecbot, can you provide a minimal sample? Can you also check if it reproduces on the latest version of cloud_firestore?
@danagbemava-nc I can't create a minimal sample because this error happen only in production and only for a rare amount of users. There are no relevant code changes between 4.4.5 and 4.5.0 which could fix this issue, therefore im sure this issue still exists in 4.5.0. Are those 3 stack traces not helpful?
Hi @tecbot, it's standard procedure to request reporters to try on newer versions whenever a bug is reported on an "older" version of a package.
Since this is a production bug with no steps to reproduce, I'll be labeling this based on the crash logs provided above.
cc @Lyokone
Hello @tecbot, Are you using ServerTimestampBehavior in your code? Can you at least provide the code used to call Firestore? Are you also using a background runner that could call native calls simultaneously?
I have a tentative fix in https://github.com/firebase/flutterfire/pull/10728, could you see if it solves your issue?
Hey @Lyokone. No we don't use this feature. We have upgraded from version 4.2.0 to 4.4.5 and the crashes started to occur. We have 100+ places where we access firestore in our app. Which of them could be the root of this I can't figure out form the crash logs, sorry.
We have some background plugins but none of them accessing firestore.
Regarding the fix in #10728 we could try this out. Approx. I can give you an answer in ~7 days after we have slowed rolled out to customers. However I think the fix also needs to be included in the FIRQuerySnapshot method. Because 1. Stacktrace happen on line 199
I've added a fix for line 199 as well. If it solves crashes on your end, I'll merge that. I suspect some kind of race conditions between 2 serialisations. I wasn't able to reproduce it with concurrent reads.
We encountered the same problem with intermittent users in production and noticed a similar log file. The issue is happening on a specific screen that was running a query snapshot.
@Lyokone๏ผ I encountered the same problem and modified it according to @Lyokone's method, but it still crashed.
The Log๏ผ
Crashed: dev.flutter.firebase.firestore 0 libobjc.A.dylib 0x7974 lookUpImpOrForward + 92 1 libobjc.A.dylib 0x20c4 _objc_msgSend_uncached + 68 2 CoreFoundation 0x2104 -[__NSDictionaryM objectForKeyedSubscript:] + 164 3 Runner 0x48d344 -[FLTFirebaseFirestoreWriter FIRDocumentSnapshot:] + 160 (FLTFirebaseFirestoreWriter.m:160) 4 Runner 0x48cae4 -[FLTFirebaseFirestoreWriter writeValue:] + 43 (FLTFirebaseFirestoreWriter.m:43) 5 Flutter 0x591bd0 (Missing UUID 4c4c44e655553144a165b4c4c8109699) 6 Flutter 0x58e48c (Missing UUID 4c4c44e655553144a165b4c4c8109699) 7 Runner 0x48a094 __63-[FLTFirebaseFirestorePlugin documentGet:withMethodCallResult:]_block_invoke + 483 (FLTFirebaseFirestorePlugin.m:483) 8 Runner 0x21b73c -[FIRDocumentReference wrapDocumentSnapshotBlock:]::Converter::OnEvent(firebase::firestore::util::StatusOrfirebase::firestore::api::DocumentSnapshot) + 440324 9 Runner 0x206c74 firebase::firestore::api::DocumentReference::GetDocument(firebase::firestore::api::Source, std::__1::unique_ptr<firebase::firestore::core::EventListenerfirebase::firestore::api::DocumentSnapshot, std::__1::default_delete<firebase::firestore::core::EventListenerfirebase::firestore::api::DocumentSnapshot > >&&)::ListenOnce::OnEvent(firebase::firestore::util::StatusOrfirebase::firestore::api::DocumentSnapshot) + 355644 10 Runner 0x2078a0 firebase::firestore::api::DocumentReference::AddSnapshotListener(firebase::firestore::core::ListenOptions, std::__1::unique_ptr<firebase::firestore::core::EventListenerfirebase::firestore::api::DocumentSnapshot, std::__1::default_delete<firebase::firestore::core::EventListenerfirebase::firestore::api::DocumentSnapshot > >&&)::Converter::OnEvent(firebase::firestore::util::StatusOrfirebase::firestore::core::ViewSnapshot) + 358760 11 Runner 0x2504e8 void std::__1::__invoke_void_return_wrapper<void, true>::__call<firebase::firestore::core::AsyncEventListenerfirebase::firestore::core::ViewSnapshot::OnEvent(firebase::firestore::util::StatusOrfirebase::firestore::core::ViewSnapshot)::'lambda'()&>(firebase::firestore::core::AsyncEventListenerfirebase::firestore::core::ViewSnapshot::OnEvent(firebase::firestore::util::StatusOrfirebase::firestore::core::ViewSnapshot)::'lambda'()&) + 656816 12 Runner 0x310cd8 firebase::firestore::util::Task::ExecuteAndRelease() + 1445280 13 libdispatch.dylib 0x3fdc _dispatch_client_callout + 20 14 libdispatch.dylib 0xb694 _dispatch_lane_serial_drain + 672 15 libdispatch.dylib 0xc1e0 _dispatch_lane_invoke + 384 16 libdispatch.dylib 0x16e10 _dispatch_workloop_worker_thread + 652 17 libsystem_pthread.dylib 0xdf8 _pthread_wqthread + 288 18 libsystem_pthread.dylib 0xb98 start_wqthread + 8
@Lyokone, after doing some trial-and-error, we were able to stop the crashes by setting firestore persistent storage = false and set snapshot includeMetadataChanges = false....
hope this gets fixed so we can take advantage of reading offline data
Thanks for your research, it will help to find the origin of the issue.
hey @Lyokone ... any luck on finding the bug? looking forward to use offline mode again.. :)
@Lyokone .. FYI: we upgrade cloud_firestore to version 4.5.3 then enabled the persistence with "includeMetadata" on snapshot and no more crashes. I think this ticket #10579) has fixed the issue
Good to know, thanks for letting me know; it was in my backlog for this week.
It's probably why it was hard to reproduce, I'll see to add some tests around this ๐
hey @Lyokone, with the latest version of Flutter and Firestore, we have this error but for now can only replicate the error on iPhone 12 with offline mode enabled.. iPhone 14 and others were ok.
Same issue??
This is log.
#2 0x00000001011f2590 in -[FLTFirebaseFirestoreWriter FIRDocumentSnapshot:] at /Users/shohei/.pub-cache/hosted/pub.dev/cloud_firestore-4.8.2/ios/Classes/FLTFirebaseFirestoreWriter.m:159
#3 0x00000001011f1d74 in -[FLTFirebaseFirestoreWriter writeValue:] at /Users/shohei/.pub-cache/hosted/pub.dev/cloud_firestore-4.8.2/ios/Classes/FLTFirebaseFirestoreWriter.m:43
Package version.
firebase_core: ^2.14.0
cloud_firestore: ^4.8.2
Occurs in the production environment. But also occurs in simulator.
When will this be fixed? I'm in a lot of trouble.
We have also seen crashes related to this. Looking at the code, the implementation of serverTimestampMap in FLTFirebaseFirestorePlugin.m is simply not thread-safe. Line 74 contains a previous attempt at a fix, which instantiates a new dictionary if the property is nil, but this has probably made things worse, if anything.
In Objective C you need to consider multi-threading. Neither properties of objects nor structures such as NSDictionary are inherently thread safe. As a static property, serverTimestampMap should probably be created via a dispatch_once block, rather than instantiated lazily by which ever threads encounter a nil.
Updates to the dictionary itself need to be protected via some form of exclusive access mechanism such as a semaphore.
From our internal monitoring this issue has been resolved by #11501
Thanks for the update, @paulw11. Could you also confirm, @tecbot? Be sure to install the latest version of cloud_firestore which contains the fix ๐
Hey @tecbot. We need more information to resolve this issue but there hasn't been an update in 7 weekdays. I'm marking the issue as stale and if there are no new updates in the next 7 days I will close it automatically.
If you have more information that will help us get to the bottom of this, just add a comment!