Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 [cloud_firestore] Still ServerTimestampBehavior crashes #10715

Closed
tecbot opened this issue Apr 3, 2023 · 21 comments
Closed

🐛 [cloud_firestore] Still ServerTimestampBehavior crashes #10715

tecbot opened this issue Apr 3, 2023 · 21 comments
Assignees
Labels
blocked: customer-response Waiting for customer response, e.g. more information was requested. platform: ios Issues / PRs which are specifically for iOS. plugin: cloud_firestore resolution: fixed A fix has been merged or is pending merge from a PR. Stale Issue with no recent activity type: bug Something isn't working type: crash A compile error or crash

Comments

@tecbot
Copy link

tecbot commented Apr 3, 2023

Bug report

Describe the bug
Still crashes related to ServerTimestampBehavior after bug fix #10391 and #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)
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)
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)
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)
@tecbot tecbot added Needs Attention This issue needs maintainer attention. type: bug Something isn't working labels Apr 3, 2023
@danagbemava-nc danagbemava-nc added the triage Issue is currently being triaged. label Apr 4, 2023
@danagbemava-nc
Copy link

Hi @tecbot, can you provide a minimal sample? Can you also check if it reproduces on the latest version of cloud_firestore?

@danagbemava-nc danagbemava-nc added blocked: customer-response Waiting for customer response, e.g. more information was requested. and removed Needs Attention This issue needs maintainer attention. labels Apr 4, 2023
@tecbot
Copy link
Author

tecbot commented Apr 4, 2023

@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?

@google-oss-bot google-oss-bot added Needs Attention This issue needs maintainer attention. and removed blocked: customer-response Waiting for customer response, e.g. more information was requested. labels Apr 4, 2023
@danagbemava-nc
Copy link

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

@danagbemava-nc danagbemava-nc added plugin: cloud_firestore platform: ios Issues / PRs which are specifically for iOS. type: crash A compile error or crash and removed Needs Attention This issue needs maintainer attention. triage Issue is currently being triaged. labels Apr 5, 2023
@Lyokone
Copy link
Contributor

Lyokone commented Apr 5, 2023

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 #10728, could you see if it solves your issue?

@tecbot
Copy link
Author

tecbot commented Apr 5, 2023

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

@Lyokone
Copy link
Contributor

Lyokone commented Apr 6, 2023

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.

@markglibres
Copy link

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.

@mazoRen
Copy link

mazoRen commented Apr 12, 2023

@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

@markglibres
Copy link

markglibres commented Apr 18, 2023

@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

@Lyokone
Copy link
Contributor

Lyokone commented Apr 19, 2023

Thanks for your research, it will help to find the origin of the issue.

@markglibres
Copy link

hey @Lyokone ... any luck on finding the bug? looking forward to use offline mode again.. :)

@markglibres
Copy link

@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

@Lyokone
Copy link
Contributor

Lyokone commented May 2, 2023

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 👍

@markglibres
Copy link

markglibres commented May 24, 2023

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.

@hukusuke1007
Copy link

hukusuke1007 commented Jul 19, 2023

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.

@hukusuke1007
Copy link

When will this be fixed? I'm in a lot of trouble.

@paulw11
Copy link
Contributor

paulw11 commented Aug 21, 2023

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.

paulw11 added a commit to paulw11/flutterfire that referenced this issue Aug 21, 2023
use dispatch_once to instantiate `serverTimestampMap`
Fix firebase#10715
paulw11 added a commit to paulw11/flutterfire that referenced this issue Aug 21, 2023
Use thread-safe `NSCache` instead of `NSMutableDictionary`
Do not bypass getter
Consistently use static property
USe `dispatch_once` to ensure single instantiation of cache.
@paulw11
Copy link
Contributor

paulw11 commented Sep 18, 2023

From our internal monitoring this issue has been resolved by #11501

@russellwheatley
Copy link
Member

Thanks for the update, @paulw11. Could you also confirm, @tecbot? Be sure to install the latest version of cloud_firestore which contains the fix 👍

@russellwheatley russellwheatley added the blocked: customer-response Waiting for customer response, e.g. more information was requested. label Sep 21, 2023
@google-oss-bot
Copy link

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!

@google-oss-bot google-oss-bot added the Stale Issue with no recent activity label Oct 2, 2023
@russellwheatley
Copy link
Member

Closing out as I believe this issue has been resolved with this fix #11501

@danagbemava-nc danagbemava-nc added the resolution: fixed A fix has been merged or is pending merge from a PR. label Oct 3, 2023
@firebase firebase locked and limited conversation to collaborators Nov 2, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
blocked: customer-response Waiting for customer response, e.g. more information was requested. platform: ios Issues / PRs which are specifically for iOS. plugin: cloud_firestore resolution: fixed A fix has been merged or is pending merge from a PR. Stale Issue with no recent activity type: bug Something isn't working type: crash A compile error or crash
Projects
None yet
Development

No branches or pull requests

9 participants