Skip to content

FIRESTORE INTERNAL ASSERTION FAILED: QueryComparator needs to have a key ordering. (expected has_key_ordering) #7672

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

Closed
isharing0726 opened this issue Mar 7, 2021 · 11 comments
Assignees

Comments

@isharing0726
Copy link

isharing0726 commented Mar 7, 2021

[REQUIRED] Step 1: Describe your environment

  • Xcode version: 12.4
  • Firebase SDK version: 6.32.2
  • Installation method: CocoaPods
  • Firebase Component: Firestore

[REQUIRED] Step 2: Describe the problem

Fatal Exception: NSInternalInconsistencyException

FIRESTORE INTERNAL ASSERTION FAILED: QueryComparator needs to have a key ordering. (expected has_key_ordering)
Fatal Exception: NSInternalInconsistencyException

0  CoreFoundation                 0x186369654 __exceptionPreprocess
1  libobjc.A.dylib                0x18608bbcc objc_exception_throw
2  CoreFoundation                 0x18626c6ec +[_CFXNotificationTokenRegistration keyCallbacks]
3  Foundation                     0x186700584 -[NSAssertionHandler handleFailureInFunction:file:lineNumber:description:]
4  iSharing                       0x100900180 firebase::firestore::util::ObjcThrowHandler(firebase::firestore::util::ExceptionType, char const*, char const*, int, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) + 59 (exception_apple.mm:59)
5  iSharing                       0x1008ffe34 firebase::firestore::util::Throw(firebase::firestore::util::ExceptionType, char const*, char const*, int, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) + 90 (exception.cc:90)
6  iSharing                       0x10094e0d4 firebase::firestore::util::internal::FailAssertion(char const*, char const*, int, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, char const*) + 42 (hard_assert.cc:42)
7  iSharing                       0x10094e174 firebase::firestore::util::internal::FailAssertion(char const*, char const*, int, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, char const*) + 49 (hard_assert.cc:49)
8  iSharing                       0x10098c35c firebase::firestore::core::Query::Comparator() const + 295 (query.cc:295)
9  iSharing                       0x1009c9e4c firebase::firestore::core::View::View(firebase::firestore::core::Query, firebase::firestore::immutable::SortedSet<firebase::firestore::model::DocumentKey, firebase::firestore::util::Comparator<firebase::firestore::model::DocumentKey> >) + 89 (view.cc:89)
10 iSharing                       0x1009b4008 firebase::firestore::core::SyncEngine::InitializeViewAndComputeSnapshot(firebase::firestore::core::Query const&, int) + 98 (sorted_map.h:98)
11 iSharing                       0x1009b377c firebase::firestore::core::SyncEngine::Listen(firebase::firestore::core::Query) + 435 (vector:435)
12 iSharing                       0x1008fce04 firebase::firestore::core::EventManager::AddQueryListener(std::__1::shared_ptr<firebase::firestore::core::QueryListener>) + 60 (event_manager.cc:60)
13 iSharing                       0x10092562c std::__1::__function::__func<firebase::firestore::core::FirestoreClient::ListenToQuery(firebase::firestore::core::Query, firebase::firestore::core::ListenOptions, std::__1::shared_ptr<firebase::firestore::core::EventListener<firebase::firestore::core::ViewSnapshot> >&&)::$_10, std::__1::allocator<firebase::firestore::core::FirestoreClient::ListenToQuery(firebase::firestore::core::Query, firebase::firestore::core::ListenOptions, std::__1::shared_ptr<firebase::firestore::core::EventListener<firebase::firestore::core::ViewSnapshot> >&&)::$_10>, void ()>::operator()() + 4211 (memory:4211)
14 iSharing                       0x1008e1598 firebase::firestore::util::AsyncQueue::ExecuteBlocking(std::__1::function<void ()> const&) + 957 (atomic:957)
15 iSharing                       0x1009c0330 firebase::firestore::util::Task::ExecuteAndRelease() + 1859 (functional:1859)
16 libdispatch.dylib              0x18602f524 _dispatch_client_callout
17 libdispatch.dylib              0x185fdb8a4 _dispatch_lane_serial_drain$VARIANT$mp
18 libdispatch.dylib              0x185fdc294 _dispatch_lane_invoke$VARIANT$mp
19 libdispatch.dylib              0x185fe578c _dispatch_workloop_worker_thread
20 libsystem_pthread.dylib        0x186080b74 _pthread_wqthread
21 libsystem_pthread.dylib        0x186083740 start_wqthread

Steps to reproduce:

Occurring on lots of customer devices. Don't know how to reproduce it.

Relevant Code:

- (void)getChatMessages:(NSString*)room
                 token:(NSString *)token
                 source:(FIRFirestoreSource)source
            completion:(void(^)(NSError * _Nullable error, NSArray<ChatMessage *> * _Nonnull messageList, NSString * _Nullable nextToken))completion
{
    const NSUInteger FETCH_COUNT = 30;
    FIRCollectionReference *ref = [[[self.db collectionWithPath:SCHEMA_CHAT_MESSAGES]
                                               documentWithPath:room]
                                             collectionWithPath:COLLECTION_MESSAGES];
    
    FIRQuery *query = [ref queryOrderedByField:@"timestamp" descending:YES];
    if (token) {
        NSInteger startTimestamp = [token integerValue];
        query = [query queryStartingAtValues:@[@(startTimestamp)]];
    }
    query = [query queryLimitedTo:FETCH_COUNT];
    
    [query getDocumentsWithSource:source
                       completion:^(FIRQuerySnapshot * _Nullable snapshot,
                                    NSError * _Nullable error) {
        if (snapshot == nil) {
            ELog(@"ClientAPIFS Error fetching document: %@", error);
            return;
        }
        
        NSMutableArray *messageList = [[NSMutableArray alloc] init];
        for (FIRQueryDocumentSnapshot *doc in snapshot.documents) {
            ChatMessage *message = [[ChatMessage alloc] initWithKey:doc.documentID data:doc.data];
            message.room = room;
            [messageList addObject:message];
        }
        
        NSString *nextToken = nil;
        if (snapshot.documents.count >= FETCH_COUNT) {
            FIRQueryDocumentSnapshot *doc = [snapshot.documents lastObject];
            NSNumber *timestamp = doc.data[@"timestamp"];
            nextToken =  [timestamp stringValue];
        }
        
        completion(nil, messageList, nextToken);
    }];
}

