Skip to content

GH-43536: [Python] Do not use borrowed references APIs - #43540

Merged
pitrou merged 7 commits into
apache:mainfrom
lysnikolaou:remove-borrowed-references
Aug 15, 2024
Merged

GH-43536: [Python] Do not use borrowed references APIs#43540
pitrou merged 7 commits into
apache:mainfrom
lysnikolaou:remove-borrowed-references

Conversation

@lysnikolaou

@lysnikolaoulysnikolaou commented Aug 2, 2024

Copy link
Copy Markdown
Contributor

Rationale for this change

For better reference safety under Python free-threaded builds (i.e. with the GIL removed), we should be using Py(List|Dict)_GetItemRef that return strong references and are implemented in a thread-safe manner.

What changes are included in this PR?

Are these changes tested?

I ran the tests with the free-threaded build before and after, and there's the same expected failures.

@github-actions

Copy link
Copy Markdown

⚠️ GitHub issue #43536has been automatically assigned in GitHub to PR creator.

@jorisvandenbossche

jorisvandenbossche commented Aug 5, 2024

Copy link
Copy Markdown
Member

General question: are the #ifdef / #else needed, or could we also simply always use the variants that return strong references, also when free-threading is not enabled? Or has that a performance cost?

(or if the answer is that those APIs are not supported for older versions of CPython, adopting pythoncapi-compat might help? #43069)

@lysnikolaou

Copy link
Copy Markdown
ContributorAuthor

We could use the strong reference APIs in the non-free-threaded build as well.

Adopting pythoncapi-compat would certainly help. Glad that there's an open issue about that already. I can work on a PR that vendors the compat header.

@pitrou

Copy link
Copy Markdown
Member

These changes add a lot of conditional code. Also, sometimes they are not necessary, if we are sure the container (list/dict) will not be mutated in another thread.

Comment threadpython/pyarrow/src/arrow/python/benchmark.cc Outdated
Comment threadpython/pyarrow/src/arrow/python/deserialize.cc Outdated
Comment threadpython/pyarrow/src/arrow/python/numpy_to_arrow.cc Outdated
Comment threadpython/pyarrow/src/arrow/python/python_to_arrow.cc Outdated
Comment threadpython/pyarrow/src/arrow/python/python_to_arrow.cc Outdated
Comment threadpython/pyarrow/src/arrow/python/python_to_arrow.cc Outdated
Comment threadpython/pyarrow/src/arrow/python/python_to_arrow.cc Outdated
@github-actionsgithub-actionsBot added awaiting committer review Awaiting committer review and removed awaiting review Awaiting review labels Aug 6, 2024
@lysnikolaou

Copy link
Copy Markdown
ContributorAuthor

Thanks @pitrou for the review! I updated the PR to add pythoncapi_compat.h and use the strong reference APIs from there.

@github-actionsgithub-actionsBot added awaiting changes Awaiting changes and removed awaiting committer review Awaiting committer review labels Aug 6, 2024
Comment threadpython/pyarrow/src/arrow/python/platform.h Outdated
Comment threadpython/pyarrow/src/arrow/python/pythoncapi_compat.h Outdated
@github-actionsgithub-actionsBot added awaiting change review Awaiting change review and removed awaiting changes Awaiting changes labels Aug 6, 2024
Comment threadpython/pyarrow/src/arrow/python/platform.h Outdated
Comment threadpython/pyarrow/src/arrow/python/vendored/pythoncapi_compat.h Outdated
@lysnikolaou

Copy link
Copy Markdown
ContributorAuthor

This is ready for another review.

@pitroupitrou changed the title GH-43536: [Python] Do not use borrowed references APIs under free-threaded CPythonGH-43536: [Python] Do not use borrowed references APIsAug 15, 2024

@pitroupitrou left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM, thanks a lot @lysnikolaou . I will rebase and try to run more CI.

@pitrou
pitrouforce-pushed the remove-borrowed-references branch from 174a1ef to 202de1aCompareAugust 15, 2024 10:03
@pitrou

Copy link
Copy Markdown
Member

@github-actions crossbow submit -g python -g wheel

@github-actions

Copy link
Copy Markdown

Revision: 202de1a

Submitted crossbow builds: ursacomputing/crossbow @ actions-ceaf2f2afb

TaskStatus
example-python-minimal-build-fedora-condaGitHub Actions
example-python-minimal-build-ubuntu-venvGitHub Actions
test-conda-python-3.10GitHub Actions
test-conda-python-3.10-cython2GitHub Actions
test-conda-python-3.10-hdfs-2.9.2GitHub Actions
test-conda-python-3.10-hdfs-3.2.1GitHub Actions
test-conda-python-3.10-pandas-latest-numpy-1.26GitHub Actions
test-conda-python-3.10-pandas-latest-numpy-latestGitHub Actions
test-conda-python-3.10-pandas-nightly-numpy-nightlyGitHub Actions
test-conda-python-3.10-substraitGitHub Actions
test-conda-python-3.11GitHub Actions
test-conda-python-3.11-dask-latestGitHub Actions
test-conda-python-3.11-dask-upstream_develGitHub Actions
test-conda-python-3.11-hypothesisGitHub Actions
test-conda-python-3.11-pandas-upstream_devel-numpy-nightlyGitHub Actions
test-conda-python-3.11-spark-masterGitHub Actions
test-conda-python-3.12GitHub Actions
test-conda-python-3.12-cpython-debugGitHub Actions
test-conda-python-3.8GitHub Actions
test-conda-python-3.8-pandas-1.0-numpy-1.19GitHub Actions
test-conda-python-3.9GitHub Actions
test-conda-python-3.9-pandas-latest-numpy-latestGitHub Actions
test-conda-python-emscriptenGitHub Actions
test-cuda-pythonGitHub Actions
test-debian-12-python-3-amd64GitHub Actions
test-debian-12-python-3-i386GitHub Actions
test-fedora-39-python-3GitHub Actions
test-ubuntu-20.04-python-3GitHub Actions
test-ubuntu-22.04-python-3GitHub Actions
wheel-macos-big-sur-cp310-arm64GitHub Actions
wheel-macos-big-sur-cp311-arm64GitHub Actions
wheel-macos-big-sur-cp312-arm64GitHub Actions
wheel-macos-big-sur-cp38-arm64GitHub Actions
wheel-macos-big-sur-cp39-arm64GitHub Actions
wheel-macos-catalina-cp310-amd64GitHub Actions
wheel-macos-catalina-cp311-amd64GitHub Actions
wheel-macos-catalina-cp312-amd64GitHub Actions
wheel-macos-catalina-cp38-amd64GitHub Actions
wheel-macos-catalina-cp39-amd64GitHub Actions
wheel-manylinux-2-28-cp310-amd64GitHub Actions
wheel-manylinux-2-28-cp310-arm64GitHub Actions
wheel-manylinux-2-28-cp311-amd64GitHub Actions
wheel-manylinux-2-28-cp311-arm64GitHub Actions
wheel-manylinux-2-28-cp312-amd64GitHub Actions
wheel-manylinux-2-28-cp312-arm64GitHub Actions
wheel-manylinux-2-28-cp38-amd64GitHub Actions
wheel-manylinux-2-28-cp38-arm64GitHub Actions
wheel-manylinux-2-28-cp39-amd64GitHub Actions
wheel-manylinux-2-28-cp39-arm64GitHub Actions
wheel-manylinux-2014-cp310-amd64GitHub Actions
wheel-manylinux-2014-cp310-arm64GitHub Actions
wheel-manylinux-2014-cp311-amd64GitHub Actions
wheel-manylinux-2014-cp311-arm64GitHub Actions
wheel-manylinux-2014-cp312-amd64GitHub Actions
wheel-manylinux-2014-cp312-arm64GitHub Actions
wheel-manylinux-2014-cp38-amd64GitHub Actions
wheel-manylinux-2014-cp38-arm64GitHub Actions
wheel-manylinux-2014-cp39-amd64GitHub Actions
wheel-manylinux-2014-cp39-arm64GitHub Actions
wheel-windows-cp310-amd64GitHub Actions
wheel-windows-cp311-amd64GitHub Actions
wheel-windows-cp312-amd64GitHub Actions
wheel-windows-cp38-amd64GitHub Actions
wheel-windows-cp39-amd64GitHub Actions

@pitrou

Copy link
Copy Markdown
Member

CI failures look unrelated, I'll merge.

@pitrou
pitrou merged commit 894f72f into apache:mainAug 15, 2024
@pitroupitrou removed the awaiting change review Awaiting change review label Aug 15, 2024
@conbench-apache-arrow

Copy link
Copy Markdown

After merging your PR, Conbench analyzed the 7 benchmarking runs that have been run so far on merge-commit 894f72f.

There were no benchmark performance regressions. 🎉

The full Conbench report has more details. It also includes information about 6 possible false positives for unstable benchmarks that are known to sometimes produce them.

jorisvandenbossche added a commit that referenced this pull request Aug 21, 2024
### Rationale for this change
#43540 already vendored `pythoncapi_compat.h`, so closing #43069 by using this as well for `Py_IsFinalizing` (which was added in #42034, and for which we opened that follow-up issue to use `pythoncapi_compat.h` instead)
Authored-by: Joris Van den Bossche <jorisvandenbossche@gmail.com>
Signed-off-by: Joris Van den Bossche <jorisvandenbossche@gmail.com>
QuietCraftsmanship pushed a commit to QuietCraftsmanship/arrow that referenced this pull request Jul 7, 2025
### Rationale for this change
apache/arrow#43540 already vendored `pythoncapi_compat.h`, so closing apache/arrow#43069 by using this as well for `Py_IsFinalizing` (which was added in apache/arrow#42034, and for which we opened that follow-up issue to use `pythoncapi_compat.h` instead)
Authored-by: Joris Van den Bossche <jorisvandenbossche@gmail.com>
Signed-off-by: Joris Van den Bossche <jorisvandenbossche@gmail.com>
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@lysnikolaou@jorisvandenbossche@pitrou