Skip to content
Open
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
31 changes: 31 additions & 0 deletions py/src/braintrust/test_trace.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Tests for Trace functionality."""

import asyncio
import os

import braintrust
Expand Down Expand Up @@ -551,6 +552,36 @@ def fake_invoke(**kwargs):
assert calls[0]["global_function"] == "custom_preprocessor"
assert calls[0]["function_type"] == "preprocessor"

@pytest.mark.asyncio
@pytest.mark.parametrize("failure", ["flush", "invoke"])
async def test_retries_failed_thread_fetch(self, monkeypatch, failure):
flushes = 0
invokes = 0
messages = [{"role": "user", "content": "retry succeeded"}]

async def flush():
nonlocal flushes
flushes += 1
if failure == "flush" and flushes == 1:
raise RuntimeError("transient flush failure")

def fake_invoke(**kwargs):
nonlocal invokes
invokes += 1
if failure == "invoke" and invokes == 1:
raise RuntimeError("transient invoke failure")
return messages

monkeypatch.setattr("braintrust.trace.invoke", fake_invoke)
trace = LocalTrace("experiment", "exp-123", "root-456", flush, _DummyState())
with pytest.raises(RuntimeError, match=f"transient {failure} failure"):
await trace.get_thread()

assert await asyncio.gather(trace.get_thread(), trace.get_thread()) == [messages, messages]
assert await trace.get_thread() == messages
assert flushes == (2 if failure == "flush" else 1)
assert invokes == (2 if failure == "invoke" else 1)

@pytest.mark.asyncio
async def test_caches_by_preprocessor(self, monkeypatch):
call_count = 0
Expand Down
9 changes: 6 additions & 3 deletions py/src/braintrust/trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -594,9 +594,12 @@ async def get_thread(self, options: GetThreadOptions | None = None) -> list[Any]
"""
preprocessor = options.get("preprocessor") if options and options.get("preprocessor") else None
cache_key = preprocessor or "project_default"
if cache_key not in self._thread_cache:
self._thread_cache[cache_key] = asyncio.create_task(self._fetch_thread(options))
return await self._thread_cache[cache_key]
task = self._thread_cache.get(cache_key)
# Share in-flight work and successful results, not a previous failed attempt.
if task is None or (task.done() and (task.cancelled() or task.exception() is not None)):

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Reset the cancelled flush before retrying the thread

When a get_thread() caller is cancelled while _ensure_spans_ready() is awaiting the production flush callback, that cancellation also cancels _spans_flush_promise. flush_and_mark() clears the promise only under except Exception, but asyncio.CancelledError is a BaseException; consequently this new task.cancelled() branch creates a replacement that immediately awaits the permanently cancelled flush task, and every later explicit retry is cancelled instead of rerunning the flush. Clear the flush promise on cancellation (and cover this path) before treating cancelled thread tasks as retryable.

Useful? React with 👍 / 👎.

task = asyncio.create_task(self._fetch_thread(options))
self._thread_cache[cache_key] = task
return await task

async def _fetch_thread(self, options: GetThreadOptions | None = None) -> list[Any]:
"""Fetch thread messages via preprocessor invocation."""
Expand Down