.NET: Fix race condition issue in FanInEdge while processing messages. - #4662
Merged
Conversation
Peter Ibekwe (peibekwe)
temporarily deployed
to
integration
March 12, 2026 22:09 — with
GitHub Actions
Inactive
Peter Ibekwe (peibekwe)
temporarily deployed
to
integration
March 12, 2026 22:09 — with
GitHub Actions
Inactive
Peter Ibekwe (peibekwe)
temporarily deployed
to
integration
March 13, 2026 19:38 — with
GitHub Actions
Inactive
Peter Ibekwe (peibekwe)
had a problem deploying
to
integration
March 13, 2026 19:38 — with
GitHub Actions
Failure
Peter Ibekwe (peibekwe)
temporarily deployed
to
integration
March 13, 2026 19:57 — with
GitHub Actions
Inactive
Peter Ibekwe (peibekwe)
requested review from
Ben Thomas (alliscode),
Chris (crickman) and
Jacob Alber (lokitoth)
March 13, 2026 21:24
Peter Ibekwe (peibekwe)
marked this pull request as ready for review
March 13, 2026 21:24
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes a race condition in the .NET workflow FanInEdge implementation by serializing concurrent FanInEdgeState.ProcessMessage calls, preventing message loss and premature workflow termination when supersteps execute in parallel.
Changes:
- Added a lock in
FanInEdgeState.ProcessMessageto make compound updates to pending messages and unseen-source tracking atomic. - Added a unit test that exercises concurrent
FanInEdgeRunner.ChaseEdgeAsynccalls to validate correct batch release under contention.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| dotnet/src/Microsoft.Agents.AI.Workflows/Execution/FanInEdgeState.cs | Serializes ProcessMessage state mutations with a lock to eliminate concurrent List/HashSet races. |
| dotnet/tests/Microsoft.Agents.AI.Workflows.UnitTests/EdgeRunnerTests.cs | Adds a concurrency-focused test to ensure exactly one batch release containing all source messages per iteration. |
Peter Ibekwe (peibekwe)
temporarily deployed
to
integration
March 13, 2026 23:14 — with
GitHub Actions
Inactive
Peter Ibekwe (peibekwe)
had a problem deploying
to
integration
March 13, 2026 23:14 — with
GitHub Actions
Failure
Peter Ibekwe (peibekwe)
temporarily deployed
to
integration
March 13, 2026 23:28 — with
GitHub Actions
Inactive
Ben Thomas (alliscode)
approved these changes
Mar 16, 2026
Jacob Alber (lokitoth)
suggested changes
Mar 17, 2026
Peter Ibekwe (peibekwe)
temporarily deployed
to
integration
March 17, 2026 17:20 — with
GitHub Actions
Inactive
Peter Ibekwe (peibekwe)
temporarily deployed
to
integration
March 17, 2026 17:20 — with
GitHub Actions
Inactive
Jacob Alber (lokitoth)
approved these changes
Mar 17, 2026
This was referenced May 8, 2026
Closed
Closed
This was referenced May 18, 2026
This was referenced May 26, 2026
This was referenced Jun 5, 2026
This was referenced Jun 24, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation and Context
FanInEdgeState.ProcessMessage()is called concurrently from parallel executor tasks duringRunSuperstepAsync(via Task.WhenAll) but uses non-thread-safe List.Add and HashSet.Remove. This race condition causes messages to be lost non-deterministically and can lead to premature workflow termination.Description
Added a lock around the
ProcessMessageimplementation to serialize concurrent access. Using a lock approach here instead of concurrent collections because the operations within ProcessMessage (add message, remove from unseen, check count, swap messages, reset unseen) are interdependent and must execute atomically as a group. Making individual collections thread-safe would not prevent interleaving between these compound steps. The critical section is tiny and synchronous, so there shouldn't be a contention concern.Fixes #3060
Contribution Checklist
Is this a breaking change? If yes, add "[BREAKING]" prefix to the title of the PR.