Program: 142145
Project: 142145
Buggy source:
private void bindUserSessionService(final byte[] key,final UserKeyRecord record,final boolean restoreSession){
  mConnection=new ServiceConnection(){
    public void onServiceConnected(    ComponentName className,    IBinder service){
      User user=null;
synchronized (serviceLock) {
        mCurrentServiceBindTimeout=MAX_BIND_TIMEOUT;
        mBoundService=((CommCareSessionService.LocalBinder)service).getService();
        mBoundService.prepareStorage(key,record);
        if (record != null) {
          for (          User u : getRawStorage("String_Node_Str",User.class,mBoundService.getUserDbHandle())) {
            if (record.getUsername().equals(u.getUsername())) {
              user=u;
            }
          }
        }
        mIsBound=true;
        mIsBinding=false;
        if (user != null) {
          mBoundService.startSession(user);
          if (restoreSession) {
            CommCareApplication.this.sessionWrapper=DevSessionRestorer.restoreSessionFromPrefs(getCommCarePlatform());
          }
 else {
            CommCareApplication.this.sessionWrapper=new AndroidSessionWrapper(CommCareApplication.this.getCommCarePlatform());
          }
          if (shouldAutoUpdate()) {
            startAutoUpdate();
          }
          syncPending=getPendingSyncStatus();
          doReportMaintenance(false);
          if (!User.TYPE_DEMO.equals(user.getUserType())) {
            getCurrentApp().getAppPreferences().edit().putString(CommCarePreferences.LAST_LOGGED_IN_USER,record.getUsername()).commit();
            PurgeStaleArchivedFormsTask.launchPurgeTask();
          }
        }
        cacheUserId();
        TimedStatsTracker.registerStartSession();
      }
    }
    public void onServiceDisconnected(    ComponentName className){
      mBoundService=null;
    }
  }
;
  startService(new Intent(this,CommCareSessionService.class));
  bindService(new Intent(this,CommCareSessionService.class),mConnection,Context.BIND_AUTO_CREATE);
  mIsBinding=true;
}

Fixed version (ground truth):
private void bindUserSessionService(final byte[] key,final UserKeyRecord record,final boolean restoreSession){
  mConnection=new ServiceConnection(){
    public void onServiceConnected(    ComponentName className,    IBinder service){
      User user=null;
synchronized (serviceLock) {
        mCurrentServiceBindTimeout=MAX_BIND_TIMEOUT;
        mBoundService=((CommCareSessionService.LocalBinder)service).getService();
        mBoundService.prepareStorage(key,record);
        if (record != null) {
          for (          User u : getRawStorage("String_Node_Str",User.class,mBoundService.getUserDbHandle())) {
            if (record.getUsername().equals(u.getUsername())) {
              user=u;
            }
          }
        }
        mIsBound=true;
        mIsBinding=false;
        if (user != null) {
          mBoundService.startSession(user);
          if (restoreSession) {
            CommCareApplication.this.sessionWrapper=DevSessionRestorer.restoreSessionFromPrefs(getCommCarePlatform());
          }
 else {
            CommCareApplication.this.sessionWrapper=new AndroidSessionWrapper(CommCareApplication.this.getCommCarePlatform());
          }
          if (shouldAutoUpdate()) {
            startAutoUpdate();
          }
          syncPending=getPendingSyncStatus();
          doReportMaintenance(false);
          if (!User.TYPE_DEMO.equals(user.getUserType())) {
            getCurrentApp().getAppPreferences().edit().putString(CommCarePreferences.LAST_LOGGED_IN_USER,record.getUsername()).commit();
            PurgeStaleArchivedFormsTask.launchPurgeTask();
          }
        }
        refreshUserIdCache();
        TimedStatsTracker.registerStartSession();
      }
    }
    public void onServiceDisconnected(    ComponentName className){
      mBoundService=null;
    }
  }
;
  startService(new Intent(this,CommCareSessionService.class));
  bindService(new Intent(this,CommCareSessionService.class),mConnection,Context.BIND_AUTO_CREATE);
  mIsBinding=true;
}