Skip to content

Python: Emit ExecutorFailedEvent before WorkflowFailedEvent when executor throws exception#2537

Merged
moonbox3 merged 3 commits intomicrosoft:mainfrom
moonbox3:2508-fix
Dec 2, 2025
Merged

Python: Emit ExecutorFailedEvent before WorkflowFailedEvent when executor throws exception#2537
moonbox3 merged 3 commits intomicrosoft:mainfrom
moonbox3:2508-fix

Conversation

@moonbox3
Copy link
Contributor

@moonbox3 moonbox3 commented Dec 1, 2025

Motivation and Context

When an executor throws an exception, the ExecutorFailedEvent is queued via context.add_event() but never yielded to the stream. This happens because when the start executor fails, it runs before run_until_convergence(), so the exception bypasses the runner's event draining logic entirely.

The fix is to drain any pending events from the context before yielding WorkflowFailedEvent in the exception handler of _run_workflow_with_tracing().

Description

Contribution Checklist

  • The code builds clean without any errors or warnings
  • The PR follows the Contribution Guidelines
  • All unit tests pass, and I have added new tests where possible
  • Is this a breaking change? If yes, add "[BREAKING]" prefix to the title of the PR.

@moonbox3 moonbox3 self-assigned this Dec 1, 2025
@moonbox3 moonbox3 added python workflows Related to Workflows in agent-framework labels Dec 1, 2025
@moonbox3 moonbox3 marked this pull request as ready for review December 1, 2025 07:27
Copilot AI review requested due to automatic review settings December 1, 2025 07:27
@markwallace-microsoft
Copy link
Member

markwallace-microsoft commented Dec 1, 2025

Python Test Coverage

Python Test Coverage Report •
FileStmtsMissCoverMissing
packages/core/agent_framework/_workflows
   _workflow.py2501892%94, 267–269, 271–272, 290, 314, 316, 413, 662, 696, 701, 704, 723–725, 790
TOTAL15863241684% 

Python Unit Test Overview

Tests Skipped Failures Errors Time
2266 130 💤 0 ❌ 0 🔥 55.356s ⏱️

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes an issue where ExecutorFailedEvent was not being emitted before WorkflowFailedEvent when an executor throws an exception. The fix ensures that pending events queued via context.add_event() are drained and yielded before WorkflowFailedEvent in the exception handler.

  • Added event draining logic in the exception handler of _run_workflow_with_tracing() to ensure ExecutorFailedEvent is emitted before WorkflowFailedEvent
  • Added comprehensive test coverage for both start executor failures and non-start executor failures in a chain
  • Added test assertions to verify the correct event ordering

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
python/packages/core/agent_framework/_workflows/_workflow.py Added logic to drain pending events from context before yielding WorkflowFailedEvent in the exception handler
python/packages/core/tests/workflow/test_workflow_states.py Added test coverage for event ordering, including a new PassthroughExecutor class and test_executor_failed_event_from_second_executor_in_chain() test case

You can also share your feedback on Copilot code review for a chance to win a $100 gift card. Take the survey.

@moonbox3 moonbox3 enabled auto-merge December 2, 2025 01:17
@moonbox3 moonbox3 added this pull request to the merge queue Dec 2, 2025
Merged via the queue into microsoft:main with commit 0d5d10d Dec 2, 2025
23 checks passed
arisng pushed a commit to arisng/agent-framework that referenced this pull request Feb 2, 2026
…utor throws exception (microsoft#2537)

* Emit ExecutorFailedEvent before WorkflowFailedEvent when executor throws exception

* Cleanup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python workflows Related to Workflows in agent-framework

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: [Bug] Missing ExecutorFailedEvent in workflow if an executor throws exception

6 participants