Skip to content

chore(native): declare PyContextVar_* locally for limited-API builds (PROF-15850) - #19903

Open
vlad-scherbich wants to merge 3 commits into
vlad/py315-manylinux-wheelsfrom
vlad/py315-pyo3-contextvar
Open

chore(native): declare PyContextVar_* locally for limited-API builds (PROF-15850)#19903
vlad-scherbich wants to merge 3 commits into
vlad/py315-manylinux-wheelsfrom
vlad/py315-pyo3-contextvar

Conversation

@vlad-scherbich

@vlad-scherbich vlad-scherbich commented Aug 27, 2026

Copy link
Copy Markdown
Contributor

Description

pyo3-ffi hides PyContextVar_New / _Get / _Set under not(Py_LIMITED_API). CPython
3.15 builds take PYO3_USE_ABI3_FORWARD_COMPATIBILITY=1 (pyo3-build-config 0.28.3 caps
ABI3_MAX_MINOR at 14), so those symbols vanish and the native module fails with E0425.

This PR declares the three stable C API entry points locally in src/native/contextvar.rs.
Context variables exist since 3.7, so the symbols are on every interpreter we support.

If this merges and nothing else new lands: limited-API contextvar builds compile. No publish,
testrunner, riot, or requires-python change.

Testing

Compiled clean on 3.9, 3.13, and 3.15, with and without the stable-ABI path.

Risks

None. Declarations match the stable C API; they do not change runtime behavior on 3.9–3.14.

Additional Notes

Extracted from #19833 (cc @wconti27). Closes #19908. Same class of fix as #18429.

No release note: internal compile fix, changelog/no-changelog.

Base: #19861. Next: #19905 (testrunner pyenv bump).

@datadog-prod-us1-4

datadog-prod-us1-4 Bot commented Aug 27, 2026

Copy link
Copy Markdown
Contributor

Pipelines  Tests

Unblock PR with BitsAI

⚠️ Warnings

Your PR has failed checks. Please review the issues below and take necessary action before merging.

🚦 5 Pipeline jobs failed

DataDog/apm-reliability/dd-trace-py | rust ci — 🔧 Needs a code fix, caused by this PR

View more details · View in GitLab

DataDog/apm-reliability/dd-trace-py | debugging/exploration/botocore: [3.12] — 🔄 Retry may pass, looks flaky

View more details · View in GitLab

DataDog/apm-reliability/dd-trace-py | profiling_native: [3.13, thread] — 🔄 Retry may pass, looks flaky

View more details · View in GitLab

View all 5 failed jobs.

ℹ️ Info

No other issues found (see more)

🧪 All tests passed
❄️ No new flaky tests detected

Useful? React with 👍 / 👎

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: c2e9f4f | Docs | View more details | Give us feedback!

@pr-commenter

pr-commenter Bot commented Aug 27, 2026

Copy link
Copy Markdown

Benchmarks

Benchmark execution time: 2026-08-28 16:32:50

Comparing candidate commit bb9aaf9 in PR branch vlad/py315-pyo3-contextvar with baseline commit 5bd6b96 in branch main.

📊 Benchmarking dashboard

Found 0 performance improvements and 7 performance regressions! Performance is the same for 575 metrics, 10 unstable metrics, 2 known flaky benchmarks, 16 flaky benchmarks without significant changes.

Explanation

This is an A/B test comparing a candidate commit's performance against that of a baseline commit. Performance changes are noted in the tables below as:

  • 🟩 = significantly better candidate vs. baseline
  • 🟥 = significantly worse candidate vs. baseline

We compute a confidence interval (CI) over the relative difference of means between metrics from the candidate and baseline commits, considering the baseline as the reference.

If the CI is entirely outside the configured SIGNIFICANT_IMPACT_THRESHOLD (or the deprecated UNCONFIDENCE_THRESHOLD), the change is considered significant.

Feel free to reach out to #apm-benchmarking-platform on Slack if you have any questions.

More details about the CI and significant changes

You can imagine this CI as a range of values that is likely to contain the true difference of means between the candidate and baseline commits.

CIs of the difference of means are often centered around 0%, because often changes are not that big:

---------------------------------(------|---^--------)-------------------------------->
                              -0.6%    0%  0.3%     +1.2%
                                 |          |        |
         lower bound of the CI --'          |        |
