Forest DB throws exception from native call



Android API 22
compile 'com.couchbase.lite:couchbase-lite-android:1.2.0’
compile ‘com.couchbase.lite:couchbase-lite-android-forestdb:1.2.0’

While performing one of the query in my app, ForestDB throws exception. The result is non-deterministic - the exception was thrown sometime but not always for the same query. See the following stack trace. Can you please provide some information/help on what would potentially cause this exception and how to fix the issue.

Caused by: com.couchbase.lite.CouchbaseLiteException: ForestException{domain=2, code=-39}, Status: -39 (HTTP 500 Internal error)
    at com.couchbase.lite.View.updateIndexes(
    at com.couchbase.lite.View.updateIndex(
    at com.couchbase.lite.Database.queryViewNamed(
    at android.os.AsyncTask$ 
    at android.os.AsyncTask$SerialExecutor$ 
    at java.util.concurrent.ThreadPoolExecutor.runWorker( 
    at java.util.concurrent.ThreadPoolExecutor$ 

Caused by: ForestException{domain=2, code=-39}
    at com.couchbase.cbforest.Indexer.iterateDocuments(Native Method)
    at com.couchbase.cbforest.Indexer.iterateDocuments(
    at com.couchbase.lite.View.updateIndexes( 
    at com.couchbase.lite.View.updateIndex( 
    at com.couchbase.lite.Database.queryViewNamed( 


Could you provide a sample of code that is causing the exception (not just the query, but that could be helpful too)?


Hi @cheng.ustc,

Could you please try 1.2.1 instead of 1.2.0. We fixed some forestdb related issues with 1.2.1 release.



Hi @hideki,

I have tried 1.2.1 and the issue still occurs.

The code which cause the exception is the following:

    QueryRunner<List<X>> query = mView.<List<X>>createQuery()
            .setKeys(Lists.newArrayList((Object) id))
    try {
        return QueryEnumerator.ResultMapper<List<X>>() {

            public List<X> map(QueryEnumerator<List<X>> rows) {
                List<X> list = new LinkedList<>();

                for (QueryRow row : rows) {
                    String id = row.getDocumentId();
                    Map<String, Object> props = row.getDocument().getUserProperties();
                    X x = new X(id, props);
                return list;
    } catch (DBException e) {
        Log.E("error loading document id=%s", id, e);
        throw e;


Hi @cheng.ustc,

Thank you for your response.

I filed the ticket for this issue.

I need two more information from you.

  • Beside the query operation, what operations are running simultaneously? The app inserting docs? or pull replication is running?
  • Can you describe QueryRunner implementation? does this create another thread?



  • Pull replication isn’t running but there might be an updating on the doc running simultaneously. More specifically, I have two doc types, let’s say A and B, and from OO perspective A contains a non-id property p and a list of B ( as the foreign key on B). The query thrown exception is fetching the list of B but there is a possibility at the time of query, p is updated asynchronously.

  • QueryRunner is nothing but a wrapper of Query and there is no thread (running on UI thread) involved. The returns our wrapper of QueryEnumerator.


@cheng.ustc thank you very much for your additional information!! It helps a lot.