workflow-core icon indicating copy to clipboard operation
workflow-core copied to clipboard

Non parallel ForEach on empty collection throws exception

Open laffebaco opened this issue 5 years ago • 5 comments

When a ForEach is executed non parallel on an empty collection the workflow will terminate with the following exception:

System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index') at System.SZArrayHelper.get_Item[T](Int32 index) at System.Linq.Enumerable.ElementAt[TSource](IEnumerable`1 source, Int32 index) at WorkflowCore.Primitives.Foreach.Run(IStepExecutionContext context) at WorkflowCore.Models.StepBody.RunAsync(IStepExecutionContext context) at WorkflowCore.Services.StepExecutor.ExecuteStep(IStepExecutionContext context, IStepBody body) at WorkflowCore.Services.WorkflowExecutor.ExecuteStep(WorkflowInstance workflow, WorkflowStep step, ExecutionPointer pointer, WorkflowExecutorResult wfResult, WorkflowDefinition def, CancellationToken cancellationToken) at WorkflowCore.Services.WorkflowExecutor.Execute(WorkflowInstance workflow, CancellationToken cancellationToken)

This is my workflow:

        builder
            .UseDefaultErrorBehavior(WorkflowErrorHandling.Terminate)
            .StartWith<GetInterviewsWithoutCollectedDataStepBody>()
                .Output(data => data.Interviews, step => step.Interviews)
            .ForEach(data => data.Interviews, _ => false)
                .Do(data => data                    
                    .StartWith<GetInterviewDataStepBody>()
                        .Input(step => step.InterviewId, (data, context) => ((Interview)context.Item).Id)
                        .Input(step => step.SurveyId, (data, context) => ((Interview)context.Item).SurveyId)
                        .Output(data => data.CollectedAt, step => step.CollectedAt)
                        .Output(data => data.InterviewData, step => step.InterviewData)
                    .Then<SaveInterviewDataStepBody>()
                        .Input(step => step.InterviewId, (data, context) => ((Interview)context.Item).Id)
                        .Input(step => step.CollectedAt, data => data.CollectedAt)
                        .Input(step => step.InterviewData, data => data.InterviewData));

data.Interviews can be an empty collection (empty array). If this is the case the .ForEach(data => data.Interviews, _ => false) will throw the System.ArgumentOutOfRangeException.

laffebaco avatar Feb 18 '21 12:02 laffebaco

laffebaco avatar Mar 18 '21 15:03 laffebaco

FIST

laffebaco avatar Apr 05 '22 11:04 laffebaco

BUMP

laffebaco avatar Apr 05 '22 11:04 laffebaco

:punch:

laffebaco avatar Apr 05 '22 11:04 laffebaco

@danielgerlag, should this issue be closed?

https://github.com/danielgerlag/workflow-core/pull/895

thefrozenfruit avatar Aug 10 '23 09:08 thefrozenfruit