deshaw / pyflyby

A set of productivity tools for Python
https://deshaw.github.io/pyflyby/
Other
353 stars 52 forks source link

Remote debugger hangs if ptrace is not allowed #142

Open antocuni opened 3 years ago

antocuni commented 3 years ago

This is problem which I ran into while trying to reproduce #131:

(venv) homer tmp $ py -d 2503089

^C^C^C

i.e., the py script just hangs indefinitely while trying to attach a debugger:

If I try to run gdb -p manually, this is what happens:

(venv) homer tmp $ gdb -p 2503089
GNU gdb (Ubuntu 9.2-0ubuntu1~20.04) 9.2
[...]
Attaching to process 2503089
ptrace: No such process.
(gdb) 

So, I think that what happens under the hood is the following:

  1. attach_debugger spawns gdb
  2. the spawned gdb waits forever for user input (which will never happen)
  3. the watchdog process waits forever for gdb to exit (which will never happen)

The solution is to enable ptrace globally. After running the following command, py -d works again:

(venv) homer tmp $ echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope

I think that the proper behavior for attach_debugger should be to detect this case and warn the user. E.g.:

  1. read the value of /proc/sys/kernel/yama/ptrace_scope and emit a clear error message, with links to the relevant documentation and maybe suggest to run the command above
  2. try to detect whether the spawned gdb works correctly or not. This is harder because it would mean to modify _dbg.inject() to read and parse the output of the gdb commands
umairanis03 commented 1 year ago

Is this issue tracking debugger attachment hanging?

Reproducer

from pyflyby import debugger

def test():
    x = 1
    y = '123'
    debugger(wait_for_attach=True)
    return x + y

test()

Attaching a debugger hangs:

$ ssh -t <hostname> /usr/local/python/python-3.10/std/bin/py -d 4157723

Entering debugger.  Use 'n' to step, 'c' to continue running, 'q' to quit Python completely.

<HANGS>