Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 7 commits into from
Aug 15, 2024

Conversation

lysnikolaou
Copy link
Contributor

@lysnikolaou lysnikolaou commented Aug 2, 2024

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.

Copy link

github-actions bot commented Aug 2, 2024

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

@jorisvandenbossche
Copy link
Member

jorisvandenbossche commented Aug 5, 2024

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
Contributor Author

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
Member

pitrou commented Aug 6, 2024

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.

python/pyarrow/src/arrow/python/benchmark.cc Outdated Show resolved Hide resolved
python/pyarrow/src/arrow/python/deserialize.cc Outdated Show resolved Hide resolved
python/pyarrow/src/arrow/python/numpy_to_arrow.cc Outdated Show resolved Hide resolved
python/pyarrow/src/arrow/python/python_to_arrow.cc Outdated Show resolved Hide resolved
python/pyarrow/src/arrow/python/python_to_arrow.cc Outdated Show resolved Hide resolved
python/pyarrow/src/arrow/python/python_to_arrow.cc Outdated Show resolved Hide resolved
@github-actions github-actions bot added awaiting committer review Awaiting committer review and removed awaiting review Awaiting review labels Aug 6, 2024
@lysnikolaou
Copy link
Contributor Author

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

@github-actions github-actions bot added awaiting changes Awaiting changes and removed awaiting committer review Awaiting committer review labels Aug 6, 2024
@github-actions github-actions bot added awaiting change review Awaiting change review and removed awaiting changes Awaiting changes labels Aug 6, 2024
@lysnikolaou
Copy link
Contributor Author

This is ready for another review.

@pitrou pitrou changed the title GH-43536: [Python] Do not use borrowed references APIs under free-threaded CPython GH-43536: [Python] Do not use borrowed references APIs Aug 15, 2024
Copy link
Member

@pitrou pitrou left a comment

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
Copy link
Member

pitrou commented Aug 15, 2024

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

Copy link

Revision: 202de1a

Submitted crossbow builds: ursacomputing/crossbow @ actions-ceaf2f2afb

Task Status
example-python-minimal-build-fedora-conda GitHub Actions
example-python-minimal-build-ubuntu-venv GitHub Actions
test-conda-python-3.10 GitHub Actions
test-conda-python-3.10-cython2 GitHub Actions
test-conda-python-3.10-hdfs-2.9.2 GitHub Actions
test-conda-python-3.10-hdfs-3.2.1 GitHub Actions
test-conda-python-3.10-pandas-latest-numpy-1.26 GitHub Actions
test-conda-python-3.10-pandas-latest-numpy-latest GitHub Actions
test-conda-python-3.10-pandas-nightly-numpy-nightly GitHub Actions
test-conda-python-3.10-substrait GitHub Actions
test-conda-python-3.11 GitHub Actions
test-conda-python-3.11-dask-latest GitHub Actions
test-conda-python-3.11-dask-upstream_devel GitHub Actions
test-conda-python-3.11-hypothesis GitHub Actions
test-conda-python-3.11-pandas-upstream_devel-numpy-nightly GitHub Actions
test-conda-python-3.11-spark-master GitHub Actions
test-conda-python-3.12 GitHub Actions
test-conda-python-3.12-cpython-debug GitHub Actions
test-conda-python-3.8 GitHub Actions
test-conda-python-3.8-pandas-1.0-numpy-1.19 GitHub Actions
test-conda-python-3.9 GitHub Actions
test-conda-python-3.9-pandas-latest-numpy-latest GitHub Actions
test-conda-python-emscripten GitHub Actions
test-cuda-python GitHub Actions
test-debian-12-python-3-amd64 GitHub Actions
test-debian-12-python-3-i386 GitHub Actions
test-fedora-39-python-3 GitHub Actions
test-ubuntu-20.04-python-3 GitHub Actions
test-ubuntu-22.04-python-3 GitHub Actions
wheel-macos-big-sur-cp310-arm64 GitHub Actions
wheel-macos-big-sur-cp311-arm64 GitHub Actions
wheel-macos-big-sur-cp312-arm64 GitHub Actions
wheel-macos-big-sur-cp38-arm64 GitHub Actions
wheel-macos-big-sur-cp39-arm64 GitHub Actions
wheel-macos-catalina-cp310-amd64 GitHub Actions
wheel-macos-catalina-cp311-amd64 GitHub Actions
wheel-macos-catalina-cp312-amd64 GitHub Actions
wheel-macos-catalina-cp38-amd64 GitHub Actions
wheel-macos-catalina-cp39-amd64 GitHub Actions
wheel-manylinux-2-28-cp310-amd64 GitHub Actions
wheel-manylinux-2-28-cp310-arm64 GitHub Actions
wheel-manylinux-2-28-cp311-amd64 GitHub Actions
wheel-manylinux-2-28-cp311-arm64 GitHub Actions
wheel-manylinux-2-28-cp312-amd64 GitHub Actions
wheel-manylinux-2-28-cp312-arm64 GitHub Actions
wheel-manylinux-2-28-cp38-amd64 GitHub Actions
wheel-manylinux-2-28-cp38-arm64 GitHub Actions
wheel-manylinux-2-28-cp39-amd64 GitHub Actions
wheel-manylinux-2-28-cp39-arm64 GitHub Actions
wheel-manylinux-2014-cp310-amd64 GitHub Actions
wheel-manylinux-2014-cp310-arm64 GitHub Actions
wheel-manylinux-2014-cp311-amd64 GitHub Actions
wheel-manylinux-2014-cp311-arm64 GitHub Actions
wheel-manylinux-2014-cp312-amd64 GitHub Actions
wheel-manylinux-2014-cp312-arm64 GitHub Actions
wheel-manylinux-2014-cp38-amd64 GitHub Actions
wheel-manylinux-2014-cp38-arm64 GitHub Actions
wheel-manylinux-2014-cp39-amd64 GitHub Actions
wheel-manylinux-2014-cp39-arm64 GitHub Actions
wheel-windows-cp310-amd64 GitHub Actions
wheel-windows-cp311-amd64 GitHub Actions
wheel-windows-cp312-amd64 GitHub Actions
wheel-windows-cp38-amd64 GitHub Actions
wheel-windows-cp39-amd64 GitHub Actions

@pitrou
Copy link
Member

pitrou commented Aug 15, 2024

CI failures look unrelated, I'll merge.

@pitrou pitrou merged commit 894f72f into apache:main Aug 15, 2024
11 checks passed
@pitrou pitrou removed the awaiting change review Awaiting change review label Aug 15, 2024
Copy link

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 <[email protected]>
Signed-off-by: Joris Van den Bossche <[email protected]>
Sign up for free to 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