Files
crewAI/tests/test_rich_live_cleanup.py
Devin AI 20cdc92142 fix: properly cleanup Rich Live sessions after flow/crew completion
- Add stop_live() method to ConsoleFormatter to clean up Live sessions
- Call cleanup in FlowFinishedEvent and CrewKickoffCompletedEvent handlers
- Add comprehensive tests for Live session cleanup functionality
- Fixes issue #3136 where logging output was suppressed after CrewAI operations

The issue was that Rich Live sessions were not being explicitly stopped
when CrewAI flows or crews completed, leaving the terminal in a state
where subsequent logging output would be suppressed until process exit.

This fix ensures that Live sessions are properly cleaned up by:
1. Adding a stop_live() method that safely stops and clears Live sessions
2. Calling this cleanup method in the appropriate event handlers
3. Adding tests to prevent regression

Resolves #3136

Co-Authored-By: Jo\u00E3o <joao@crewai.com>
2025-07-11 08:04:32 +00:00

58 lines
2.0 KiB
Python

import logging
from io import StringIO
from unittest.mock import MagicMock, patch
from rich.logging import RichHandler
from rich.tree import Tree
from crewai.utilities.events.utils.console_formatter import ConsoleFormatter
from crewai.utilities.events.event_listener import EventListener
class TestRichLiveCleanup:
"""Test that Rich Live sessions are properly cleaned up after CrewAI operations."""
def test_logging_works_after_tree_rendering(self):
"""Test that logging output appears after tree rendering with proper cleanup."""
formatter = ConsoleFormatter()
tree = Tree("Test Flow")
formatter.print(tree)
assert formatter._live is not None
formatter.stop_live()
assert formatter._live is None
with patch.object(formatter.console, 'print') as mock_print:
formatter.print("This should appear immediately")
mock_print.assert_called_once_with("This should appear immediately")
def test_event_listener_cleanup_integration(self):
"""Test that EventListener properly cleans up Live sessions."""
event_listener = EventListener()
formatter = event_listener.formatter
tree = Tree("Test Crew")
formatter.print(tree)
assert formatter._live is not None
formatter.stop_live()
assert formatter._live is None
def test_stop_live_restores_normal_output(self):
"""Test that stop_live properly restores normal console output behavior."""
formatter = ConsoleFormatter()
tree = Tree("Test Tree")
formatter.print(tree)
assert formatter._live is not None
formatter.stop_live()
assert formatter._live is None
with patch.object(formatter.console, 'print') as mock_print:
formatter.print("Normal output")
mock_print.assert_called_once_with("Normal output")