Skip to content

perf: cache project lookups and deduplicate logins - #934

Merged
Abhijeet Prasad (AbhiPrasad) merged 3 commits into
mainfrom
abhi/port-2523-python-sdk-d71ef9a6
Oct 14, 2026
Merged

Abhijeet Prasad (AbhiPrasad) merged 3 commits into
mainfrom
abhi/port-2523-python-sdk-d71ef9a6

Conversation

@AbhiPrasad

@AbhiPrasad Abhijeet Prasad (AbhiPrasad) commented Oct 14, 2026 •

Copy link
Copy Markdown
Member

python version of braintrustdata/braintrust-sdk-javascript#2523.

here is ai slop explanation:

Loggers and imported span parents currently resolve the same project independently. Share project metadata within each Braintrust state using a 1,000-entry LRU cache with a fixed 15-minute TTL starting when a lookup succeeds. Concurrent callers share the same request and receive independent metadata copies; failed lookups allow later retries.

Deduplicate matching concurrent logins while preserving fresh requests for forced logins. Bound login and project lookups to 30 seconds, cancel pending and queued logins on reset, and ignore late results. Clear project caches when login context or the HTTP adapter changes while keeping resolved logger IDs stable. Project-group registration and the generated API client remain supported.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 14, 2026 •

Copy link
Copy Markdown

Codex Review Summary

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-14T21:26:28.058295Z fe39be0 New commits
ℹ️ 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: d8ff06f069

ℹ️ 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".

Comment thread py/src/braintrust/logger.py Outdated
Comment on lines +864 to +870
self._start_request(
pending,
lambda: login_to_state(app_url=app_url, api_key=api_key, org_name=org_name),
timeout=_LOGIN_TIMEOUT_SECONDS,
label="login",
previous=previous,
on_success=self.copy_state,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Revalidate queued non-forced logins before switching credentials

When a cold BraintrustState receives concurrent non-forced logins with different API keys or organizations, both calls can pass the logged_in recheck before the first request finishes. The second worker then waits for the first and calls login_to_state directly, overwriting the first caller's state without the credential-mismatch error that the previous serialized implementation produced; the first caller can consequently resolve its logger or project against the second caller's organization. Revalidate the active credentials after previous completes, or reject a conflicting pending key unless force_login=True.

Useful? React with 👍 / 👎.

@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: 5f5f4cc3a0

ℹ️ 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".

Comment on lines +910 to +912
if previous is not None:
try:
previous.result()

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 Start the timeout before waiting for the prior login

When multiple forced logins are queued behind a stalled login, each worker waits for previous.result() before starting its own timeout timer. Consequently, the second caller can block for roughly 60 seconds and later callers for N * 30 seconds, despite each login being advertised as bounded to 30 seconds. Start the deadline before the queue wait, or pass the remaining timeout into the actual login attempt.

Useful? React with 👍 / 👎.

Comment thread py/src/braintrust/logger.py Outdated
label="login",
previous=previous,
on_success=self.copy_state,
on_discard=lambda state: state._client.close() if state._client is not None else 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 Close all resources belonging to discarded login states

When a real-key login completes after its future timed out or was canceled by a reset, this callback closes only the generated _client. However, login_to_state also creates a separate long-lived _api_conn and installs it in a background logger whose bound finalizer is retained by atexit, so the legacy session and its connection pool remain alive until process shutdown. Repeated stalled or reset logins can therefore accumulate resources; discard cleanup should close the temporary state's legacy connections as well.

Useful? React with 👍 / 👎.

Abhijeet Prasad (AbhiPrasad) added a commit to braintrustdata/braintrust-sdk-javascript that referenced this pull request Oct 14, 2026
…als (#2623)

the bot found this while working on
braintrustdata/braintrust-sdk-python#934, and I
think it's legit.

- Revalidate active login credentials after a queued non-forced login
waits for its predecessor.
- Reuse matching completed logins and reject conflicting credentials
with `forceLogin: true` guidance.
@AbhiPrasad
Abhijeet Prasad (AbhiPrasad) force-pushed the abhi/port-2523-python-sdk-d71ef9a6 branch from 5f5f4cc to fe39be0 Compare October 14, 2026 21:22

@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: fe39be094f

ℹ️ 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".

timer.cancel()

ctx = contextvars.copy_context()
threading.Thread(target=lambda: ctx.run(run), name=f"braintrust-{label}", daemon=True).start()

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 Bound workers left behind by timed-out requests

When an adapter or transport call hangs permanently, the timer completes the future and removes its login/cache entry, but this daemon worker remains blocked inside compute(). Each subsequent retry therefore starts another native thread while the previous one is still alive, so the stalled-adapter scenario this timeout is intended to recover from can exhaust process threads after repeated retries. Run these requests through a bounded worker pool or otherwise retain/cancel the outstanding computation before permitting another worker.

Useful? React with 👍 / 👎.

@AbhiPrasad
Abhijeet Prasad (AbhiPrasad) merged commit 02b708a into main Oct 14, 2026
98 checks passed
@AbhiPrasad
Abhijeet Prasad (AbhiPrasad) deleted the abhi/port-2523-python-sdk-d71ef9a6 branch October 14, 2026 21:31
@AbhiPrasad

Copy link
Copy Markdown
Member Author

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.

2 participants