eclipsepydev

how to handle pydevd.settrace() with python when forking a process


I have a question about how pydevd.settrace() works.

I am working with a python application that forks a process to handle incoming requests. This raises a question. At the beginning of the execution, in order to debug with pydev, the following are called: import pydevd; pydevd.settrace('<network address>'). I assume this sets up a network connection between the process being debugged and the debug server. Now, this application is listening on another socket (let's use linux/unix in this example) and when a request arrives, it forks a process to handle that request. What is desired is for the debugging to now involve two different independent processes - the original process and the forked process. However, (at least in unix-like OSes) both the parent and child processes share the same socket, so if a breakpoint is set for code in the parent there is no way to distinguish this from setting a breakpoint for code in the child. In other words, if the breakpoint is reached, there is no way to tell if the parent or child process triggered the breakpoint.

If the socket created by the settrace call were available, when the child process executes, it could close it and then issue a new import pydevd; pydevd.settrace call. This would separate the debugging of the parent and child But, I see no way to do that with pydevd functions (at least there is nothing in the documentation I have found on the web). Is there a way to "close" a settrace() session in order to start another?


Solution

  • The PyDev debugger should usually attach to all subprocesses automatically and create a new debugger session (so, what you're saying about closing a session and then starting a new one shouldn't be needed as multiple sessions can happen in parallel over the same socket server from the Eclipse side).

    You may want to see whether the related setting is properly set in your installation just to make sure.

    PyDev Debugger Settings

    There is a caveat when using Remote Debugging though:

    If you're using pydevd.settrace you need to ask it to do that though by passing patch_multiprocessing=True.

    i.e.:

    pydevd.settrace(host='192.168.0.1', patch_multiprocessing=True)

    Another option could be just calling pydevd.settrace(...) multiple times from multiple processes -- after starting the remote debugger, any new pydevd.settrace(...) call should automatically attach to the debugger and create a new independent debug session -- just make sure you haven't stopped the remote debugger session (i.e.: in this case as in the regular case, multiple sessions can happen in parallel over the same socket server from the Eclipse side).

    If the remote scenario is common for you, maybe you can leave the remote debugger always on...

    Remote Debugger always on