Program: 142167
Project: 142167
Buggy source:
@Override protected Integer doTaskBackground(Void... params){
  if (!CommCareSessionService.sessionAliveLock.tryLock()) {
    return UNKNOWN_FAILURE;
  }
  try {
    publishProgress(PROGRESS_STARTED);
    CommCareApp app=CommCareApplication._().getCurrentApp();
    SharedPreferences prefs=app.getAppPreferences();
    String keyServer=prefs.getString("String_Node_Str",null);
    mTotalItems=-1;
    mCurrentProgress=-1;
    boolean useExternalKeys=!(keyServer == null || keyServer.equals("String_Node_Str"));
    boolean loginNeeded=true;
    boolean useRequestFlags=false;
    try {
      loginNeeded=!CommCareApplication._().getSession().isActive();
    }
 catch (    SessionUnavailableException sue) {
    }
    int responseError=UNKNOWN_FAILURE;
    prefs.edit().putLong("String_Node_Str",new Date().getTime()).commit();
    HttpRequestGenerator requestor=new HttpRequestGenerator(username,password);
    CommCareTransactionParserFactory factory=new CommCareTransactionParserFactory(c,requestor){
      boolean publishedAuth=false;
      @Override public void reportProgress(      int progress){
        if (!publishedAuth) {
          DataPullTask.this.publishProgress(PROGRESS_AUTHED,progress);
          publishedAuth=true;
        }
      }
    }
;
    Logger.log(AndroidLogger.TYPE_USER,"String_Node_Str");
    long bytesRead=-1;
    UserKeyRecord ukr=null;
    try {
      if (loginNeeded) {
        if (!useExternalKeys) {
          SecretKey newKey=CryptUtil.generateSemiRandomKey();
          if (newKey == null) {
            this.publishProgress(PROGRESS_DONE);
            return UNKNOWN_FAILURE;
          }
          String sandboxId=PropertyUtils.genUUID().replace("String_Node_Str","String_Node_Str");
          ukr=new UserKeyRecord(username,UserKeyRecord.generatePwdHash(password),CryptUtil.wrapKey(newKey.getEncoded(),password),new Date(),new Date(Long.MAX_VALUE),sandboxId);
        }
 else {
          ukr=ManageKeyRecordTask.getCurrentValidRecord(app,username,password,true);
          if (ukr == null) {
            Logger.log(AndroidLogger.TYPE_ERROR_ASSERTION,"String_Node_Str");
            this.publishProgress(PROGRESS_DONE);
            return UNKNOWN_FAILURE;
          }
        }
        byte[] wrappedKey=CryptUtil.wrapKey(ukr.getEncryptedKey(),password);
        factory.initUserParser(wrappedKey);
      }
 else {
        factory.initUserParser(CommCareApplication._().getSession().getLoggedInUser().getWrappedKey());
        purgeCases();
        useRequestFlags=true;
      }
      this.publishProgress(PROGRESS_CLEANED);
      RemoteDataPullResponse pullResponse=dataPullRequester.makeDataPullRequest(this,requestor,server,useRequestFlags);
      Logger.log(AndroidLogger.TYPE_USER,"String_Node_Str" + pullResponse.responseCode);
      if (pullResponse.responseCode == 401) {
        if (loginNeeded) {
          CommCareApplication._().releaseUserResourcesAndServices();
        }
        Logger.log(AndroidLogger.TYPE_USER,"String_Node_Str" + username);
        return AUTH_FAILED;
      }
 else       if (pullResponse.responseCode >= 200 && pullResponse.responseCode < 300) {
        if (loginNeeded) {
          CommCareApplication._().startUserSession(CryptUtil.unWrapKey(ukr.getEncryptedKey(),password),ukr);
          wasKeyLoggedIn=true;
        }
        this.publishProgress(PROGRESS_AUTHED,0);
        Logger.log(AndroidLogger.TYPE_USER,"String_Node_Str" + username);
        try {
          BitCache cache=pullResponse.writeResponseToCache(c);
          InputStream cacheIn=cache.retrieveCache();
          String syncToken=readInput(cacheIn,factory);
          updateUserSyncToken(syncToken);
          Editor e=prefs.edit();
          e.putLong("String_Node_Str",new Date().getTime());
          e.commit();
          if (loginNeeded) {
            CommCareApplication._().getAppStorage(UserKeyRecord.class).write(ukr);
          }
          Intent i=new Intent("String_Node_Str");
          this.c.sendBroadcast(i);
          Logger.log(AndroidLogger.TYPE_USER,"String_Node_Str" + username);
          this.publishProgress(PROGRESS_DONE);
          return DOWNLOAD_SUCCESS;
        }
 catch (        InvalidStructureException e) {
          e.printStackTrace();
          Logger.log(AndroidLogger.TYPE_USER,"String_Node_Str" + e.getMessage());
          return BAD_DATA;
        }
catch (        XmlPullParserException e) {
          e.printStackTrace();
          Logger.log(AndroidLogger.TYPE_USER,"String_Node_Str" + e.getMessage());
          return BAD_DATA;
        }
catch (        UnfullfilledRequirementsException e) {
          e.printStackTrace();
          Logger.log(AndroidLogger.TYPE_ERROR_ASSERTION,"String_Node_Str" + e.getMessage());
        }
catch (        IllegalStateException e) {
          e.printStackTrace();
          Logger.log(AndroidLogger.TYPE_ERROR_ASSERTION,"String_Node_Str" + e.getMessage());
        }
catch (        RecordTooLargeException e) {
          e.printStackTrace();
          Logger.log(AndroidLogger.TYPE_ERROR_ASSERTION,"String_Node_Str" + e.getMessage());
        }
      }
 else       if (pullResponse.responseCode == 412) {
        int returnCode=recover(requestor,factory);
        if (returnCode == PROGRESS_DONE) {
          this.publishProgress(PROGRESS_DONE);
          return DOWNLOAD_SUCCESS;
        }
 else         if (returnCode == PROGRESS_RECOVERY_FAIL_SAFE) {
          if (loginNeeded) {
            CommCareApplication._().releaseUserResourcesAndServices();
          }
          this.publishProgress(PROGRESS_DONE);
          return UNKNOWN_FAILURE;
        }
 else         if (returnCode == PROGRESS_RECOVERY_FAIL_BAD) {
          if (loginNeeded) {
            CommCareApplication._().releaseUserResourcesAndServices();
          }
          this.publishProgress(PROGRESS_DONE);
          return UNKNOWN_FAILURE;
        }
        if (loginNeeded) {
          CommCareApplication._().releaseUserResourcesAndServices();
        }
      }
 else       if (pullResponse.responseCode == 500) {
        if (loginNeeded) {
          CommCareApplication._().releaseUserResourcesAndServices();
        }
        Logger.log(AndroidLogger.TYPE_USER,"String_Node_Str" + username);
        return SERVER_ERROR;
      }
    }
 catch (    SocketTimeoutException e) {
      e.printStackTrace();
      Logger.log(AndroidLogger.TYPE_WARNING_NETWORK,"String_Node_Str");
      responseError=CONNECTION_TIMEOUT;
    }
catch (    ConnectTimeoutException e) {
      e.printStackTrace();
      Logger.log(AndroidLogger.TYPE_WARNING_NETWORK,"String_Node_Str");
      responseError=CONNECTION_TIMEOUT;
    }
catch (    ClientProtocolException e) {
      e.printStackTrace();
      Logger.log(AndroidLogger.TYPE_WARNING_NETWORK,"String_Node_Str" + e.getMessage());
    }
catch (    UnknownHostException e) {
      Logger.log(AndroidLogger.TYPE_WARNING_NETWORK,"String_Node_Str");
      responseError=UNREACHABLE_HOST;
    }
catch (    IOException e) {
      e.printStackTrace();
      Logger.log(AndroidLogger.TYPE_WARNING_NETWORK,"String_Node_Str" + e.getMessage());
    }
catch (    SessionUnavailableException sue) {
      sue.printStackTrace();
    }
    if (loginNeeded) {
      CommCareApplication._().releaseUserResourcesAndServices();
    }
    this.publishProgress(PROGRESS_DONE);
    return responseError;
  }
  finally {
    CommCareSessionService.sessionAliveLock.unlock();
  }
}

