dd-trace-py icon indicating copy to clipboard operation
dd-trace-py copied to clipboard

fix: wrapping context __exit__ leak [backport 2.21]

Open github-actions[bot] opened this issue 6 months ago • 2 comments

Backport e7c0d746efa3f8cc39a72d803e3d8c13a54c906f from #13626 to 2.21.

We fix a leak in the universal wrapping context caused to bound exit method leaking in CPython 3.9 and 3.10. This was due to a reference to the bound method exit of the universal wrapping context not getting popped from the stack before every return statement. Every call to a wrapped function would then create one such bounded method object that would not get GC'd. No other versions of CPython are affected because the bytecode and/or approach are different.

Checklist

  • [x] PR author has checked that all the criteria below are met
  • The PR description includes an overview of the change
  • The PR description articulates the motivation for the change
  • The change includes tests OR the PR description describes a testing strategy
  • The PR description notes risks associated with the change, if any
  • Newly-added code is easy to change
  • The change follows the library release note guidelines
  • The change includes or references documentation updates if necessary
  • Backport labels are set (if applicable)

Reviewer Checklist

  • [x] Reviewer has checked that all the criteria below are met
  • Title is accurate
  • All changes are related to the pull request's stated goal
  • Avoids breaking API changes
  • Testing strategy adequately addresses listed risks
  • Newly-added code is easy to change
  • Release note makes sense to a user of the library
  • If necessary, author has acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment
  • Backport labels are set in a manner that is consistent with the release branch maintenance policy

github-actions[bot] avatar Jun 10 '25 12:06 github-actions[bot]

CODEOWNERS have been resolved as:

releasenotes/notes/fix-wrapping-context-memory-leak-ad3d8f27586ba662.yaml  @DataDog/apm-python
ddtrace/internal/wrapping/context.py                                    @DataDog/apm-core-python
tests/internal/test_wrapping.py                                         @DataDog/debugger-python @DataDog/apm-core-python

github-actions[bot] avatar Jun 10 '25 12:06 github-actions[bot]

Benchmarks

Benchmark execution time: 2025-06-18 11:00:35

Comparing candidate commit db0db1a2cd093b10c2ed3174c30973359603fb5e in PR branch backport-13626-to-2.21 with baseline commit 89e8597a02cb00bb1b11e0036817dd5a1fdc4662 in branch 2.21.

Found 0 performance improvements and 0 performance regressions! Performance is the same for 328 metrics, 2 unstable metrics.

pr-commenter[bot] avatar Jun 18 '25 11:06 pr-commenter[bot]

This pull request has been automatically closed after a period of inactivity. After this much time, it will likely be easier to open a new pull request with the same changes than to update this one from the base branch. Please comment or reopen if you think this pull request was closed in error.

github-actions[bot] avatar Jul 20 '25 00:07 github-actions[bot]