Program: 155802
Project: 155802
Buggy source:
/** 
 * Find all objects that fulfill the query conditions and sorted by specific field name.
 * @return immediately an empty {@link RealmResults}. Users need to register a listener  {@link io.realm.RealmResults#addChangeListener(RealmChangeListener)} to be notifiedwhen the query completes.
 * @throws java.lang.RuntimeException Any other error
 * @see io.realm.RealmResults
 */
public RealmResults<E> findAllAsync(){
  checkQueryIsNotReused();
  final WeakReference<Handler> weakHandler=getWeakReferenceHandler();
  final long handoverQueryPointer=query.handoverQuery(realm.sharedGroupManager.getNativePointer());
  argumentsHolder=new ArgumentsHolder(ArgumentsHolder.TYPE_FIND_ALL);
  final RealmConfiguration realmConfiguration=realm.getConfiguration();
  RealmResults<E> realmResults=new RealmResults<E>(realm,query,clazz);
  final WeakReference<RealmResults<?>> weakRealmResults=new WeakReference<RealmResults<?>>(realmResults,realm.getReferenceQueue());
  realm.addAsyncRealmResults(weakRealmResults,this);
  final Future<Long> pendingQuery=Realm.asyncQueryExecutor.submit(new Callable<Long>(){
    @Override public Long call() throws Exception {
      if (!Thread.currentThread().isInterrupted()) {
        SharedGroup sharedGroup=null;
        try {
          sharedGroup=new SharedGroup(realmConfiguration.getPath(),SharedGroup.IMPLICIT_TRANSACTION,realmConfiguration.getDurability(),realmConfiguration.getEncryptionKey());
          long handoverTableViewPointer=query.findAllWithHandover(sharedGroup.getNativePointer(),sharedGroup.getNativeReplicationPointer(),handoverQueryPointer);
          QueryUpdateTask.Result result=QueryUpdateTask.Result.newRealmResultsResponse();
          result.updatedTableViews.put(weakRealmResults,handoverTableViewPointer);
          result.versionID=sharedGroup.getVersion();
          Handler handler=weakHandler.get();
          if (handler != null && handler.getLooper().getThread().isAlive()) {
            handler.obtainMessage(Realm.REALM_COMPLETED_ASYNC_QUERY,result).sendToTarget();
          }
          return handoverTableViewPointer;
        }
 catch (        Exception e) {
          RealmLog.e(e.getMessage());
        }
 finally {
          if (null != sharedGroup) {
            sharedGroup.close();
          }
        }
      }
 else {
        TableQuery.nativeCloseQueryHandover(handoverQueryPointer);
      }
      return INVALID_NATIVE_POINTER;
    }
  }
);
  realmResults.setPendingQuery(pendingQuery);
  return realmResults;
}

Fixed version (ground truth):
/** 
 * Find all objects that fulfill the query conditions and sorted by specific field name. (Need a Realm opened from a looper thread to work).
 * @return immediately an empty {@link RealmResults}. Users need to register a listener  {@link io.realm.RealmResults#addChangeListener(RealmChangeListener)} to be notifiedwhen the query completes.
 * @throws java.lang.RuntimeException Any other error
 * @see io.realm.RealmResults
 */
public RealmResults<E> findAllAsync(){
  checkQueryIsNotReused();
  final WeakReference<Handler> weakHandler=getWeakReferenceHandler();
  final long handoverQueryPointer=query.handoverQuery(realm.sharedGroupManager.getNativePointer());
  argumentsHolder=new ArgumentsHolder(ArgumentsHolder.TYPE_FIND_ALL);
  final RealmConfiguration realmConfiguration=realm.getConfiguration();
  RealmResults<E> realmResults=new RealmResults<E>(realm,query,clazz);
  final WeakReference<RealmResults<?>> weakRealmResults=new WeakReference<RealmResults<?>>(realmResults,realm.getReferenceQueue());
  realm.addAsyncRealmResults(weakRealmResults,this);
  final Future<Long> pendingQuery=Realm.asyncQueryExecutor.submit(new Callable<Long>(){
    @Override public Long call() throws Exception {
      if (!Thread.currentThread().isInterrupted()) {
        SharedGroup sharedGroup=null;
        try {
          sharedGroup=new SharedGroup(realmConfiguration.getPath(),SharedGroup.IMPLICIT_TRANSACTION,realmConfiguration.getDurability(),realmConfiguration.getEncryptionKey());
          long handoverTableViewPointer=query.findAllWithHandover(sharedGroup.getNativePointer(),sharedGroup.getNativeReplicationPointer(),handoverQueryPointer);
          QueryUpdateTask.Result result=QueryUpdateTask.Result.newRealmResultsResponse();
          result.updatedTableViews.put(weakRealmResults,handoverTableViewPointer);
          result.versionID=sharedGroup.getVersion();
          Handler handler=weakHandler.get();
          if (handler != null && handler.getLooper().getThread().isAlive()) {
            handler.obtainMessage(Realm.REALM_COMPLETED_ASYNC_QUERY,result).sendToTarget();
          }
          return handoverTableViewPointer;
        }
 catch (        Exception e) {
          RealmLog.e(e.getMessage());
        }
 finally {
          if (null != sharedGroup) {
            sharedGroup.close();
          }
        }
      }
 else {
        TableQuery.nativeCloseQueryHandover(handoverQueryPointer);
      }
      return INVALID_NATIVE_POINTER;
    }
  }
);
  realmResults.setPendingQuery(pendingQuery);
  return realmResults;
}