Fixed version (ground truth):
@Override protected Integer doTaskBackground(Void... params){
  if (!CommCareSessionService.sessionAliveLock.tryLock()) {
    return UNKNOWN_FAILURE;
  }
  try {
    publishProgress(PROGRESS_STARTED);
    CommCareApp app=CommCareApplication._().getCurrentApp();
    SharedPreferences prefs=app.getAppPreferences();
    String keyServer=prefs.getString("String_Node_Str",null);
    mTotalItems=-1;
    mCurrentProgress=-1;
    boolean useExternalKeys=!(keyServer == null || keyServer.equals("String_Node_Str"));
    boolean loginNeeded=true;
    boolean useRequestFlags=false;
    try {
      loginNeeded=!CommCareApplication._().getSession().isActive();
    }
 catch (    SessionUnavailableException sue) {
    }
    int responseError=UNKNOWN_FAILURE;
    prefs.edit().putLong("String_Node_Str",new Date().getTime()).commit();
    HttpRequestGenerator requestor=new HttpRequestGenerator(username,password);
    CommCareTransactionParserFactory factory=new CommCareTransactionParserFactory(c,requestor){
      boolean publishedAuth=false;
      @Override public void reportProgress(      int progress){
        if (!publishedAuth) {
          DataPullTask.this.publishProgress(PROGRESS_AUTHED,progress);
          publishedAuth=true;
        }
      }
    }
;
    Logger.log(AndroidLogger.TYPE_USER,"String_Node_Str");
    long bytesRead=-1;
    UserKeyRecord ukr=null;
    try {
      if (loginNeeded) {
        if (!useExternalKeys) {
          SecretKey newKey=CryptUtil.generateSemiRandomKey();
          if (newKey == null) {
            this.publishProgress(PROGRESS_DONE);
            return UNKNOWN_FAILURE;
          }
          String sandboxId=PropertyUtils.genUUID().replace("String_Node_Str","String_Node_Str");
          ukr=new UserKeyRecord(username,UserKeyRecord.generatePwdHash(password),CryptUtil.wrapKey(newKey.getEncoded(),password),new Date(),new Date(Long.MAX_VALUE),sandboxId);
        }
 else {
          ukr=ManageKeyRecordTask.getCurrentValidRecord(app,username,password,true);
          if (ukr == null) {
            Logger.log(AndroidLogger.TYPE_ERROR_ASSERTION,"String_Node_Str");
            this.publishProgress(PROGRESS_DONE);
            return UNKNOWN_FAILURE;
          }
        }
        byte[] wrappedKey=CryptUtil.wrapKey(ukr.getEncryptedKey(),password);
        factory.initUserParser(wrappedKey);
      }
 else {
        factory.initUserParser(CommCareApplication._().getSession().getLoggedInUser().getWrappedKey());
        purgeCases();
        useRequestFlags=true;
      }
      this.publishProgress(PROGRESS_CLEANED);
      RemoteDataPullResponse pullResponse=dataPullRequester.makeDataPullRequest(this,requestor,server,useRequestFlags);
      Logger.log(AndroidLogger.TYPE_USER,"String_Node_Str" + pullResponse.responseCode);
      if (pullResponse.responseCode == 401) {
        if (loginNeeded) {
          CommCareApplication._().releaseUserResourcesAndServices();
        }
        Logger.log(AndroidLogger.TYPE_USER,"String_Node_Str" + username);
        return AUTH_FAILED;
      }
 else       if (pullResponse.responseCode >= 200 && pullResponse.responseCode < 300) {
        if (loginNeeded) {
          CommCareApplication._().startUserSession(CryptUtil.unWrapKey(ukr.getEncryptedKey(),password),ukr);
          wasKeyLoggedIn=true;
        }
        this.publishProgress(PROGRESS_AUTHED,0);
        Logger.log(AndroidLogger.TYPE_USER,"String_Node_Str" + username);
        try {
          BitCache cache=pullResponse.writeResponseToCache(c);
          InputStream cacheIn=cache.retrieveCache();
          String syncToken=readInput(cacheIn,factory);
          updateUserSyncToken(syncToken);
          Editor e=prefs.edit();
          e.putLong("String_Node_Str",new Date().getTime());
          e.commit();
          if (loginNeeded) {
            CommCareApplication._().getAppStorage(UserKeyRecord.class).write(ukr);
          }
          Intent i=new Intent("String_Node_Str");
          this.c.sendBroadcast(i);
          Logger.log(AndroidLogger.TYPE_USER,"String_Node_Str" + username);
          this.publishProgress(PROGRESS_DONE);
          return DOWNLOAD_SUCCESS;
        }
 catch (        InvalidStructureException e) {
          e.printStackTrace();
          Logger.log(AndroidLogger.TYPE_USER,"String_Node_Str" + e.getMessage());
          return BAD_DATA;
        }
catch (        XmlPullParserException e) {
          e.printStackTrace();
          Logger.log(AndroidLogger.TYPE_USER,"String_Node_Str" + e.getMessage());
          return BAD_DATA;
        }
catch (        UnfullfilledRequirementsException e) {
          e.printStackTrace();
          Logger.log(AndroidLogger.TYPE_ERROR_ASSERTION,"String_Node_Str" + e.getMessage());
        }
catch (        IllegalStateException e) {
          e.printStackTrace();
          Logger.log(AndroidLogger.TYPE_ERROR_ASSERTION,"String_Node_Str" + e.getMessage());
        }
catch (        RecordTooLargeException e) {
          e.printStackTrace();
          Logger.log(AndroidLogger.TYPE_ERROR_ASSERTION,"String_Node_Str" + e.getMessage());
          return STORAGE_FULL;
        }
      }
 else       if (pullResponse.responseCode == 412) {
        int returnCode=recover(requestor,factory);
        if (returnCode == PROGRESS_DONE) {
          this.publishProgress(PROGRESS_DONE);
          return DOWNLOAD_SUCCESS;
        }
 else         if (returnCode == PROGRESS_RECOVERY_FAIL_SAFE) {
          if (loginNeeded) {
            CommCareApplication._().releaseUserResourcesAndServices();
          }
          this.publishProgress(PROGRESS_DONE);
          return UNKNOWN_FAILURE;
        }
 else         if (returnCode == PROGRESS_RECOVERY_FAIL_BAD) {
          if (loginNeeded) {
            CommCareApplication._().releaseUserResourcesAndServices();
          }
          this.publishProgress(PROGRESS_DONE);
          return UNKNOWN_FAILURE;
        }
        if (loginNeeded) {
          CommCareApplication._().releaseUserResourcesAndServices();
        }
      }
 else       if (pullResponse.responseCode == 500) {
        if (loginNeeded) {
          CommCareApplication._().releaseUserResourcesAndServices();
        }
        Logger.log(AndroidLogger.TYPE_USER,"String_Node_Str" + username);
        return SERVER_ERROR;
      }
    }
 catch (    SocketTimeoutException e) {
      e.printStackTrace();
      Logger.log(AndroidLogger.TYPE_WARNING_NETWORK,"String_Node_Str");
      responseError=CONNECTION_TIMEOUT;
    }
catch (    ConnectTimeoutException e) {
      e.printStackTrace();
      Logger.log(AndroidLogger.TYPE_WARNING_NETWORK,"String_Node_Str");
      responseError=CONNECTION_TIMEOUT;
    }
catch (    ClientProtocolException e) {
      e.printStackTrace();
      Logger.log(AndroidLogger.TYPE_WARNING_NETWORK,"String_Node_Str" + e.getMessage());
    }
catch (    UnknownHostException e) {
      Logger.log(AndroidLogger.TYPE_WARNING_NETWORK,"String_Node_Str");
      responseError=UNREACHABLE_HOST;
    }
catch (    IOException e) {
      e.printStackTrace();
      Logger.log(AndroidLogger.TYPE_WARNING_NETWORK,"String_Node_Str" + e.getMessage());
    }
catch (    SessionUnavailableException sue) {
      sue.printStackTrace();
    }
    if (loginNeeded) {
      CommCareApplication._().releaseUserResourcesAndServices();
    }
    this.publishProgress(PROGRESS_DONE);
    return responseError;
  }
  finally {
    CommCareSessionService.sessionAliveLock.unlock();
  }
}