Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix class BoundedAttributes to have RLock rather than Lock #3859

Merged
merged 10 commits into from
May 23, 2024
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

- Fix class BoundedAttributes to have RLock rather than Lock
([#3859](https://github.com/open-telemetry/opentelemetry-python/pull/3859))
- Remove thread lock by loading RuntimeContext explicitly.
([#3763](https://github.com/open-telemetry/opentelemetry-python/pull/3763))
- Update proto version to v1.2.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def __init__(
self.max_value_len = max_value_len
# OrderedDict is not used until the maxlen is reached for efficiency.
self._dict = {} # type: dict | OrderedDict
self._lock = threading.Lock() # type: threading.Lock
self._lock = threading.RLock() # type: threading.RLock
if attributes:
for key, value in attributes.items():
self[key] = value
Expand Down
14 changes: 14 additions & 0 deletions opentelemetry-api/tests/attributes/test_attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,17 @@ def test_immutable(self):
bdict = BoundedAttributes()
with self.assertRaises(TypeError):
bdict["should-not-work"] = "dict immutable"

def test_locking(self):
"""Supporting test case for a commit titled: Fix class BoundedAttributes to have RLock rather than Lock. See #3858.
The change was introduced because __iter__ of the class BoundedAttributes holds lock, and we observed some deadlock symptoms
in the codebase. This test case is to verify that the fix works as expected.
"""
bdict = BoundedAttributes(immutable=False)

with bdict._lock:
for num in range(100):
bdict[str(num)] = num

for num in range(100):
self.assertEqual(bdict[str(num)], num)