Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] ParallelForEach Behavior Inconsistency #5389

Open
hmorovati opened this issue May 15, 2024 · 2 comments
Open

[BUG] ParallelForEach Behavior Inconsistency #5389

hmorovati opened this issue May 15, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@hmorovati
Copy link

hmorovati commented May 15, 2024

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, new List<Guid>());

await foreach (var item in items)
{
    // For each item, declare a new variable for the work to be scheduled.
    var currentValueVariable = new Variable<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)
    };

    var currentIndexVariable = new Variable<int>("CurrentIndex", currentIndex++) { StorageDriverType = typeof(WorkflowStorageDriver) };
    var variables = new List<Variable> { currentValueVariable, currentIndexVariable };

    // Schedule a body of work for each item.
    var tag = 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.

@hmorovati hmorovati added the bug Something isn't working label May 15, 2024
Copy link

glime-ai bot commented May 15, 2024

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.

This comment was generated by Glime.

@hmorovati
Copy link
Author

@sfmskywalker would you please take a look?
Is there a way to get the CurrentValue in the ParallelForEach without having conflict with other instances?

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant