elsa-workflows / elsa-workflows/elsa-core
Intermittent `InvalidProgramException` during workflow state commit leaves instance stuck in Running (Executing)
- Dominant language
- C#
- Stars
- 7.9k
- Forks
- 1.5k
- Avg merge
- 15h 22m
- Merged PRs (30d)
- 114
Description
## Description
We are hitting an intermittent runtime failure when Elsa commits workflow state to the EF Core store.
Error:
`System.InvalidProgramException: Common Language Runtime detected an invalid program.`
This happens during scheduled execution of a workflow. Activities complete, but on commit/save Elsa throws, and the workflow instance remains stuck in `Running` (`Executing`).
Because we allow only one active instance per workflow, subsequent scheduled runs are blocked by that stuck instance.
---
## Steps to Reproduce
1. **Detailed Steps**
1. Configure a scheduled workflow (flowchart) that runs every 5 minutes.
2. Use Elsa EF Core persistence with PostgreSQL.
3. Ensure only one instance can run at a time (we check for existing `Running` instances at workflow start).
4. Let the workflow run repeatedly (in our case, the issue appears always in the same pipeline and can also happen when there are 0 items to process).
5. Intermittently, after activities complete, Elsa fails during state persistence/commit.
2. **Code Snippets**
- Relevant behavior in our app:
- Workflow executes normally.
- Failure happens in Elsa commit path (`DefaultCommitStateHandler` / `EFCoreWorkflowInstanceStore.OnSaveAsync`), not inside custom activity logic.
- Stack trace included in **Log Output** below.
3. **Reproduction Rate**
- Intermittent (not deterministic).
- Happens repeatedly in production-like traffic, but not on every run.
4. **Additional Configuration**
- Scheduled workflow.
- EF Core + PostgreSQL persistence.
- Single-running-instance guard in our workflow start logic.
- Elsa compression enabled in management persistence (Gzip).
---
## Expected Behavior
If state commit fails, the workflow should not remain indefinitely in `Running`. It should transition to a safe terminal/error state (or be recoverable automatically) so future scheduled runs are not blocked.
---
## Actual Behavior
The workflow activities finish, then Elsa throws during state save/serialization and leaves the instance in `Running` (`Executing`).
Subsequent scheduled runs are blocked because Elsa still sees one running instance.
---
## Environment
- **Elsa Package Version**: `3.5.0` (`Elsa`, `Elsa.EntityFrameworkCore`, `Elsa.EntityFrameworkCore.PostgreSql`, `Elsa.Scheduling`, etc.)
- **Operating System**: Linux container / server environment (can provide exact distro/version if needed)
- **Browser and Version**: N/A (server-side workflow execution issue)
- **Database**: PostgreSQL
- **Runtime**: .NET (`net10.0` currently).
Note: We do not believe this is tied to a specific .NET version; the issue is intermittent and workflow-specific in our environment. This already happened before upgrading to .Net 10.
---
## Log Output
```text
2026-02-28 15:10:18.716 +01:00 [ERR] [WF:ba1b437e5313ebf4] [ACT:] Error executing scheduled task
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
---> System.AggregateException: One or more errors occurred. (Common Language Runtime detected an invalid program.)
---> System.InvalidProgramException: Common Language Runtime detected an invalid program.
at IsExecutingGetter(Object)
at System.Text.Json.Serialization.Metadata.JsonPropertyInfo`1.GetMemberAndWriteJson(Object obj, WriteStack& state, Utf8JsonWriter writer)
...
at Elsa.EntityFrameworkCore.Modules.Management.EFCoreWorkflowInstanceStore.OnSaveAsync(...)
at Elsa.Workflows.Runtime.DefaultCommitStateHandler.CommitAsync(...)
at Elsa.Workflows.WorkflowRunner.RunAsync(...)
at Elsa.Scheduling.Tasks.ResumeWorkflowTask.ExecuteAsync(...)
```
We also sometimes see (possibly unrelated) around similar time windows:
`Activity Delay2 is not reachable from the flowchart graph. Unable to schedule it's outbound activities.`
———
## Troubleshooting Attempts
- Verified custom activities in the failing workflow path (no direct exception there).
- Confirmed activities complete before the failure point.
- Added operational mitigation: watchdog background service that auto-cancels stale Running instances (>15 min) to unblock scheduling.
- Mitigation works operationally, but does not solve root cause in Elsa commit/persistence path.
———
## Additional Context
- This has happened multiple times and consistently on the same workflow definition.
- Other workflows continue running normally.
- Main impact is scheduler blockage due to stuck Running instance.
———
## Related Issues
No confirmed related issue found yet.
If this is known/fixed in newer RLC/nightly builds, please point us to the related issue/PR/commit.
Thanks!
Contributor guide
Assessment
This issue has not been assessed yet.