You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Description:
In the Elsa Workflow , I believe there's a potential issue with the ParallelForEach implementation. After examining the code, it seems that the usage of CurrentValue variable within the loop may lead to race conditions and incorrect behavior in multi-threaded scenarios.
Code Segment:
context.SetProperty(ScheduledTagsProperty, tags);
context.SetProperty(CompletedTagsProperty,newList<Guid>());awaitforeach(var item in items){// For each item, declare a new variable for the work to be scheduled.varcurrentValueVariable=newVariable<T>("CurrentValue", item){// TODO: This should be configurable, because this won't work for e.g. file streams and other non-serializable types.StorageDriverType=typeof(WorkflowStorageDriver)};varcurrentIndexVariable=newVariable<int>("CurrentIndex",currentIndex++){StorageDriverType=typeof(WorkflowStorageDriver)};varvariables=newList<Variable>{ currentValueVariable, currentIndexVariable };// Schedule a body of work for each item.vartag= Guid.NewGuid();
tags.Add(tag);await context.ScheduleActivityAsync(Body, OnChildCompleted, tag, variables);}
Issue:
The concern arises from the fact that the CurrentValue variable is shared across iterations of the loop. Since each iteration schedules work asynchronously, there's a possibility of race conditions where one thread might modify the CurrentValue before another thread can consume its value.
Expected Behavior:
In a parallel loop like this, each iteration should have its own isolated context, including its own instance of the CurrentValue variable. This ensures that modifications made by one iteration do not affect the others.
The text was updated successfully, but these errors were encountered:
To address the bug report regarding the ParallelForEach behavior inconsistency, it's important to understand that in Elsa Workflow, the ParallelForEach activity is designed to execute each iteration potentially in parallel, depending on the capabilities of the workflow host environment.
From the code segment provided, it appears that the CurrentValue variable is being instantiated within the loop for each item, which should ensure that each iteration has its own isolated context. However, if there are race conditions observed, it could be due to the shared state outside the loop or improper handling of asynchronous operations.
The expected behavior, as mentioned, is correct. Each iteration should indeed have its own isolated context, including its own instance of the CurrentValue variable. If the CurrentValue variable is being shared across iterations, it would be a bug that needs to be addressed.
To resolve this issue, the Elsa Workflow team would need to ensure that each iteration's context is properly isolated and that the CurrentValue variable is not shared between iterations. This might involve reviewing the implementation of the ParallelForEach activity and making necessary changes to the way variables are scoped and managed within the activity.
If you are experiencing this issue with the latest version of Elsa Workflow, I would recommend reporting this issue on the Elsa Workflow GitHub repository, providing the details of the bug, including the code segment, the observed issue, and the expected behavior. The development team can then investigate and provide a fix or guidance on how to avoid such race conditions.
Please let me know if you need further assistance or if there are any specific details you would like to discuss regarding this issue.
@sfmskywalker would you please take a look?
Is there a way to get the CurrentValue in the ParallelForEach without having conflict with other instances?
Description:
In the Elsa Workflow , I believe there's a potential issue with the
ParallelForEach
implementation. After examining the code, it seems that the usage ofCurrentValue
variable within the loop may lead to race conditions and incorrect behavior in multi-threaded scenarios.Code Segment:
Issue:
The concern arises from the fact that the
CurrentValue
variable is shared across iterations of the loop. Since each iteration schedules work asynchronously, there's a possibility of race conditions where one thread might modify theCurrentValue
before another thread can consume its value.Expected Behavior:
In a parallel loop like this, each iteration should have its own isolated context, including its own instance of the
CurrentValue
variable. This ensures that modifications made by one iteration do not affect the others.The text was updated successfully, but these errors were encountered: