Buggy code:
private static final int BATCH_SIZE = 100 ; private final Recorder recorder ; private final ReadPersistenceService persistenceRead ; private final EventService eventService ; private final ActivityInstanceService activityService ; private final SProcessInstanceBuilderFactory processInstanceKeyProvider ; private final EventInstanceService bpmEventInstanceService ; private final DataInstanceService dataInstanceService ; private final ArchiveService archiveService ; private final TechnicalLoggerService logger ; private final TransitionService transitionService ; private final ProcessDefinitionService processDefinitionService ; private final ConnectorInstanceService connectorInstanceService ; private final ClassLoaderService classLoaderService ; private final DocumentService documentService ; private final SCommentService commentService ; private final TokenService tokenService ; public ProcessInstanceServiceImpl ( final Recorder recorder , final ReadPersistenceService persistenceRead , final EventService eventService , final ActivityInstanceService activityService , final TechnicalLoggerService logger , final EventInstanceService bpmEventInstanceService , final DataInstanceService dataInstanceService , final ArchiveService archiveService , final TransitionService transitionService , final ProcessDefinitionService processDefinitionService , final ConnectorInstanceService connectorInstanceService , final ClassLoaderService classLoaderService , final DocumentService documentService , final SCommentService commentService , final TokenService tokenService ) { } @ Override public void createProcessInstance ( final SProcessInstance processInstance ) throws SProcessInstanceCreationException { } @ Override public SProcessInstance getProcessInstance ( final long processInstanceId ) throws SProcessInstanceNotFoundException , SProcessInstanceReadException { } @ Override public void deleteProcessInstance ( final long processInstanceId ) throws SProcessInstanceModificationException , SProcessInstanceNotFoundException , SProcessInstanceReadException { } @ Override public long deleteParentProcessInstanceAndElements ( final List < SProcessInstance > sProcessInstances ) throws SFlowNodeReadException , SProcessInstanceHierarchicalDeletionException , SProcessInstanceModificationException { } @ Override public void deleteParentProcessInstanceAndElements ( final long processInstanceId ) throws SFlowNodeReadException , SProcessInstanceHierarchicalDeletionException , SProcessInstanceModificationException , SProcessInstanceNotFoundException , SProcessInstanceReadException { } @ Override public void deleteParentProcessInstanceAndElements ( final SProcessInstance sProcessInstance ) throws SFlowNodeReadException , SProcessInstanceHierarchicalDeletionException , SProcessInstanceModificationException { } protected void logProcessInstanceNotFound ( final SProcessInstanceModificationException e ) { } protected void logArchivedProcessInstanceNotFound ( final SBonitaException e ) { } @ Override public long deleteParentArchivedProcessInstancesAndElements ( final List < SAProcessInstance > saProcessInstances ) throws SFlowNodeReadException , SProcessInstanceHierarchicalDeletionException , SProcessInstanceModificationException { } @ Override public void deleteParentArchivedProcessInstanceAndElements ( final SAProcessInstance saProcessInstance ) throws SFlowNodeReadException , SProcessInstanceHierarchicalDeletionException , SProcessInstanceModificationException { } @ Override public void deleteArchivedProcessInstance ( final SAProcessInstance archivedProcessInstance ) throws SProcessInstanceModificationException { } @ Override public void deleteArchivedProcessInstanceElements ( final long processInstanceId , final long processDefinitionId ) throws SProcessInstanceModificationException { } private void deleteArchivedChidrenProcessInstanceElements ( final long processInstanceId , final long processDefinitionId ) throws SBonitaException { } protected void deleteArchivedFlowNodeInstances ( final long processInstanceId ) throws SConnectorInstanceDeletionException , SFlowNodeDeletionException , SFlowNodeReadException , SDataInstanceException , SBonitaReadException { } private void deleteArchivedFlowNodeInstanceElements ( final SAFlowNodeInstance activityInstance ) throws SConnectorInstanceDeletionException , SFlowNodeReadException , SDataInstanceException , SBonitaReadException { } @ Override public List < Long > getSourceProcesInstanceIdsOfArchProcessInstancesFromDefinition ( final long processDefinitionId , final int fromIndex , final int maxResults , final OrderByType sortingOrder ) throws SProcessInstanceReadException { } @ Override public void deleteProcessInstance ( final SProcessInstance sProcessInstance ) throws SProcessInstanceModificationException { } protected void checkIfCallerIsNotActive ( final long callerId ) throws SFlowNodeReadException , SProcessInstanceHierarchicalDeletionException { } protected void deleteProcessInstanceElements ( final SProcessInstance processInstance ) throws SBonitaException { } private void deleteConnectorInstancesIfNecessary ( final SProcessInstance processInstance , final SProcessDefinition processDefinition ) throws SConnectorInstanceDeletionException , SConnectorInstanceReadException { } protected void deleteConnectorInstancesIfNecessary ( final SFlowNodeInstance flowNodeInstance , final SProcessDefinition processDefinition ) throws SConnectorInstanceDeletionException , SConnectorInstanceReadException { } private boolean hasConnectors ( final SFlowNodeInstance flowNodeInstance , final SProcessDefinition processDefinition ) { } private void deleteDataInstancesIfNecessary ( final SProcessInstance processInstance , final SProcessDefinition processDefinition ) throws SDataInstanceException { } private void deleteFlowNodeInstances ( final long processInstanceId , final SProcessDefinition processDefinition ) throws SFlowNodeReadException , SProcessInstanceModificationException { List < SFlowNodeInstance > activityInstances ; do { activityInstances = activityService . getFlowNodeInstances ( processInstanceId , 0 , ProcessInstanceServiceImpl . BATCH_SIZE ) ; for ( final SFlowNodeInstance activityInstance : activityInstances ) { deleteFlowNodeInstance ( activityInstance , processDefinition ) ; } <START_BUG> } while ( ! ( activityInstances . isEmpty ( ) ) ) ; <END_BUG> } @ Override public void deleteArchivedProcessInstancesOfProcessInstance ( final long processInstanceId ) throws SBonitaException { } @ Override public void deleteFlowNodeInstance ( final SFlowNodeInstance flowNodeInstance , final SProcessDefinition processDefinition ) throws SProcessInstanceModificationException { } void deleteFlowNodeInstanceElements ( final SFlowNodeInstance flowNodeInstance , final SProcessDefinition processDefinition ) throws SBonitaException { } void deleteSubProcess ( final SFlowNodeInstance flowNodeInstance , final SProcessDefinition processDefinition ) throws SBonitaException { } private void setExceptionContext ( final SProcessDefinition processDefinition , final SFlowNodeInstance flowNodeInstance , final SBonitaException e ) { } private void setExceptionContext ( final SFlowNodeInstance flowNodeInstance , final SBonitaException e ) { } void deleteDataInstancesIfNecessary ( final SFlowNodeInstance flowNodeInstance , final SProcessDefinition processDefinition ) throws SDataInstanceException { } @ Override public void setState ( final SProcessInstance processInstance , final ProcessInstanceState state ) throws SProcessInstanceModificationException { } @ Override public void setMigrationPlanId ( final SProcessInstance processInstance , final long migrationPlanId ) throws SProcessInstanceModificationException { } private void setProcessState ( final SProcessInstance processInstance , final ProcessInstanceState state ) throws SProcessInstanceModificationException { } private void updateProcessInstance ( final SProcessInstance processInstance , final EntityUpdateDescriptor descriptor , final String eventType ) throws SProcessInstanceModificationException { } private void archiveProcessInstance ( final SProcessInstance processInstance ) throws SProcessInstanceModificationException { } @ Override public void setStateCategory ( final SProcessInstance processInstance , final SStateCategory stateCatetory ) throws SProcessInstanceModificationException { } @ Override public List < Long > getChildInstanceIdsOfProcessInstance ( final long processInstanceId , final int fromIndex , final int maxResults , final String sortingField , final OrderByType sortingOrder ) throws SProcessInstanceReadException { } @ Override public SProcessInstance getChildOfActivity ( final long activityInstId ) throws SProcessInstanceNotFoundException , SBonitaReadException { } @ Override public long getNumberOfChildInstancesOfProcessInstance ( final long processInstanceId ) throws SProcessInstanceReadException { } @ Override public long getNumberOfProcessInstances ( final QueryOptions queryOptions ) throws SBonitaReadException { } @ Override public List < SProcessInstance > searchProcessInstances ( final QueryOptions queryOptions ) throws SBonitaReadException { } @ Override public long getNumberOfOpenProcessInstancesSupervisedBy ( final long userId , final QueryOptions queryOptions ) throws SBonitaReadException { } @ Override public List < SProcessInstance > searchOpenProcessInstancesSupervisedBy ( final long userId , final QueryOptions queryOptions ) throws SBonitaReadException { } @ Override public long getNumberOfOpenProcessInstancesInvolvingUser ( final long userId , final QueryOptions queryOptions ) throws SBonitaReadException { } @ Override public List < SProcessInstance > searchOpenProcessInstancesInvolvingUser ( final long userId , final QueryOptions queryOptions ) throws SBonitaReadException { }

Fix:
} while ( ( activityInstances . size ( ) ) == ( ProcessInstanceServiceImpl . BATCH_SIZE ) ) ;