py-spy icon indicating copy to clipboard operation
py-spy copied to clipboard

Doesn't work on OSX

Open barskykd opened this issue 4 years ago • 19 comments

Started python interpreter

>>> import sys
>>> sys.version
'3.7.7 (v3.7.7:d7c567b08f, Mar 10 2020, 02:56:16) \n[Clang 6.0 (clang-600.0.57)]'
>>> sys.executable
'/Library/Frameworks/Python.framework/Versions/3.7/bin/python3'
>>> import os
>>> os.getpid()
62993

Trying to attach profiler

Mac-mini:~ konstantin$ sudo py-spy dump --pid 62993
Error: Failed to open process - check if it is running.
Reason: Undefined error: 0 (os error 0)

Mac-mini:~ konstantin$ sudo py-spy record -o test.svg --pid 62993
Error: Failed to open process - check if it is running.
Reason: Operation timed out (os error 60)

Mac-mini:~ konstantin$ py-spy -V
py-spy 0.3.3

OSX version: 10.15.3 (19D76)

barskykd avatar May 27 '20 08:05 barskykd

I'm also seeing this error on OSX 10.13.6; oddly, it works fine if I use either python3 or python2 installed by nix, but fails on a python2 installed in a venv. No idea what the difference is between those two, but maybe that data point could be helpful for debugging.

(Yes, I know python2 is unmaintained; we've got a project we'll be replacing Real Soon Now that we couldn't upgrade to python3 due to external libraries.)

edanaher avatar May 27 '20 14:05 edanaher

@barskykd Are you running the python installed by OSX? I'm wondering if this issue is related to System Integrity Protection (https://github.com/benfred/py-spy/#why-am-i-having-issues-profiling-usrbinpython-on-osx).

Fwiw, when I try the same on my OSX system it works - but I have disabled SIP

%  /Library/Frameworks/Python.framework/Versions/3.8/bin/python3  
Python 3.8.0 (v3.8.0:fa919fdf25, Oct 14 2019, 10:23:27) 
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.getpid()
25584
>>> 
% sudo py-spy dump -p 25584

Process 25584: /Library/Frameworks/Python.framework/Versions/3.8/Resources/Python.app/Contents/MacOS/Python
Python v3.8.0 (/Library/Frameworks/Python.framework/Versions/3.8/Resources/Python.app/Contents/MacOS/Python)

Thread 0x1130A0DC0 (idle)
...

@edanaher Is this the exact same error? Is the python2 installed in a venv from /usr/bin/python? Last time I tried to the OSX system python2 version in a venv it worked for me , even with SIP enabled

benfred avatar May 27 '20 15:05 benfred

I didn't read quite carefully enough; I'm just getting:

$ sudo py-spy record -o test.svg -p 16261
Error: Operation timed out (os error 60)

No Error: Failed to open process - check if it is running. And the timeout is pretty short; time sudo py-spy... shows it taking 0.6 seconds real time to error out.

i don't think the venv is using OS X's python; at least, venv/bin/python and /usr/bin/python have different filesizes and both appear to be binaries, so it's unlikely that one is calling the other.

I don't want to dig too deep into this, since I can work around this by using a slightly less convenient Linux instance, but can do some light digging if that would help.

edanaher avatar May 27 '20 16:05 edanaher

@benfred It is python downloaded from python.org Installed into /Library/Frameworks/Python.framework/Versions/3.7/bin/python3

Tried inside venv - got same errors

barskykd avatar May 28 '20 07:05 barskykd

Just hit this myself. I've used Pyspy successfully previously so this is definitely something new. Pyspy has not updated but Mac OS has. Something in the last 3 months. Not sure what else is relevant here. MacOS - 10.15.5

alextriaca avatar Jun 24 '20 10:06 alextriaca

I think this might be a change with SIP (system integrity protection) on the latest version of OSX - where SIP prevents us from attaching to programs in /Library/Frameworks/ as well as in /usr/bin (https://github.com/benfred/py-spy#why-am-i-having-issues-profiling-usrbinpython-on-osx) . If so this might not be something we can fix - the only choices will be to use a different python (anaconda installs to a user directory instead of a system directory) or disable SIP

benfred avatar Jun 25 '20 00:06 benfred

Hello !

I am using a python that is neither in /Library/Frameworks nor in /usr/bin, and I am still getting this error :

$ ps aux | grep droop.py
olojkine          3934   2.7  0.3  4370532  44564   ??  S     7:05PM   0:01.17 /Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.7/Resources/Python.app/Contents/MacOS/Python /Users/olojkine/Developpement/experimental/droop_controller/droop.py
olojkine          3942   0.0  0.0  4278564    744 s004  S+    7:06PM   0:00.00 grep droop.py
(venv) (19:06:12) ~/Developpement/experimental/droop_controller
$ sudo py-spy record -o droop-profile.svg --pid 3934
Error: Failed to open process - check if it is running.
Reason: Operation timed out (os error 60)

lovasoa avatar Sep 17 '20 17:09 lovasoa

I am seeing the same problem here:

sudo py-spy top -- python3
Password:

Error: Failed to open process - check if it is running.
Reason: Operation timed out (os error 60)

My python is located at /Users/$user/.pyenv/versions/3.8.2/bin

chbndrhnns avatar Nov 05 '20 07:11 chbndrhnns

I face the same issue using pyenv installed on a user directory. Any ideas on what the issue might be @benfred

guidj avatar Nov 27 '20 11:11 guidj

I'm having the same issue but noticed it was working fine with python3.6 and not with python3.8... Hopefully this can be of any help solving this issue

$ ls -la /usr/local/bin/python3.6
lrwxr-xr-x  1 root  wheel  71 10 Jul 13:59 /usr/local/bin/python3.6 -> ../../../Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6

$ ls -la /usr/local/bin/python3.8
lrwxr-xr-x  1 root  wheel  71 22 Oct 15:40 /usr/local/bin/python3.8 -> ../../../Library/Frameworks/Python.framework/Versions/3.8/bin/python3.8

$ sudo ./py-spy record --output test.svg -- python3.6
Python 3.6.8 (v3.6.8:3c6b436a57, Dec 24 2018, 02:04:31)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> py-spy> Sampling process 100 times a second. Press Control-C to exit.
KeyboardInterrupt
>>>
py-spy> Stopped sampling because Control-C pressed
[2020-12-14T20:23:54.707783000Z ERROR inferno::flamegraph] No stack counts found
Error: Failed to write flamegraph: I/O error: No stack counts found

$ sudo ./py-spy record --output test.svg -- python3.8
Python 3.8.6 (v3.8.6:db455296be, Sep 23 2020, 13:31:39)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> Error: Failed to open process - check if it is running.
Reason: Operation timed out (os error 60)

$

lpouvreau avatar Dec 14 '20 20:12 lpouvreau

With SIP disabled it definitely works on macOS 11 (using the Python 3.9 framework build from python.org).

Also @benfred, thank you for a great project! From all the profilers which I have tried py-spy works the best with multithreaded applications. And it really runs with very little overhead.

samschott avatar Mar 20 '21 14:03 samschott

Experienced with macOS Big Sur 11.2.3, disabling SIP and using a virtualenv did not resolve the issue.

Workaround found is to dockerize the python app to bypass macOS restrictions. This blog post proposes a nice walkthrough of a minimum reproducible example of using py-spy through docker / docker-compose to monitor a python app.

fpaupier avatar Jun 11 '21 06:06 fpaupier

I got this today. One process works fine with py-spy, another not. The one I couldn't look at was python 3.9.6, and then I tried with a 3.11 and that worked. Both installed via homebrew.

boxed avatar Mar 14 '23 08:03 boxed

My workaround:

  1. Bash into the container
  2. Install py-spy inside the container
  3. top to get the PID of desired process
  4. py-spy record -o bla.svg --pid <pid> you know the drill
  5. Copy image from inside of the container to your local machine docker cp <container_id>:/path/bla.svg .
  6. Voila

My setup:

  • OSX 13.1
  • docker-compose
  • Python 3.11
  • FastAPI

go2nirvana avatar Oct 11 '23 08:10 go2nirvana

the same in virtualenv

❯ ps -ef|grep python
  502 64885 27141   0 11:23AM ttys006    0:02.90 .venv/bin/python main.py
  502 64890 64885   0 11:23AM ttys006    0:00.02 .venv/bin/python main.py
  502 64891 64885   0 11:23AM ttys006    0:00.02 .venv/bin/python main.py
  502 64892 64885   0 11:23AM ttys006    0:00.02 .venv/bin/python main.py
  502 64893 64892   0 11:23AM ttys006    0:00.04 /Users/xiaowangzi/Documents/RED/Program/change/.venv/bin/python -c from multiprocessing.resource_tracker import main;main(12)
  502 64894 64892   0 11:23AM ttys006    0:01.76 /Users/xiaowangzi/Documents/RED/Program/change/.venv/bin/python -c from multiprocessing.spawn import spawn_main; spawn_main(tracker_fd=13, pipe_handle=15) --multiprocessing-fork
  502 64895 64892   0 11:23AM ttys006    0:01.84 /Users/xiaowangzi/Documents/RED/Program/change/.venv/bin/python -c from multiprocessing.spawn import spawn_main; spawn_main(tracker_fd=13, pipe_handle=17) --multiprocessing-fork
  502 64896 64892   0 11:23AM ttys006    0:01.82 /Users/xiaowangzi/Documents/RED/Program/change/.venv/bin/python -c from multiprocessing.spawn import spawn_main; spawn_main(tracker_fd=13, pipe_handle=19) --multiprocessing-fork
  502 64897 64892   0 11:23AM ttys006    0:01.90 /Users/xiaowangzi/Documents/RED/Program/change/.venv/bin/python -c from multiprocessing.spawn import spawn_main; spawn_main(tracker_fd=13, pipe_handle=21) --multiprocessing-fork
  502 65031 64923   0 11:23AM ttys009    0:00.00 grep python
❯ sudo py-spy top --pid 64885
Password:
Error: Failed to open process - check if it is running.
Reason: Operation timed out (os error 60)
Reason: Operation timed out (os error 60)

My environment

  • osx 13.5, m1
  • python 3.9.6

beyondskyway avatar Jan 12 '24 03:01 beyondskyway

same here, the command actually starts running, but then stops abruptly.

macOS 14.4 py-spy 0.3.14

➜  ~ which python3
/usr/bin/python3
➜  ~ python3
Python 3.9.6 (default, Feb  3 2024, 15:58:27) 
[Clang 15.0.0 (clang-1500.3.9.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
➜  plotter git:(master) ✗ sudo py-spy record --format speedscope -- python3 plotter.py   
Executing command PathCommand(cmd='M', coords=array([[63.960938, 60.984375]]))
...snip...
Executing command PathCommand(cmd='C', coords=array([[101.14472 ,  78.990208],
       [101.38926 ,  78.671504],
       [101.40847 ,  78.453975]]))
Error: Failed to open process - check if it is running.
Reason: Operation timed out (os error 60)
Reason: Operation timed out (os error 60)

whateverforever avatar Mar 13 '24 19:03 whateverforever