Skip to content

Commit 033e9b3

Browse files
fix(tracing_utils): resolves RuntimeError dictionary changed size during iteration [backport 1.20] (#7808)
Backport 215f2bf from #7427 to 1.20. We've been seeing this infrequently in a FastAPI app and decided to dig in. Looks like `context._meta` is changing size during iteration. Saw a similar issue [here](#1745) so implementing a similar fix. --- `1.x` PR: #7439 ## Checklist - [x] Change(s) are motivated and described in the PR description. - [x] Testing strategy is described if automated tests are not included in the PR. - [x] Risk is outlined (performance impact, potential for breakage, maintainability, etc). - [x] Change is maintainable (easy to change, telemetry, documentation). - [x] [Library release note guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html) are followed. If no release note is required, add label `changelog/no-changelog`. - [x] Documentation is included (in-code, generated user docs, [public corp docs](https://github.com/DataDog/documentation/)). - [x] Backport labels are set (if [applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)) ## Reviewer Checklist - [x] Title is accurate. - [x] No unnecessary changes are introduced. - [x] Description motivates each change. - [x] Avoids breaking [API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) changes unless absolutely necessary. - [x] Testing strategy adequately addresses listed risk(s). - [x] Change is maintainable (easy to change, telemetry, documentation). - [x] Release note makes sense to a user of the library. - [x] Reviewer has explicitly acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment. - [x] Backport labels are set in a manner that is consistent with the [release branch maintenance policy](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting) - [x] If this PR touches code that signs or publishes builds or packages, or handles credentials of any kind, I've requested a review from `@DataDog/security-design-and-guidance`. - [x] This PR doesn't touch any of that. Co-authored-by: Gil Aviv <[email protected]>
1 parent f6339fc commit 033e9b3

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

ddtrace/internal/utils/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ def set_argument_value(
7777
def _get_metas_to_propagate(context):
7878
# type: (Any) -> List[Tuple[str, str]]
7979
metas_to_propagate = []
80-
for k, v in context._meta.items():
80+
# copying context._meta.items() to avoid RuntimeError: dictionary changed size during iteration
81+
for k, v in list(context._meta.items()):
8182
if isinstance(k, six.string_types) and k.startswith("_dd.p."):
8283
metas_to_propagate.append((k, v))
8384
return metas_to_propagate
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
fixes:
3+
- |
4+
tracing: This fix resolves an issue where concurrent mutations to the ``context._meta`` dict
5+
caused `RuntimeError: dictionary changed size during iteration`.

0 commit comments

Comments
 (0)