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

fix: injection on try block start for CPython>=3.11

Open P403n1x87 opened this issue 5 months ago • 3 comments

We fix an issue with the injection of hooks on a line that contains just a try:. In later versions of Python this has become a mere NOP placeholder, which we previously ignored.

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

  • [ ] 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

P403n1x87 avatar Jun 27 '25 11:06 P403n1x87

CODEOWNERS have been resolved as:

releasenotes/notes/fix-try-line-injection-7fdfc03e478066f9.yaml         @DataDog/apm-python
ddtrace/internal/bytecode_injection/__init__.py                         @DataDog/apm-core-python
tests/internal/bytecode_injection/test_injection.py                     @DataDog/apm-core-python

github-actions[bot] avatar Jun 27 '25 11:06 github-actions[bot]

Bootstrap import analysis

Comparison of import times between this PR and base.

Summary

The average import time from this PR is: 280 ± 6 ms.

The average import time from base is: 286 ± 6 ms.

The import time difference between this PR and base is: -5.8 ± 0.3 ms.

Import time breakdown

The following import paths have shrunk:

ddtrace.auto 2.330 ms (0.83%)
ddtrace.bootstrap.sitecustomize 1.644 ms (0.59%)
ddtrace.bootstrap.preload 1.644 ms (0.59%)
ddtrace.internal.remoteconfig.client 0.733 ms (0.26%)
ddtrace 0.686 ms (0.25%)
ddtrace.internal._unpatched 0.035 ms (0.01%)
json 0.035 ms (0.01%)
json.decoder 0.035 ms (0.01%)
re 0.035 ms (0.01%)
enum 0.035 ms (0.01%)
types 0.035 ms (0.01%)

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

Benchmarks

Benchmark execution time: 2025-06-30 09:40:52

Comparing candidate commit 1175a98a1771895c33d64d8dad42094a918df0e8 in PR branch fix/try-line-injection with baseline commit a78ab21066956ae82f30e699bec432427c609c90 in branch main.

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

scenario:iastaspects-lstrip_aspect

  • 🟥 execution_time [+760.882ns; +825.050ns] or [+7.311%; +7.927%]

scenario:iastaspects-replace_aspect

  • 🟥 execution_time [+713.895ns; +775.740ns] or [+15.057%; +16.362%]

scenario:iastaspects-strip_aspect

  • 🟥 execution_time [+769.719ns; +848.213ns] or [+7.412%; +8.167%]

pr-commenter[bot] avatar Jun 27 '25 12:06 pr-commenter[bot]

The backport to 2.21 failed:

The process '/usr/bin/git' failed with exit code 1

To backport manually, run these commands in your terminal:

# Fetch latest updates from GitHub
git fetch
# Create a new working tree
git worktree add .worktrees/backport-2.21 2.21
# Navigate to the new working tree
cd .worktrees/backport-2.21
# Create a new branch
git switch --create backport-13792-to-2.21
# Cherry-pick the merged commit of this pull request and resolve the conflicts
git cherry-pick -x --mainline 1 563f14b3f18d612d49579b31259b12cd2d06c140
# Push it to GitHub
git push --set-upstream origin backport-13792-to-2.21
# Go back to the original working tree
cd ../..
# Delete the working tree
git worktree remove .worktrees/backport-2.21

Then, create a pull request where the base branch is 2.21 and the compare/head branch is backport-13792-to-2.21.

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

The backport to 3.9 failed:

The process '/usr/bin/git' failed with exit code 1

To backport manually, run these commands in your terminal:

# Fetch latest updates from GitHub
git fetch
# Create a new working tree
git worktree add .worktrees/backport-3.9 3.9
# Navigate to the new working tree
cd .worktrees/backport-3.9
# Create a new branch
git switch --create backport-13792-to-3.9
# Cherry-pick the merged commit of this pull request and resolve the conflicts
git cherry-pick -x --mainline 1 563f14b3f18d612d49579b31259b12cd2d06c140
# Push it to GitHub
git push --set-upstream origin backport-13792-to-3.9
# Go back to the original working tree
cd ../..
# Delete the working tree
git worktree remove .worktrees/backport-3.9

Then, create a pull request where the base branch is 3.9 and the compare/head branch is backport-13792-to-3.9.

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