Program: 156043
Project: 156043
Buggy source:
public final void refresh(){
  if (dataManager != null) {
    try {
      geofencesLock.writeLock().lock();
      groupGeofencesLock.writeLock().lock();
      deviceGeofencesLock.writeLock().lock();
      try {
        geofences.clear();
        for (        Geofence geofence : dataManager.getGeofences()) {
          geofences.put(geofence.getId(),geofence);
        }
        userGeofences.clear();
        for (        GeofencePermission geofencePermission : dataManager.getGeofencePermissions()) {
          getUserGeofencesIds(geofencePermission.getUserId()).add(geofencePermission.getGeofenceId());
        }
        groupGeofences.clear();
        for (        GroupGeofence groupGeofence : dataManager.getGroupGeofences()) {
          getGroupGeofences(groupGeofence.getGroupId()).add(groupGeofence.getGeofenceId());
        }
        deviceGeofences.clear();
        deviceGeofencesWithGroups.clear();
        for (        DeviceGeofence deviceGeofence : dataManager.getDeviceGeofences()) {
          getDeviceGeofences(deviceGeofences,deviceGeofence.getDeviceId()).add(deviceGeofence.getGeofenceId());
          getDeviceGeofences(deviceGeofencesWithGroups,deviceGeofence.getDeviceId()).add(deviceGeofence.getGeofenceId());
        }
        for (        Device device : dataManager.getAllDevicesCached()) {
          long groupId=device.getGroupId();
          while (groupId != 0) {
            getDeviceGeofences(deviceGeofencesWithGroups,device.getId()).addAll(getGroupGeofences(groupId));
            groupId=dataManager.getGroupById(groupId).getGroupId();
          }
          List<Long> deviceGeofenceIds=device.getGeofenceIds();
          if (deviceGeofenceIds == null) {
            deviceGeofenceIds=new ArrayList<Long>();
          }
 else {
            deviceGeofenceIds.clear();
          }
          Position lastPosition=Context.getConnectionManager().getLastPosition(device.getId());
          if (lastPosition != null && deviceGeofencesWithGroups.containsKey(device.getId())) {
            for (            Long geofenceId : deviceGeofencesWithGroups.get(device.getId())) {
              if (getGeofence(geofenceId).getGeometry().containsPoint(lastPosition.getLatitude(),lastPosition.getLongitude())) {
                deviceGeofenceIds.add(geofenceId);
              }
            }
          }
          device.setGeofenceIds(deviceGeofenceIds);
        }
      }
  finally {
        geofencesLock.writeLock().unlock();
        groupGeofencesLock.writeLock().unlock();
        deviceGeofencesLock.writeLock().unlock();
      }
    }
 catch (    SQLException error) {
      Log.warning(error);
    }
  }
}

Fixed version (ground truth):
public final void refresh(){
  if (dataManager != null) {
    try {
      geofencesLock.writeLock().lock();
      groupGeofencesLock.writeLock().lock();
      deviceGeofencesLock.writeLock().lock();
      try {
        geofences.clear();
        for (        Geofence geofence : dataManager.getGeofences()) {
          geofences.put(geofence.getId(),geofence);
        }
        userGeofences.clear();
        for (        GeofencePermission geofencePermission : dataManager.getGeofencePermissions()) {
          getUserGeofencesIds(geofencePermission.getUserId()).add(geofencePermission.getGeofenceId());
        }
        groupGeofences.clear();
        for (        GroupGeofence groupGeofence : dataManager.getGroupGeofences()) {
          getGroupGeofences(groupGeofence.getGroupId()).add(groupGeofence.getGeofenceId());
        }
        deviceGeofences.clear();
        deviceGeofencesWithGroups.clear();
        for (        DeviceGeofence deviceGeofence : dataManager.getDeviceGeofences()) {
          getDeviceGeofences(deviceGeofences,deviceGeofence.getDeviceId()).add(deviceGeofence.getGeofenceId());
          getDeviceGeofences(deviceGeofencesWithGroups,deviceGeofence.getDeviceId()).add(deviceGeofence.getGeofenceId());
        }
        for (        Device device : dataManager.getAllDevicesCached()) {
          Group group=dataManager.getGroupById(device.getGroupId());
          while (group != null) {
            getDeviceGeofences(deviceGeofencesWithGroups,device.getId()).addAll(getGroupGeofences(group.getGroupId()));
            group=dataManager.getGroupById(group.getGroupId());
          }
          List<Long> deviceGeofenceIds=device.getGeofenceIds();
          if (deviceGeofenceIds == null) {
            deviceGeofenceIds=new ArrayList<>();
          }
 else {
            deviceGeofenceIds.clear();
          }
          Position lastPosition=Context.getConnectionManager().getLastPosition(device.getId());
          if (lastPosition != null && deviceGeofencesWithGroups.containsKey(device.getId())) {
            for (            Long geofenceId : deviceGeofencesWithGroups.get(device.getId())) {
              if (getGeofence(geofenceId).getGeometry().containsPoint(lastPosition.getLatitude(),lastPosition.getLongitude())) {
                deviceGeofenceIds.add(geofenceId);
              }
            }
          }
          device.setGeofenceIds(deviceGeofenceIds);
        }
      }
  finally {
        geofencesLock.writeLock().unlock();
        groupGeofencesLock.writeLock().unlock();
        deviceGeofencesLock.writeLock().unlock();
      }
    }
 catch (    SQLException error) {
      Log.warning(error);
    }
  }
}