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

psutil fails (segfault) on Mac OS in the free-threaded (no GIL) version of Python 3.13 #2427

Closed
pkoning2 opened this issue Jun 19, 2024 · 8 comments · Fixed by #2428
Closed

Comments

@pkoning2
Copy link

Summary

  • Mac OS 12.7.5
  • Architecture: 64bit ARM (Apple M1)
  • Psutil version: 6.0.0
  • Python version: Python 3.13.0b2
  • Type: fails to import

Description

Transfered here from python/cpython#120717

I used pip to install psutil on python 3.13 0b2, both the regular and the "t" version. The regular one works; the t one fails at import psutil with a segfault.

The Python teams comments: "Any extensions that uses more than Python would need to adapt to free-threading if they want to onboard. It's impossible for CPython to ensure all the C extensions work without GIL. So this should be an issue for psutil ."

@giampaolo
Copy link
Owner

I don't know enough about free-thread cPython to give any guidance. Some work in this sense was done in #2401. @colesbury any idea?

@KRRT7
Copy link

KRRT7 commented Jun 23, 2024

happens on windows as well.

@giampaolo
Copy link
Owner

giampaolo commented Jun 23, 2024

Can you guys run:

python3 -c "import faulthandler; faulthandler.enable(); import psutil"

...and paste the output?

@KRRT7
Copy link

KRRT7 commented Jun 23, 2024

Administrator in ~\Documents\tests via 🐍 v3.13.0b1 (313t-venv)
❯ python -c "import faulthandler; faulthandler.enable(); import psutil"
Windows fatal exception: access violation

Current thread 0x00002388 (most recent call first):
  File "<frozen importlib._bootstrap>", line 488 in _call_with_frames_removed
  File "<frozen importlib._bootstrap_external>", line 1315 in create_module
  File "<frozen importlib._bootstrap>", line 813 in module_from_spec
  File "<frozen importlib._bootstrap>", line 921 in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1331 in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1360 in _find_and_load
  File "C:\Users\NuitkaDevOps\Documents\cmbos\313t-venv\Lib\site-packages\psutil\_pswindows.py", line 35 in <module>
  File "<frozen importlib._bootstrap>", line 488 in _call_with_frames_removed
  File "<frozen importlib._bootstrap_external>", line 1021 in exec_module
  File "<frozen importlib._bootstrap>", line 935 in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1331 in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1360 in _find_and_load
  File "<frozen importlib._bootstrap>", line 488 in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1415 in _handle_fromlist
  File "C:\Users\NuitkaDevOps\Documents\cmbos\313t-venv\Lib\site-packages\psutil\__init__.py", line 110 in <module>
  File "<frozen importlib._bootstrap>", line 488 in _call_with_frames_removed
  File "<frozen importlib._bootstrap_external>", line 1021 in exec_module
  File "<frozen importlib._bootstrap>", line 935 in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1331 in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1360 in _find_and_load
  File "<string>", line 1 in <module>

@pkoning2
Copy link
Author

I get this, on Mac OS:

pkoning:foo pkoning$ PYTHONFAULTHANDLER=1 python3.13t -c "import psutil"
Fatal Python error: Segmentation fault

Current thread 0x0000000104750580 (most recent call first):
  File "<frozen importlib._bootstrap>", line 488 in _call_with_frames_removed
  File "<frozen importlib._bootstrap_external>", line 1316 in create_module
  File "<frozen importlib._bootstrap>", line 813 in module_from_spec
  File "<frozen importlib._bootstrap>", line 921 in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1331 in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1360 in _find_and_load
  File "<frozen importlib._bootstrap>", line 488 in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1415 in _handle_fromlist
  File "/Library/Frameworks/PythonT.framework/Versions/3.13/lib/python3.13/site-packages/psutil/_psposix.py", line 28 in <module>
  File "<frozen importlib._bootstrap>", line 488 in _call_with_frames_removed
  File "<frozen importlib._bootstrap_external>", line 1022 in exec_module
  File "<frozen importlib._bootstrap>", line 935 in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1331 in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1360 in _find_and_load
  File "<frozen importlib._bootstrap>", line 488 in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1415 in _handle_fromlist
  File "/Library/Frameworks/PythonT.framework/Versions/3.13/lib/python3.13/site-packages/psutil/_psosx.py", line 13 in <module>
  File "<frozen importlib._bootstrap>", line 488 in _call_with_frames_removed
  File "<frozen importlib._bootstrap_external>", line 1022 in exec_module
  File "<frozen importlib._bootstrap>", line 935 in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1331 in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1360 in _find_and_load
  File "<frozen importlib._bootstrap>", line 488 in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1415 in _handle_fromlist
  File "/Library/Frameworks/PythonT.framework/Versions/3.13/lib/python3.13/site-packages/psutil/__init__.py", line 124 in <module>
  File "<frozen importlib._bootstrap>", line 488 in _call_with_frames_removed
  File "<frozen importlib._bootstrap_external>", line 1022 in exec_module
  File "<frozen importlib._bootstrap>", line 935 in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1331 in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1360 in _find_and_load
  File "<string>", line 1 in <module>
Segmentation fault: 11

This is with the published release (from pip install). Should we try it with the current latest code from github?

@colesbury
Copy link
Contributor

Sorry for the ping ponging of issues between projects. I think this is mostly not specific to psutil, but I'm not entirely sure yet.

I'd like to focus on macOS first, because I think it'll be easier to get it into a good state.

@pkoning2, how did you get Python, pip, and psutil installed?

Here are some issues I noticed on macOS:

  • macOS installer appears to be missing pip for 3.13t.
  • python3.13t -m ensurepip installs 24.0, which is not compatible. You need 24.1. Unfortunately, I think this issue will persist in beta 3 and won't be fixed until beta 4.

Here's what I did to successfully install psutil:

  • Install Python 3.13 with free-threading binaries
  • python3.13t -m ensurepip && python3.13t -m pip install --upgrade pip
  • python3.13t -m pip install psutil

There's still the issue that importing psutil enables the GIL:

<frozen importlib._bootstrap>:488: RuntimeWarning: The global interpreter lock (GIL) has been enabled to load module 'psutil._psutil_osx', which has not declared that it can run safely without the GIL. To override this behavior and keep the GIL disabled (at your own risk), run with PYTHON_GIL=0 or -Xgil=0.

That can be fixed in this project, but the other issues are more CPython related.

@pkoning2
Copy link
Author

Yes, what I did roughly follows the steps you describe. I didn't realize that pip was out of date and this is the first time I've run into a situation where it actually matters.

So yes, now you have the GIL lock complaint. I ran python3.13t with that flag to suppress the warning, and psutil seems to work fine. At least for net_if_addrs() which is the only function I have used so far. So perhaps all that is needed is to supply the flag that says to Python that you're compatible with the free-threading flavor?

I noticed the help mentions net_connections(); that fails on 3.13 (either flavor) with an "operation not permitted" exception, but it seems to work on 3.12.

@colesbury
Copy link
Contributor

I'll work on a patch to suppress the GIL warnings and behavior in the free-threaded build, as well as a few small thread-safety issues on some OS's if certain psutil functions are used from multiple threads concurrently.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants