From 4859dc66a5d4dff3e52ef8a1cb2a0b6cb4b5410d Mon Sep 17 00:00:00 2001 From: Tiago Freire Date: Wed, 18 Mar 2026 21:29:35 -0300 Subject: [PATCH] =?UTF-8?q?fix:=20address=20PR=20review=20findings=20?= =?UTF-8?q?=E2=80=94=20forward=20skip=5Fcontext=5Fcheck=20in=20=20=20recur?= =?UTF-8?q?sive=20fallback,=20reduce=20retry=20backoff,=20clean=20up=20bat?= =?UTF-8?q?ch=20state?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Forward skip_context_check parameter in the 401/403 ephemeral fallback recursive call to prevent silent early return when is_tracing_enabled_in_context() is False - Reduce retry backoff from 1s to 200ms to minimize lock hold time on the non-first-time path (worst case 400ms vs 2s) - Add batch state cleanup after _finalize_backend_batch in the first-time handler, mirroring finalize_batch: reset current_batch, event_buffer, trace_batch_id, is_current_batch_ephemeral, batch_owner_type, batch_owner_id, and call _cleanup_batch_data() --- .../events/listeners/tracing/first_time_trace_handler.py | 9 +++++++++ .../events/listeners/tracing/trace_batch_manager.py | 9 ++++++--- lib/crewai/tests/tracing/test_tracing.py | 2 +- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/lib/crewai/src/crewai/events/listeners/tracing/first_time_trace_handler.py b/lib/crewai/src/crewai/events/listeners/tracing/first_time_trace_handler.py index 2e3bc5357..e3bb7c0c3 100644 --- a/lib/crewai/src/crewai/events/listeners/tracing/first_time_trace_handler.py +++ b/lib/crewai/src/crewai/events/listeners/tracing/first_time_trace_handler.py @@ -122,6 +122,15 @@ class FirstTimeTraceHandler: self.batch_manager._finalize_backend_batch(events_count) self.ephemeral_url = self.batch_manager.ephemeral_trace_url + # Clean up batch state (mirrors finalize_batch cleanup) + self.batch_manager.batch_owner_type = None + self.batch_manager.batch_owner_id = None + self.batch_manager.current_batch = None + self.batch_manager.event_buffer.clear() + self.batch_manager.trace_batch_id = None + self.batch_manager.is_current_batch_ephemeral = False + self.batch_manager._cleanup_batch_data() + if not self.ephemeral_url: self._show_local_trace_message() diff --git a/lib/crewai/src/crewai/events/listeners/tracing/trace_batch_manager.py b/lib/crewai/src/crewai/events/listeners/tracing/trace_batch_manager.py index 747c3dad9..7ce51fa9c 100644 --- a/lib/crewai/src/crewai/events/listeners/tracing/trace_batch_manager.py +++ b/lib/crewai/src/crewai/events/listeners/tracing/trace_batch_manager.py @@ -162,14 +162,14 @@ class TraceBatchManager: f"Trace batch init attempt {attempt + 1} failed " f"(status={response.status_code if response else 'None'}), retrying..." ) - time.sleep(1) + time.sleep(0.2) except Exception as e: last_exception = e if attempt < max_retries: logger.debug( f"Trace batch init attempt {attempt + 1} raised {type(e).__name__}, retrying..." ) - time.sleep(1) + time.sleep(0.2) if last_exception and response is None: logger.warning( @@ -192,7 +192,10 @@ class TraceBatchManager: ) self.is_current_batch_ephemeral = True return self._initialize_backend_batch( - user_context, execution_metadata, use_ephemeral=True + user_context, + execution_metadata, + use_ephemeral=True, + skip_context_check=skip_context_check, ) if response.status_code in [201, 200]: diff --git a/lib/crewai/tests/tracing/test_tracing.py b/lib/crewai/tests/tracing/test_tracing.py index a1d4a1185..affe558d2 100644 --- a/lib/crewai/tests/tracing/test_tracing.py +++ b/lib/crewai/tests/tracing/test_tracing.py @@ -1099,7 +1099,7 @@ class TestInitializeBackendBatchRetry: assert bm.trace_batch_id == server_id assert mock_init.call_count == 2 - mock_sleep.assert_called_once_with(1) + mock_sleep.assert_called_once_with(0.2) def test_retries_on_5xx_then_succeeds(self): """Retries on 500 server error, succeeds on second attempt."""