Ptrace, Syscall, and Python

I was hunting down an obscure interaction between ptrace(2) and what happens when you attach to a process which is in the middle of a syscall. In fact, if you read the ptrace man page there are a lot of options related to how syscalls are handled, and I tried a variety of them (e.g. PTRACE_ATTACH v.s. PTRACE_SEIZE followed by PTRACE_INTERRUPT). I got stumped on this problem for most of today, and eventually went to go write a simple test case that didn't involve the Python interpreter so I could get a better understanding of what was actually happening.

Oddly, my simple test case worked totally fine---I had no issues with ptrace() while the program was in a syscall state. In fact, by ptrace() didn't even cause the syscall to EINTR.

I eventually figured out what was going on: it's related to some weird magic in the Python interpreter. I would write about it here but I already wrote about the issue in great depth in the GitHub repo. So if you want to read the rest of the story go to eklitzke/ptrace-syscall and check out the README.