Skip to content

Commit feec6b4

Browse files
fix: gracefully terminate the future when executing a task async
* fix: gracefully terminate the future when executing a task async * core: add unit test --------- Co-authored-by: Greyson LaLonde <[email protected]>
1 parent e43c7de commit feec6b4

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

lib/crewai/src/crewai/task.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -494,8 +494,11 @@ def _execute_task_async(
494494
future: Future[TaskOutput],
495495
) -> None:
496496
"""Execute the task asynchronously with context handling."""
497-
result = self._execute_core(agent, context, tools)
498-
future.set_result(result)
497+
try:
498+
result = self._execute_core(agent, context, tools)
499+
future.set_result(result)
500+
except Exception as e:
501+
future.set_exception(e)
499502

500503
async def aexecute_sync(
501504
self,

lib/crewai/tests/test_task.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1727,3 +1727,24 @@ def test_task_output_includes_messages():
17271727
assert hasattr(task2_output, "messages")
17281728
assert isinstance(task2_output.messages, list)
17291729
assert len(task2_output.messages) > 0
1730+
1731+
1732+
def test_async_execution_fails():
1733+
researcher = Agent(
1734+
role="Researcher",
1735+
goal="Make the best research and analysis on content about AI and AI agents",
1736+
backstory="You're an expert researcher, specialized in technology, software engineering, AI and startups. You work as a freelancer and is now working on doing research and analysis for a new customer.",
1737+
allow_delegation=False,
1738+
)
1739+
1740+
task = Task(
1741+
description="Give me a list of 5 interesting ideas to explore for na article, what makes them unique and interesting.",
1742+
expected_output="Bullet point list of 5 interesting ideas.",
1743+
async_execution=True,
1744+
agent=researcher,
1745+
)
1746+
1747+
with patch.object(Task, "_execute_core", side_effect=RuntimeError("boom!")):
1748+
with pytest.raises(RuntimeError, match="boom!"):
1749+
execution = task.execute_async(agent=researcher)
1750+
execution.result()

0 commit comments

Comments
 (0)