- (void)listenChatMessageChangeFor:(sw_id_t)friendId room:(NSString *) room onReceive:(void(^)(ChatMessage *data))callback
{
    
    const NSInteger DELAY_THRESHOLD = 60;
    const NSInteger now = [[NSDate date] timeIntervalSince1970] - DELAY_THRESHOLD;

    FIRCollectionReference *ref = [[[self.db collectionWithPath:SCHEMA_CHAT_MESSAGES]
                                               documentWithPath:room]
                                             collectionWithPath:COLLECTION_MESSAGES];
    
    if (_messageChangedListener){
        [_messageChangedListener remove];
    }
    _messageChangedListener = [ref addSnapshotListener:^(FIRQuerySnapshot * _Nullable snapshot,
                                                         NSError * _Nullable error) {
        
        if (snapshot == nil) {
            ELog(@"ClientAPIFS Error fetching document: %@", error);
            return;
        }
        
        for (FIRDocumentChange *diff in snapshot.documentChanges) {
            if (diff.type == FIRDocumentChangeTypeAdded) {
                ChatMessage *message = [[ChatMessage alloc] initWithKey:diff.document.documentID data:diff.document.data];
                message.room = room;
                
                if (message.timestamp >= now) {
                    self.callbackMessageAdded(message);
                }
            }
            
            if (diff.type == FIRDocumentChangeTypeModified) {
                ChatMessage *message = [[ChatMessage alloc] initWithKey:diff.document.documentID data:diff.document.data];
                message.room = room;
                callback(message);
            }
        }
    }];

}
@rafikhan
Copy link

rafikhan commented Mar 8, 2021

Can you enable logging in your app and share the contents from a crashing device?

@isharing0726
Copy link
Author

Can you enable logging in your app and share the contents from a crashing device?

No, I can't. the crash comes from customer devices. It is not reproduced on my device.

@rafikhan
Copy link

rafikhan commented Mar 8, 2021

Can you provide more context on what is happening in the application when the crash occurs?

@morganchen12
Copy link
Contributor

@isharing0726 looks like you're using an older major version of Firebase, can you try updating to the latest version to see if the crash has already been fixed since then?

@isharing0726
Copy link
Author

isharing0726 commented Mar 8, 2021

Can you provide more context on what is happening in the application when the crash occurs?

The above functions (listenChatMessageChangeFor and getChatMessages) might be called right before the crash.

When the crash occurred, there was a thread executing the following.

`
com.google.firebase.firestore.rpc

0 libsystem_kernel.dylib 0x1bf6c2b5c poll + 8
1 grpc 0x103be9548 pollset_work(grpc_pollset*, grpc_pollset_worker**, long long) + 1172
2 grpc 0x103beb3e0 pollset_work(grpc_pollset*, grpc_pollset_worker**, long long) + 72
3 grpc 0x103bdd738 cq_next(grpc_completion_queue*, gpr_timespec, void*) + 520
4 grpcpp 0x103f67550 grpc_impl::CompletionQueue::AsyncNextInternal(void**, bool*, gpr_timespec) + 60
5 iSharing 0x100836f9c firebase::firestore::remote::Datastore::PollGrpcQueue() + 135 (datastore.cc:135)
6 iSharing 0x100910330 firebase::firestore::util::Task::ExecuteAndRelease() + 1859 (functional:1859)
7 libdispatch.dylib 0x1933ba280 _dispatch_client_callout + 16
8 libdispatch.dylib 0x193362dcc _dispatch_lane_serial_drain$VARIANT$mp + 612
9 libdispatch.dylib 0x1933638a8 _dispatch_lane_invoke$VARIANT$mp + 424
10 libdispatch.dylib 0x19336d338 _dispatch_workloop_worker_thread + 712
11 libsystem_pthread.dylib 0x1dbfa55a4 _pthread_wqthread + 272
12 libsystem_pthread.dylib 0x1dbfa8874 start_wqthread + 8
`

@rafikhan
Copy link

rafikhan commented Mar 8, 2021

As @morganchen12 was saying, can you upgrade to the latest version of the SDKs ad try again?

@isharing0726
Copy link
Author

As @morganchen12 was saying, can you upgrade to the latest version of the SDKs ad try again?

ok, I'll update to the latest and let you know.

@schmidt-sebastian schmidt-sebastian self-assigned this Mar 11, 2021
@schmidt-sebastian
Copy link
Contributor

@isharing0726 Do you know if this crash occurs with or without the query = [query queryStartingAtValues:@[@(startTimestamp)]]; call?

@isharing0726
Copy link
Author

@isharing0726 Do you know if this crash occurs with or without the query = [query queryStartingAtValues:@[@(startTimestamp)]]; call?

I don't know. I can't verify it because the crash is occurring in customer devices.

@morganchen12
Copy link
Contributor

@isharing0726 is this still an issue with the latest SDK version?

@isharing0726
Copy link
Author

@isharing0726 is this still an issue with the latest SDK version?

It is resolved. We're using 7.3.0.

@firebase firebase locked and limited conversation to collaborators Apr 18, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants