couchbase / couchbase-lite-android-ce

The community edition of couchbase lite for android
Apache License 2.0
9 stars 1 forks source link

Crash while upgrading from 2.1.5 to 2.6.0 #27

Closed K-Leon closed 5 years ago

K-Leon commented 5 years ago

First of Thanks for your hard work!! I just stumbled over a NDK Crash while installing 2.6 over an 2.1.5 Database.

********** Crash dump: **********
Build fingerprint: 'M3/msm8953_64/msm8953_64:7.1.1/NMF26F/Data.B05031742:user/release-keys'
#00 0x0000000000315424 /data/app/my.new.app.debug-2/lib/arm64/libLiteCoreJNI.so (_ZNK6fleece4impl5Value8asStringEv+24)
                                                                                             fleece::impl::Value::tag() const
                                                                                             /Users/myFolder/couchbase-lite-android-ce/couchbase-lite-java/../couchbase-lite-core/vendor/fleece/Fleece/Core/Value.hh:213:72
                                                                                             fleece::impl::Value::asString() const
                                                                                             /Users/myFolder/couchbase-lite-android-ce/couchbase-lite-core/vendor/fleece/Fleece/Core/Value.cc:207:0
#01 0x00000000002fd180 /data/app/my.new.app.debug-2/lib/arm64/libLiteCoreJNI.so (_ZNK6fleece4impl4Dict8iterator9keyStringEv+20)
                                                                                             fleece::impl::Dict::iterator::keyString() const
                                                                                             /Users/myFolder/couchbase-lite-android-ce/couchbase-lite-core/vendor/fleece/Fleece/Core/Dict.cc:422:30
#02 0x00000000002f6a00 /data/app/my.new.app.debug-2/lib/arm64/libLiteCoreJNI.so (FLDictIterator_GetKeyString+28)
                                                                                             FLDictIterator_GetKeyString
                                                                                             /Users/myFolder/couchbase-lite-android-ce/couchbase-lite-core/vendor/fleece/Fleece/API_Impl/Fleece.cc:319:34
#03 0x000000000011ce40 /data/app/my.new.app.debug-2/lib/arm64/libLiteCoreJNI.so (Java_com_couchbase_lite_internal_fleece_FLDictIterator_getKeyString+20)
                                                                                             Java_com_couchbase_lite_internal_fleece_FLDictIterator_getKeyString
                                                                                             /Users/myFolder/couchbase-lite-android-ce/couchbase-lite-java/src/main/cpp/native_fleece.cc:182:18
#04 0x000000000132d354 /data/app/my.new.app.debug-2/oat/arm64/base.odex (offset 0x11fa000)

May you look into it? (Taking self build 2.6 or stock 2.6 won't making any difference. Except I can symbolicate the crash dump )

Edit: Upgrading to 2.5.x works. The bug has been introduced lately i guess.

bmeike commented 5 years ago

@K-Leon Of course I'll look into it!

Can you get me a bit more of the log context? This one doesn't even say what happened!

Also, as close as you can get to steps to replicate would help a lot.

bawelter commented 5 years ago

I'm updating from 2.5.3 to 2.6.0 and I get this crash when I select the fields property in the data. So it might have something to do with that. If I remove the "fields" column from the select it will work fine.

Data Example:

{
  "cells": {},
  "channels": [
    ...
  ],
  "createdAt": "2019-09-12T14:09:00.859Z",
  "createdByIP": "###.###.###.###",
  "creator": "user::test@example.com",
  "fields": [
    {
      "fieldType": "textField",
      "index": 0,
      "name": "Title",
      "textField": {}
    }
  ],
  "groupID": "group::test@example.com",
  "name": "Test 1",
  "organizationID": "organization::test@example.com",
  "sharedUsers": [
    ...
  ],
  "type": "sheet",
  "updatedAt": "2019-09-26T19:09:22.813Z",
  "updatedByIP": "###.###.###.###"
}

Live Query Example:

    final Query query = QueryBuilder.select(
                SelectResult.expression(Expression.property("name").from("sheets")),
                SelectResult.expression(Expression.property("fields").from("sheets")),
                SelectResult.expression(Expression.property("createdAt").from("sheets")),
                SelectResult.expression(Expression.property("updatedAt").from("sheets")),
                SelectResult.expression(Expression.property("deletedAt").from("sheets"))
    )
        .from(DataSource.database(myDatabase).as("sheets"))
        .where(Meta.id.from("sheets").equalTo(Expression.string(documentID)));

Error Output

F/libc    ( 5166): Fatal signal 11 (SIGSEGV), code 1, fault addr 0x0 in tid 5198 (AsyncTask #1)
*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
Build fingerprint: 'samsung/zeroflteue/zerofltetmo:7.0/NRD90M/G920TUES6ERC2:user/release-keys'
Revision: '11'
ABI: 'arm64'
pid: 5166, tid: 5198, name: AsyncTask #1  >>> com.example.android.dev <<<
signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x0
    x0   0000000000000000  x1   00000073da748bd4  x2   00000073932493c0  x3   00000073dbc70c00
    x4   00000073da749068  x5   00000073bd70cb80  x6   00000073bd92251c  x7   0000000000000000
    x8   6c5544bdb9436207  x9   0000000000000000  x10  0000000000430000  x11  00000073db6b0f7c
    x12  00000073db6b0fd0  x13  00000073db6b1024  x14  00000073db6b1084  x15  0000000000000000
    x16  0000007376938958  x17  00000073767ae708  x18  0000000070fe0d5c  x19  00000073da74c4e8
    x20  00000073932493c0  x21  00000073dbc70c00  x22  00000073da748e7c  x23  00000073bd70cb80
    x24  0000000000000008  x25  6c5544bdb9436207  x26  00000073dbc70c98  x27  6c5544bdb9436207
    x28  0000000000000002  x29  00000073da748b30  x30  0000007376796698
    sp   00000073da748b00  pc   00000073767ae724  pstate 0000000020000000
backtrace:
    #00 pc 000000000031d724  /data/app/com.example.android.dev-1/lib/arm64/libLiteCoreJNI.so (_ZNK6fleece4impl5Value8asStringEv+28)
    #01 pc 0000000000305694  /data/app/com.example.android.dev-1/lib/arm64/libLiteCoreJNI.so (_ZNK6fleece4impl4Dict8iterator9keyStringEv+20)
    #02 pc 00000000002ff0e8  /data/app/com.example.android.dev-1/lib/arm64/libLiteCoreJNI.so (FLDictIterator_GetKeyString+28)
    #03 pc 000000000012a5a0  /data/app/com.example.android.dev-1/lib/arm64/libLiteCoreJNI.so (Java_com_couchbase_lite_internal_fleece_FLDictIterator_getKeyString+20)
    #04 pc 00000000007bcc64  /data/app/com.example.android.dev-1/oat/arm64/base.odex (offset 0x784000)
bmeike commented 5 years ago

One more thing, @bawelter . I'd like to see the code that you used to build your document

bmeike commented 5 years ago

I cannot reproduce this problem. I'm using 2.6.0 and I have code that creates, I believe, exactly that document, and then retrieves it with exactly that query, all with failing. Happy to share it with you, if that is any kind of help.

bawelter commented 5 years ago

If you can share your code with me I will find a way to reproduce the error and let you know so you don’t have to waste time.

Right now I’m using flutter so this is all generated through a platform channel, so I just have to rewrite it for android.

bawelter commented 5 years ago

Another thing to consider is that there was a query change listener on this as well, which could be the root of the problem.

The only other thing to consider is that the queries are ran on a background thread but I don’t think that is the issue since it works on all other queries.

I will try not putting the listener on and see what I get.

bawelter commented 5 years ago

The error is happening in the result.toMap() function.

final Query query = QueryBuilder.select(SelectResult.property("fields"))
    .from(DataSource.database(db))
    .where(Meta.id.equalTo(Expression.string("sheet-test")));

String snackbarText = "Failed";

try {
    ResultSet results = query.execute();
    for (Result result : results.allResults()) {
        result.toMap();
        //Log.d("CouchbaseResults", result.toMap().toString());
    }

    snackbarText = "Result Count: " + results.allResults().size();
} catch(CouchbaseLiteException e) {
    Log.e("CouchbaseTest", e.getLocalizedMessage());
    e.printStackTrace();
}
bawelter commented 5 years ago

So I tested all scenarios and this always happens with empty javascript objects like below.

{
    "myObject" : {}
}
bawelter commented 5 years ago

Here is a test case that can be added to couchbase-lite-java/src/shared/test/java/com/couchbase/lite/ResultTest.java

   @Test
    public void testResultToMap() throws Exception {
        String docID = String.format(Locale.ENGLISH, "doc%d", 3);
        MutableDocument mDoc = new MutableDocument(docID);
        mDoc.setDictionary("emptyDict", new MutableDictionary());
        save(mDoc);

        final Query query = QueryBuilder.select(
                SelectResult.property("emptyDict")
        )
            .from(DataSource.database(db))
            .where(Meta.id.equalTo(Expression.string(docID)));

        try {
            ResultSet results = query.execute();
            for (Result result : results.allResults()) {
                result.toMap();
            }
        } catch(Exception e) {
            fail();
        }
    }

Purposed quick fix is to use the getValue function in the toList() function in /couchbase-lite-java/src/shared/main/java/com/couchbase/lite/Result.java

   /**
     * Gets all values as an List. The value types of the values contained
     * in the returned List object are Array, Blob, Dictionary, Number types, String, and null.
     *
     * @return The List representing all values.
     */
    @NonNull
    @Override
    public List<Object> toList() {
        final List<Object> array = new ArrayList<>();
        for (int i = 0; i < count(); i++) {
            array.add(getValue(i));
        }
        return array;
    }

I think this is actually a bug in the Native code though not the java code. I will look into this a bit more.

bmeike commented 5 years ago

Nice work, @bawelter . On it.

bawelter commented 5 years ago

I see you already found this bug, just need to rewrite some loops as @snej just commented a few days ago. Thanks @bmeike !

https://github.com/couchbaselabs/fleece/issues/44

bmeike commented 5 years ago

Tracking affect on Android, here: https://issues.couchbase.com/browse/CBL-436

K-Leon commented 5 years ago

Thanks for your fix! Is a Backport / Patch Release for 2.6 planned? (e.g. 2.6.0.1)

bmeike commented 5 years ago

I can't comment at this point. If you can confirm that this change fixes the problem, it would help.

K-Leon commented 5 years ago

Is there already a branch or a fixed commit for 2.7 including this fix? I would then start testing it with my app. As i read in other issues it already settelt a bit?

bawelter commented 5 years ago

The issue was fixed with the changes you made from what I could tell.

bmeike commented 5 years ago

Excellent. Closing. @K-Leon : the fix is on master and you are right not to use master in production. From what I hear, the chances of a 2.6.x patch release are pretty good. Closing