Skip to content

Allow trace thread retrieval to retry after a transient failure - #1148

Open
Gareth.Y (linjiaye929) wants to merge 1 commit into
braintrustdata:mainfrom
linjiaye929:fix/retry-failed-trace-thread
Open

Gareth.Y (linjiaye929) wants to merge 1 commit into
braintrustdata:mainfrom
linjiaye929:fix/retry-failed-trace-thread

Conversation

@linjiaye929

Copy link
Copy Markdown
Contributor

LocalTrace.get_thread() caches the task for each preprocessor. If a span flush or preprocessor invocation fails once, a later call on the same trace awaits the already-failed task and gets the original exception without attempting the operation again. In the flush case this also prevents the retry that _ensure_spans_ready() already supports.

Keep sharing in-flight work and successful results. On the caller’s next explicit request, replace a completed task that failed or was cancelled. The initial error still propagates. This adds no automatic retries and does not change how cancellation propagates between active waiters.

The regression covers a one-time flush failure and a one-time invoke failure. In each case, two concurrent retries share a successful fetch and a later call uses the cached messages.

Verification on Linux, Python 3.14.7, against 4df5c78d:

  • Both new cases failed before the fix.
  • test_trace.py: 43 passed, using VCR replay.
  • make fixup: all seven configured hooks passed.
  • make -C py test-core: 1,227 passed, 112 skipped, 12 expected failures; two pytest-asyncio deprecation warnings.

Prepared with AI assistance. Live provider integrations and the full OS/Python CI matrix were not run locally.

@linjiaye929
Gareth.Y (linjiaye929) marked this pull request as ready for review November 19, 2026 04:48
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-19T04:51:53.376515Z b7fd563 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: b7fd563b9b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "Codex (@codex) review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "Codex (@codex) address that feedback".

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 👍 / 👎.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant