Skip to content

Fix flow not terminating on CTRL+C (#2611) #2612

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/crewai/flow/flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,10 @@ def kickoff(self, inputs: Optional[Dict[str, Any]] = None) -> Any:
async def run_flow():
return await self.kickoff_async(inputs)

return asyncio.run(run_flow())
try:
return asyncio.run(run_flow())
except KeyboardInterrupt:
raise

async def kickoff_async(self, inputs: Optional[Dict[str, Any]] = None) -> Any:
"""
Expand Down
26 changes: 26 additions & 0 deletions tests/flow_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -755,3 +755,29 @@ def anemia_analysis(self):
assert execution_order.index("anemia_analysis") > execution_order.index(
"anemia_router"
)


def test_flow_keyboard_interrupt_handling():
"""Test that a flow properly terminates when a keyboard interrupt is received."""
execution_order = []

class KeyboardInterruptFlow(Flow):
@start()
def step_1(self):
execution_order.append("step_1")

@listen(step_1)
def step_2(self):
execution_order.append("step_2")
raise KeyboardInterrupt()

@listen(step_2)
def step_3(self):
execution_order.append("step_3")

flow = KeyboardInterruptFlow()

with pytest.raises(KeyboardInterrupt):
flow.kickoff()

assert execution_order == ["step_1", "step_2"]
Loading