Skip to content

fix(telemetry): make MetricsClient singleton thread-safe#2349

Merged
zastrowm merged 1 commit into
strands-agents:mainfrom
gtholpadi:bug-fix/2342
May 28, 2026
Merged

fix(telemetry): make MetricsClient singleton thread-safe#2349
zastrowm merged 1 commit into
strands-agents:mainfrom
gtholpadi:bug-fix/2342

Conversation

@gtholpadi
Copy link
Copy Markdown
Contributor

Motivation

The MetricsClient singleton 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 before create_instruments() has assigned all the counter/histogram attributes. This causes intermittent AttributeError crashes when running multiple agents in parallel via ThreadPoolExecutor.

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

  • Concurrent agent execution: Running multiple Agent instances in a ThreadPoolExecutor without needing to manually pre-initialize MetricsClient() 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.

  • I ran hatch run prepare

Checklist

  • I have read the CONTRIBUTING document
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly --> NO CHANGE
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

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 zastrowm changed the title fix(telemetry): Make MetricsClient singleton thread-safe fix(telemetry): make MetricsClient singleton thread-safe May 27, 2026
@zastrowm zastrowm enabled auto-merge (squash) May 27, 2026 23:48
@codecov
Copy link
Copy Markdown

codecov Bot commented May 27, 2026

Codecov Report

❌ Patch coverage is 91.66667% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
strands-py/src/strands/telemetry/metrics.py 91.66% 0 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

@zastrowm zastrowm merged commit ef4ce11 into strands-agents:main May 28, 2026
42 of 52 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] MetricsClient singleton is not thread-safe

2 participants