sample mean (center of the CI) -------------'        |
         upper bound of the CI ----------------------'

As described above, a change is considered significant if the CI is entirely outside the configured SIGNIFICANT_IMPACT_THRESHOLD (or the deprecated UNCONFIDENCE_THRESHOLD).

For instance, for an execution time metric, this confidence interval indicates a significantly worse performance:

----------------------------------------|---------|---(---------^---------)---------->
                                       0%        1%  1.3%      2.2%      3.1%
                                                  |   |         |         |
       significant impact threshold --------------'   |         |         |
                      lower bound of CI --------------'         |         |
       sample mean (center of the CI) --------------------------'         |
                      upper bound of CI ----------------------------------'

scenario:httppropagationextract-tracecontext_headers

  • 🟥 execution_time [+2.330µs; +2.578µs] or [+7.308%; +8.086%]

scenario:httppropagationinject-ids_only

  • 🟥 execution_time [+2.570µs; +2.709µs] or [+15.545%; +16.384%]

scenario:iastaspects-add_aspect

  • 🟥 execution_time [+10.303µs; +13.386µs] or [+10.372%; +13.477%]

scenario:iastaspectsospath-ospathbasename_aspect

  • 🟥 execution_time [+134.485µs; +142.396µs] or [+32.471%; +34.381%]

scenario:iastaspectssplit-rsplit_aspect

  • 🟥 execution_time [+36.684µs; +41.600µs] or [+25.768%; +29.221%]

scenario:telemetryaddmetric-1-count-metric-1-times

  • 🟥 execution_time [+637.013ns; +682.242ns] or [+23.846%; +25.539%]

scenario:tracer-small

  • 🟥 execution_time [+29.083µs; +31.390µs] or [+8.952%; +9.661%]

Unstable benchmarks

These benchmarks have a confidence interval too wide to call a change; treat them as noise rather than signal.

scenario:coreapiscenario-context_with_data_listeners

  • unstable execution_time [-656.499ns; +801.534ns] or [-6.008%; +7.335%]

scenario:coreapiscenario-core_dispatch_1_listener

  • unstable execution_time [-33.958ns; +31.883ns] or [-5.553%; +5.213%]

scenario:coreapiscenario-core_dispatch_50_listeners

  • unstable execution_time [-1358.135ns; +1911.008ns] or [-8.104%; +11.402%]

scenario:coreapiscenario-core_dispatch_exception_listeners

  • unstable execution_time [-895.828ns; +1596.063ns] or [-6.976%; +12.429%]

scenario:coreapiscenario-core_dispatch_listeners

  • unstable execution_time [-251.536ns; +395.318ns] or [-6.949%; +10.921%]

scenario:coreapiscenario-core_dispatch_no_args_listeners

  • unstable execution_time [-190.197ns; +315.854ns] or [-6.620%; +10.993%]

scenario:coreapiscenario-core_dispatch_with_results_1_listener

  • unstable execution_time [-80.205ns; +70.281ns] or [-6.809%; +5.967%]

scenario:coreapiscenario-core_dispatch_with_results_50_listeners

  • unstable execution_time [-3963.518ns; +3991.105ns] or [-9.730%; +9.798%]

scenario:coreapiscenario-core_dispatch_with_results_listeners

  • unstable execution_time [-846.194ns; +694.760ns] or [-10.453%; +8.583%]

scenario:packagesupdateimporteddependencies-import_many_stdlib_cached

  • unstable execution_time [-54.216µs; +63.749µs] or [-8.701%; +10.230%]

Known flaky benchmarks

These benchmarks are marked as flaky and will not trigger a failure. Modify FLAKY_BENCHMARKS_REGEX to control which benchmarks are marked as flaky.

scenario:iastaspects-casefold_noaspect

  • 🟥 execution_time [+62.972µs; +69.109µs] or [+25.204%; +27.661%]

scenario:iastaspects-translate_aspect

  • 🟥 execution_time [+90.008µs; +97.550µs] or [+20.582%; +22.306%]

