Repository navigation
Conversation
Span finish records telemetry metrics through _recorder_lock, and integrations finish spans from weakref.finalize callbacks. The cyclic GC can run such a finalizer on a thread that already holds the lock, which deadlocked that thread on the non-reentrant lock. Use forksafe.RLock().
Codeowners resolved asResolved from the full PR diff against |
Dependency direction analysis
|
🎉 All green!🧪 All tests passed 🔗 Commit SHA: 1ecc5ea | Docs | View more details | Give us feedback! |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour 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. |
There was a problem hiding this comment.
The reentrant lock lets a GC finalizer finish a span without waiting on its own thread. If the callback reenters recorder creation or first-write registration for the same metric, span counts can split across duplicate native contexts.
🤖 Bits Code Review · Commit 1ecc5ea
| # Guards the registry and every transition of a recorder's binding. | ||
| _recorder_lock = forksafe.Lock() | ||
| # Reentrant: GC finalizers that finish spans can record metrics on a thread already holding it. | ||
| _recorder_lock = forksafe.RLock() |
There was a problem hiding this comment.
Keep first writes on one metric context during GC reentry
If cyclic GC finishes another span for the same integration during the first write to a pending spans_finished recorder, the reentrant lock lets the nested write enter while the recorder still points to the pending worker. Both writes then register a native context for the same metric and send their points under different context keys, splitting telemetry that should aggregate together. The static reviewer reproduced the two registrations with a fake worker. The fix needs to handle nested writes during registration while preserving both points.
React: 👍 useful · 👎 wrong · 😕 unclear · 👀 right, but not acting on it
What each reaction means
- 👍 Useful: correct, and you're fixing it or will.
- 👎 Wrong: the finding is incorrect, for example the bug can't happen or the code was misread.
- 😕 Unclear: you can't tell what it means or why it matters.
- 👀 Right, not acting: correct, but intended, out of scope for this PR, a known risk, or too minor.
🤖 Bits Code Review · Open Bits AI session
| # Guards the registry and every transition of a recorder's binding. | ||
| _recorder_lock = forksafe.Lock() | ||
| # Reentrant: GC finalizers that finish spans can record metrics on a thread already holding it. | ||
| _recorder_lock = forksafe.RLock() |
There was a problem hiding this comment.
Preserve recorder deduplication when GC reenters creation
If a GC finalizer requests the same spans_finished metric after the outer registry lookup misses and before it inserts a recorder, the nested call can create and use its own recorder. The outer call then overwrites the registry entry with a second recorder. With an active worker, both recorders can register distinct contexts for the same metric and split its span counts. The static reviewer reproduced the duplicate creation with a fake worker. Recorder creation needs to preserve the entry installed by the nested call.
React: 👍 useful · 👎 wrong · 😕 unclear · 👀 right, but not acting on it
What each reaction means
- 👍 Useful: correct, and you're fixing it or will.
- 👎 Wrong: the finding is incorrect, for example the bug can't happen or the code was misread.
- 😕 Unclear: you can't tell what it means or why it matters.
- 👀 Right, not acting: correct, but intended, out of scope for this PR, a known risk, or too minor.
🤖 Bits Code Review · Open Bits AI session
BenchmarksBenchmark execution time: 2026-11-23 17:08:51 Comparing candidate commit 1ecc5ea in PR branch Found 0 performance improvements and 0 performance regressions! Performance is the same for 622 metrics, 10 unstable metrics, 1 flaky benchmarks without significant changes.
|
Why
weakref.finalize): grpc, google_adk, openai realtime._recorder_lock._recorder_lock.What
_recorder_lockis now aforksafe.RLock(), so the same thread can take it twice.Customer impact
Removes a rare hang of an application thread. No hang has been reported. Release note added.
Testing
tests/telemetry/passes locally.Risks
Low. Only the lock type changes. It is still reset in the child after a fork.
🤖 Generated with Claude Code