fix(telemetry): make MetricsClient singleton thread-safe#2349
Merged
Conversation
Add threading.Lock with double-checked locking to MetricsClient's __new__ and __init__ methods to prevent race conditions when multiple threads concurrently initialize the singleton. Without this fix, concurrent agent invocations can intermittently raise AttributeError when accessing instrument attributes before create_instruments() completes. Closes strands-agents#2342
zastrowm
approved these changes
May 27, 2026
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
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.
Motivation
The
MetricsClientsingleton uses a pattern where__new__creates the instance and__init__sets up OpenTelemetry instruments. Without synchronization, concurrent threads can observe a partially-initialized instance — one thread creates the object while another thread gets that same object beforecreate_instruments()has assigned all the counter/histogram attributes. This causes intermittentAttributeErrorcrashes when running multiple agents in parallel viaThreadPoolExecutor.Resolves: #2342
Public API Changes
No public API changes. The fix is internal to
MetricsClient's initialization logic. Existing single-threaded and multi-threaded usage continues to work identically — the singleton is simply safe to access concurrently now.Use Cases
Agentinstances in aThreadPoolExecutorwithout needing to manually pre-initializeMetricsClient()on the main thread first.Description
Add threading.Lock with double-checked locking to MetricsClient's new and init methods to prevent race conditions when multiple threads concurrently initialize the singleton. Without this fix, concurrent agent invocations can intermittently raise AttributeError when accessing instrument attributes before create_instruments() completes.
Type of Change
Bug fix
Testing
Tested locally via new unit test and existing integration tests.
hatch run prepareChecklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.