Known flaky benchmarks without significant changes:

  • scenario:errortrackingflasksqli-baseline
  • scenario:flasksimple-iast-get
  • scenario:iastaspects-casefold_aspect
  • scenario:iastaspects-index_aspect
  • scenario:iastaspects-ljust_noaspect
  • scenario:iastaspects-lower_aspect
  • scenario:iastaspects-replace_aspect
  • scenario:iastaspects-swapcase_aspect
  • scenario:iastaspects-title_noaspect
  • scenario:iastaspects-translate_noaspect
  • scenario:iastaspects-upper_noaspect
  • scenario:packagespackageforrootmodulemapping-cache_off
  • scenario:packagespackageforrootmodulemapping-cache_on
  • scenario:sethttpmeta-all-enabled
  • scenario:span-start
  • scenario:telemetryaddmetric-record-100-metrics

@vlad-scherbich

Copy link
Copy Markdown
Contributor Author

Part of #19908

@vlad-scherbich vlad-scherbich added the changelog/no-changelog A changelog entry is not required for this PR. label Aug 27, 2026
@vlad-scherbich
vlad-scherbich marked this pull request as ready for review August 27, 2026 21:03
@vlad-scherbich
vlad-scherbich requested a review from a team as a code owner August 27, 2026 21:03
@vlad-scherbich
vlad-scherbich requested review from juanjux and a lite review from Copilot August 27, 2026 21:03
@vlad-scherbich vlad-scherbich changed the title chore(native): declare PyContextVar_* locally for limited-API builds chore(native): declare PyContextVar_* locally for limited-API builds (PROF-15850) Aug 27, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes Rust native extension builds under Py_LIMITED_API (e.g., CPython 3.15 builds using PYO3_USE_ABI3_FORWARD_COMPATIBILITY) by locally declaring the PyContextVar_New, PyContextVar_Get, and PyContextVar_Set C-API entry points that are not exposed by pyo3-ffi in that configuration.

Changes:

  • Add local extern "C" declarations for PyContextVar_* functions in src/native/contextvar.rs.
  • Switch call sites from ffi::PyContextVar_* to the locally declared symbols so the module compiles with and without Py_LIMITED_API.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/native/contextvar.rs Outdated
@vlad-scherbich
vlad-scherbich force-pushed the vlad/py315-pyo3-contextvar branch from 5e2e905 to 9c1783d Compare August 27, 2026 21:11
@vlad-scherbich
vlad-scherbich requested a review from a team as a code owner August 27, 2026 21:11
@vlad-scherbich
vlad-scherbich changed the base branch from main to vlad/no-cp315-to-pypi August 27, 2026 21:11
@cit-pr-commenter-54b7da

cit-pr-commenter-54b7da Bot commented Aug 27, 2026

Copy link
Copy Markdown

Codeowners resolved as

Resolved from the full PR diff against vlad/py315-manylinux-wheels using the target branch CODEOWNERS file.
CODEOWNERS team requests not listed below are not required by the current file set.

src/native/contextvar.rs                                                @DataDog/apm-core-python

@cit-pr-commenter-54b7da

cit-pr-commenter-54b7da Bot commented Aug 27, 2026

Copy link
Copy Markdown

Circular import analysis

⚠️ Existing circular imports

There are 3 circular imports that already exist on the base branch and have not been changed by this PR.

ddtrace.errortracking._handled_exceptions.bytecode_injector -> ddtrace.errortracking._handled_exceptions.callbacks -> ddtrace.errortracking._handled_exceptions.collector -> ddtrace.errortracking._handled_exceptions.bytecode_reporting -> ddtrace.errortracking._handled_exceptions.bytecode_injector
ddtrace.llmobs -> ddtrace.llmobs._evaluators -> ddtrace.llmobs._evaluators.format -> ddtrace.llmobs._experiment -> ddtrace.llmobs
ddtrace.appsec._asm_request_context -> ddtrace.appsec._iast._iast_request_context_base -> ddtrace.appsec._iast._iast_env -> ddtrace.appsec._iast.reporter -> ddtrace.appsec._exploit_prevention.stack_traces -> ddtrace.appsec._asm_request_context

@cit-pr-commenter-54b7da

cit-pr-commenter-54b7da Bot commented Aug 27, 2026

Copy link
Copy Markdown

Dependency direction analysis

⚠️ Existing dependency direction violations

