Repository navigation
test(util-genai): count distinct singleton objects in test_thread_safe_single_new - #380
Open
pylaterreur wants to merge 1 commit into
Open
pylaterreur wants to merge 1 commit into
pylaterreur wants to merge 1 commit into
Conversation
…_new test_thread_safe_single_new counted a "fresh instance" whenever TelemetryHandler.__new__ returned an object whose _initialized flag was still False. Threads that reach the singleton while the first thread is still inside __init__ get that same, not yet initialized, object, so the test counts them too and fails although __new__ allocated only one instance. Whether that happens depends on how long __init__ takes. With the packages CI installs, the test failed in 20 of 20 runs on its own on Python 3.13 (30 of 30 on 3.14), and in the full util-genai suite in 2 of 10 runs on 3.13 and 3 of 20 on 3.14. Count distinct returned objects instead. Each allocation is returned by the call that made it, so this still fails if __new__ allocates twice.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
TestGetTelemetryHandlerSingleton::test_thread_safe_single_newis flaky. It counts a "fresh instance" wheneverTelemetryHandler.__new__returns an object whose_initializedflag is stillFalse. Threads that reach the singleton while the first thread is still inside__init__get that same, not yet initialized, object, so the test counts them too and fails withTelemetryHandler created 2 fresh instances, expected 1although__new__allocated only one instance.Whether it fails depends on how long the first
__init__takes. With the packagesci-main.yamlinstalls, I got:util/opentelemetry-util-genai/tests/Changes
Count distinct returned objects instead of
_initializedflags. Each allocation is returned by the call that made it, and the list keeps them alive, so the test still fails if__new__allocates twice.Validation
With a 20 ms sleep added to
TelemetryHandler.__init__to widen the window, the old count reports 9-10 fresh instances in every run, while the number of distinct objects is always 1. The fixed test passed in all 20 full-suite runs on 3.14, andruff check/ruff format --check(0.14.1) pass.Note: the old check also points at a real race this PR doesn't change. Threads that get the instance before the first
__init__finishes see_initializedasFalseand run__init__again on the shared object.🤖 Generated with Claude Code
https://claude.ai/code/session_01GwUvhmzgfkiZ6vo4uGyesN