-
Notifications
You must be signed in to change notification settings - Fork 447
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
"Killed: 9"? #48
Comments
So - it sort of looks like something killed py-spy =(by going 'kill -9' or similar) =(. Can you figure out what process issued the SIGKILL? There are probably a bunch of ways of getting this, but an easy first stab might be to use dtrace. Running these commands: curl http://www.brendangregg.com/DTrace/kill.d > kill.d
sudo /usr/sbin/dtrace -qs kill.d will print out a series of lines with the sending process info and what signal is being sent every time a signal happens on your machine. If you filter down to '9' signals sent at the same time py-spy is killed, you should get the process that is killing it. |
Good idea. I found that each attempt to run
sudo is killing itself. I added some debug logging to sudo via /etc/sudo.conf and obtained these logs: gist. I'm confident that the security policy in effect is permitting the command to go through:
cross-referencing w/
from man sudo_plugin. Also note that in the ultra debug mode logs its attempting to exec the py-spy command. I suspect what's most relevant is
in part because that's the single line that gets logged if I decrease the log level to Probably worth mentioning at this point, too:
|
For reference, here's the line of code that's coming from. |
More data, demoing the 'direct invocation' style + showing I'm not trying to use /usr/bin/python:
|
Ah, looks like the process gets created, but as a child of /sbin/launchd:
|
Hmm, weird. Instead of launching py-spy like Can you also enable logging in py-spy? ( I think the error message from using the system python would be different. (also the system python on high sierra is 2.7.10 instead). |
Strangely, Brendan Gregg's dtrace script isn't listing any SIGKILLs, unlike before. When I run the looping script w/o py-spy and then manually kill it, the signal is observed by dtrace:
I'm starting to wonder if system integrity protection might be involved. I attempted to trace system calls to see if I could find what was sending or printing the "Killed: 9", but no luck yet
|
hm, I have another machine, macOS 10.13.4 w/ SIP enabled, and |
I don't think this problem is related to SIP - iirc the error messages when hitting SIP are a permissions denied error (also it's not getting that far =). Based off the logs, py-spy is getting killed when reading the virtual memory maps - which really only calls task_for_pid followed by a couple of mach_vm_region calls: https://github.com/rbspy/proc-maps/blob/96b274b826d7f9246856986af632b33672e9a7d9/src/mac_maps/mod.rs#L93-L95 You should be able to see these calls running with something like sudo dtrace -n 'pid$target::mach_vm_region:entry' \
-n 'pid$target::task_for_pid:entry' \
-c 'py-spy -f o.svg -- python foo.py' which should list one task_for_pid call followed by a bunch of mach_vm_region (to get the memory maps). if this works there will also be an extra task_for_pid call at the end (to get the handle to read the process memory). I'm really not sure why this would cause a sigkill though =( Do you have any antivirus software running? Or is there any other major differences in the environment between your machines? |
hmm - actually you should be able to use dtruss, and if you can't you probably can't use dtrace either 🤦♂️ . Can you use dtruss/dtrace on your python (without py-spy) ? like does I'll try out re-enabling SIP tomorrow to test out. |
I identified the issue with dtruss:
The output of the command you suggested above:
|
Good idea. I upgraded the other to the same version of macOS and ensured same version of Python and py-spy, and still the one works, one doesn't. There is an antivirus running, Cb Defense. Unfortunately, I can't turn it off without a trip over to our IT team (and even no guarantees then ;) ). I'll pitch it to them to try to test that hypothesis, though. I don't know anything about how antivirus software works, but that's kinda freaky if it can kill things without being observed by dtrace. |
hmm , it looks like it's getting killed in task_for_pid then? I guess we'd have to also log the task_for_pid return too to be sure (instead of just the entry)
which should dump out something like
|
Reran it multiple times and the output seemed consistently that ^ |
I think I've seen this problem before 🤔 |
@crepererum I think that htop problem might be the same as this : https://jvns.ca/blog/2018/01/28/mac-freeze/ - which we should handle (with the sleep call here: https://github.com/rbspy/proc-maps/blob/96b274b826d7f9246856986af632b33672e9a7d9/src/mac_maps/mod.rs#L144 and I think apple has fixed the kernel bug in the macos version he's using). The htop issue is mentioned on this twitter thread about that post anyways: https://twitter.com/b0rk/status/957498366606368768 @spencerwilson-optimizely I think this program should reproduce the problem (without all the other rust/py-spy code getting in the way and confusing the issue): // file: task_for_pid_test.cc
#include <mach/mach_init.h>
#include <mach/port.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, const char * argv[]) {
if (argc <= 1) {
printf("usage %s <pid>\n", argv[0]);
return 1;
}
int pid = atoi(argv[1]);
mach_port_name_t task = MACH_PORT_NULL;
kern_return_t kr = task_for_pid(mach_task_self(), pid, &task);
if (kr != KERN_SUCCESS) {
printf("failed to call task_for_pid on pid %i: %x\n", pid, kr);
return kr;
}
printf("success!\n");
return 0;
} compile with |
@benfred : In one shell I kicked off $ ./task_for_pid_test 30533
Killed: 9 Really nice work! You called it 🔮 Again, nothing appears in |
It's great that we figured out where it's happening - but I'm still not sure of why =(. It might be worth checking to see if you can disable your antivirus and try it out. I'm leaning towards it being a 3rd party program killing the process rather than something with os restrictions like SIP or taskgated (because I'd expect the task_for_pid call to fail rather than cause the process to get killed if this call is restricted by the OS). It still might be worth turning off SIP and verifying though. Is there anything in the system log when this happens ? (Like open Console.app -> and checking system.log or /var/log). Or is there anything in the antivirus logs? |
Filed a ticket with our IT team to see if an exception can safely be made. |
awesome! glad we got to the bottom of this. |
Hello there,
Thank you for your work on the tool! I'm very excited to use it, but seem to have hit a snag:
I've ensured that the PID of
pytest
isn't moving around; i.e., it's a long-running process with no child processes. Yet while it's running, I get the outputKilled: 9
, exit status 137.Python 2.7.12
py-spy 0.1.8
macOS 12.13.6
Let me know if I can provide any other details!
The text was updated successfully, but these errors were encountered: