py-spy: Doesn't work on OSX

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)

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Reactions: 7
  • Comments: 16 (2 by maintainers)

Most upvoted comments

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

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

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.

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)

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.

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)

$