There are 240 dependency direction violations that already exist on the base branch and have not been changed by this PR.

Show existing violations (showing 5 of 240 highest severity)
ddtrace.internal.tracemethods -×-> ddtrace.trace  (internal-core -> product:tracing, score=135)
ddtrace.llmobs._integrations.crewai -×-> ddtrace.trace  (product:llmobs -> product:tracing, score=133)
ddtrace.llmobs._integrations.openai_agents -×-> ddtrace.trace  (product:llmobs -> product:tracing, score=133)
ddtrace.profiling.scheduler -×-> ddtrace.trace  (product:profiling -> product:tracing, score=133)
ddtrace.llmobs._integrations.pydantic_ai -×-> ddtrace.trace  (product:llmobs -> product:tracing, score=133)

To see all violations, download the layers-base.json and layers-pr.json artifacts from this CI job and run:

uv run --script scripts/import-analysis/layers.py compare layers-base.json layers-pr.json

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 5e2e90539b

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/native/contextvar.rs

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated no new comments.

@vlad-scherbich
vlad-scherbich force-pushed the vlad/py315-pyo3-contextvar branch 2 times, most recently from f9ee924 to 12acbff Compare August 28, 2026 14:57
@vlad-scherbich
vlad-scherbich requested a review from a team as a code owner August 28, 2026 14:57
@vlad-scherbich
vlad-scherbich changed the base branch from vlad/no-cp315-to-pypi to vlad/py315-manylinux-wheels August 28, 2026 14:58
@vlad-scherbich
vlad-scherbich force-pushed the vlad/py315-pyo3-contextvar branch from 12acbff to 59047aa Compare August 28, 2026 15:51
@vlad-scherbich
vlad-scherbich requested review from a team as code owners August 28, 2026 15:51
@vlad-scherbich
vlad-scherbich requested review from KowalskiThomas and dubloom and removed request for a team August 28, 2026 15:51
@vlad-scherbich
vlad-scherbich changed the base branch from vlad/py315-manylinux-wheels to vlad/315-profiling-dev-tooling August 28, 2026 15:52
@vlad-scherbich
vlad-scherbich force-pushed the vlad/py315-pyo3-contextvar branch from 59047aa to bb9aaf9 Compare August 28, 2026 16:07
@vlad-scherbich
vlad-scherbich changed the base branch from vlad/315-profiling-dev-tooling to vlad/py315-manylinux-wheels August 28, 2026 16:07
@vlad-scherbich
vlad-scherbich force-pushed the vlad/py315-manylinux-wheels branch from 4362679 to c493409 Compare August 28, 2026 18:33
wconti27 and others added 3 commits August 28, 2026 14:33
pyo3-ffi gates its entire `context` module behind `not(Py_LIMITED_API)`
(pyo3-ffi 0.28.3 `src/lib.rs` lines 436 and 503), so `ffi::PyContextVar_New`,
`ffi::PyContextVar_Get` and `ffi::PyContextVar_Set` disappear whenever the
extension is built against the stable ABI. On CPython 3.15 that happens via
`PYO3_USE_ABI3_FORWARD_COMPATIBILITY=1`, since pyo3-build-config 0.28.3 caps
`ABI3_MAX_MINOR` at 14. The build then fails with three E0425 "cannot find
function ... in module `ffi`" errors on this file.

Declare the three stable C API entry points locally so the module compiles in
both configurations. Context variables date to Python 3.7, so the symbols are
present on every interpreter ddtrace supports; verified to compile clean on
3.9, 3.13 and 3.15 with and without the stable-ABI path.

Same class of fix as #18429, which closed the sibling `PyFrame_GetBack` gap for
the crashtracker.

Extracted from fc09033 on #19833, which
bundled it with three unrelated formatting fixes. The code is unchanged from
that commit; only the explanatory comment above the declarations was reworded.

Co-authored-by: Vlad Scherbich <vlad.scherbich@datadoghq.com>
@vlad-scherbich
vlad-scherbich force-pushed the vlad/py315-pyo3-contextvar branch from bb9aaf9 to c2e9f4f Compare August 28, 2026 18:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changelog/no-changelog A changelog entry is not required for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[3.15] Native/contextvar: PyContextVar_* hidden under Py_LIMITED_API

3 participants