elsa-workflows / elsa-workflows/elsa-core
[BUG] Workflow with FaultStrategy continues execution if exception is thrown by custom behavior attached to activity
- Dominant language
- C#
- Stars
- 7.9k
- Forks
- 1.5k
- Avg merge
- 15h 22m
- Merged PRs (30d)
- 114
Description
## Description
## Steps to Reproduce
1. **Detailed Steps**:
- Use Elsa Workflows version 3.2.0-rc3 with the default workflow runtime (I haven't checked to see if this issue also exists with the Proto.Actor workflow runtime).
- Have a custom workflow that extends `WorkflowBase`.
- The workflow should be configured to use `FaultStrategy` as its incident strategy, by calling `builder.WorkflowOptions.IncidentStrategyType = typeof(FaultStrategy);` when overriding method `WorkflowBase.Build(IWorkflowBuilder builder)`.
- The workflow should execute multiple activities. I was able to reproduce this issue with both `Sequence` and `Flowchart` as the workflow's `Root`.
- One of the activities in the workflow should have a custom behavior attached to it.
- How the issue occurs: When the custom behavior throws an exception, workflow execution continues with the next activity in the workflow. It doesn't stop at the faulted activity, like using `FaultStrategy` usually does.
2. **Attachments**:
- **Sample Project**: I created a console app that reproduces the issue using **Elsa version 3.2.0-rc3**:
[ElsaWorkflowsBehaviorBugRepro.zip](https://github.com/user-attachments/files/16332999/ElsaWorkflowsBehaviorBugRepro.zip). The example contains three workflows:
- One that uses `FaultStrategy` as the incident strategy, and throws an unhandled exception from an activity, to illustrate the correct behavior. Here, the workflow stops executing after the faulted activity.
- One that uses `FaultStrategy` as the incident strategy, and throws an unhandled exception from a behavior attached to an activity, to illustrate the bug. Here, the workflow continues to the next activity in the sequence after the behavior throws an unhandled exception.
- One that uses a custom incident strategy that contains a workaround. This workflow throws an unhandled exception from a behavior attached to an activity. If you call `context.WorkflowExecutionContext.Scheduler.Clear()` from the custom incident strategy, the workflow doesn't continue to the next activity anymore like it did in the second example.
3. **Reproduction Rate**: The bug appears every time when an exception is thrown from a behavior in a workflow that uses `FaultStrategy` for incidents.
## Expected Behavior
I expect a workflow that uses incident strategy `FaultStrategy` to not move to executing the next activity if a behavior of the previous activity threw an unhandled exception.
## Actual Behavior
Even if the workflow uses incident strategy `FaultStrategy`, if a behavior attached to an activity throws an exception, the workflow continues with executing the next activities instead of stopping at the faulted activity.
## Environment
- **Elsa Package Version**: 3.2.0-rc3
- **Operating System**: Windows 11
- **Browser and Version**: not applicable
## Troubleshooting Attempts
I was able to find a workaround that is illustrated in the console app I attached to this issue:
- Create a custom incident strategy, and when overriding method `IIncidentStrategy.HandleIncident(ActivityExecutionContext context)`, call `context.WorkflowExecutionContext.Scheduler.Clear()`.
- Set this custom incident strategy as the incident strategy for the workflow by calling `builder.WorkflowOptions.IncidentStrategyType = typeof(CustomStrategy)` when overriding `WorkflowBase.Build(IWorkflowBuilder builder)`.
- With this custom incident strategy, execution won't continue to the next activity even if the exception was thrown from a behavior attached to an activity.
Contributor guide
Assessment
This issue has not been assessed yet.