Skip to content

.NET: [Bug]: Race Condition in Handoff Workflow as Agent? #4544

@stinalina

Description

@stinalina

Description

Hey there!
I wrote an HandoffWorkflow as an Agent with an interactive UserInput Loop. Yesterday it seemed to work and no changes happens. Since now when a handoff occurs the workflow seems to be broken. The agent respones are empty. No error is thrown to debug , there's only a note in the message history. ("An error occurred while executing the workflow.")
I can see there's is also the issue with the user role mentiond here #4290 , but it seems there's another problem too. I was wondering if the order of the messages is correct, the response of the new requested agent (asia) is sandwiched between handoff_to and functionResult "transfered". Is this a race condition and the cause of the issue?

Image

Code Sample

# the created handoff as agent workflow    
Workflow workflow = AgentWorkflowBuilder.CreateHandoffBuilderWith(africaExpert)
      .WithHandoffs(allAgents.Except([africaExpert]), africaExpert, "The question is related to the africa continent.")
      .WithHandoffs(allAgents.Except([americaExpert]), americaExpert, "The question is related to the america continent.")
      .WithHandoffs(allAgents.Except([asiaExpert]), asiaExpert, "The question is related to the asia continent.")
      .WithHandoffs(allAgents.Except([europeExpert]), europeExpert, "The question is related to the europe continent.")
      .WithHandoffs(allAgents.Except([oceaniaExpert]), oceaniaExpert, "The question is related to the oceania continent.")
      .Build()
.AsAgent(
  id: "workflow-agent",
  name: "Continet Expert Handoff Workflow Agent",
  description: "A multi-agent workflow for continent experts handoff",
  checkpointManager: CheckpointManager.CreateInMemory(),
  executionEnvironment: InProcessExecution.Default
);

# Then I use an extension method for the interaction loop
extension(AIAgent workflowAgent)
{
  public async Task StartWorkflowAgentConversationAsync(string initialQuestion)
  {
    try
    {
      AgentSession session = await workflowAgent.CreateSessionAsync();

      var messages = new List<ChatMessage> { };

      Console.WriteLine("\nYou: " + initialQuestion);
      await workflowAgent.ProcessWorkflowQuestionAsync(messages, initialQuestion, session); 

      while (true)
      {
        Console.Write("\nYou: ");
        string? userInput = Console.ReadLine();
        if (string.IsNullOrWhiteSpace(userInput))
          continue;

        if (userInput.Equals("exit", StringComparison.OrdinalIgnoreCase) ||
         userInput.Equals("quit", StringComparison.OrdinalIgnoreCase))
        {
          Console.WriteLine("Goodbye!");
          break;
        }

        await workflowAgent.ProcessWorkflowQuestionAsync(messages, userInput, session);
      }

      Console.WriteLine(JsonSerializer.Serialize(messages));
    } 
    catch (Exception ex)
    {
      Console.WriteLine($"An error occurred: {ex.Message}");
    }
  }

  private async Task ProcessWorkflowQuestionAsync(List<ChatMessage> messages, string userInput, AgentSession session)
  {
    try
    {
      messages.Add(new ChatMessage(ChatRole.User, userInput));

      AgentResponse response = await workflowAgent.RunAsync(messages, session);

      foreach (ChatMessage message in response.Messages)
      {
        if (!string.IsNullOrWhiteSpace(message.Text))
        {
          Console.WriteLine($"\n{message.AuthorName}: {message.Text}");
        }
      }
      messages.AddRange(response.Messages);
    }
    catch (Exception ex)
    {
      Console.WriteLine($"An error occurred: {ex.Message}\n");
    }
  }
}

Error Messages / Stack Traces

{
        "AuthorName": null,
        "CreatedAt": "2026-03-08T16:01:16.3723698+00:00",
        "Role": "assistant",
        "Contents": [
            {
                "$type": "error",
                "Message": "An error occurred while executing the workflow.",
                "ErrorCode": null,
                "Details": null,
                "Annotations": null,
                "AdditionalProperties": null
            }
        ],
        "MessageId": "291a29c6942c4bb5bee1a7cb4fed85a1",
        "AdditionalProperties": null
    }

Package Versions

Microsoft.Agents.AI: 1.0.0-preview.260212.1 Microsoft.Agents.AI.Workflows: 1.0.0-preview.260212.1

.NET Version

.Net 10

Additional Context

No response

Metadata

Metadata

Assignees

Labels

.NETbugSomething isn't workingworkflowsRelated to Workflows in agent-framework

Type

Projects

Status

In Progress

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions