Docker debugging with a multiprocess app
I'm using the python plugin for IntelliJ, but I'd guess the problem is the same in PyCharm. I'm trying to debug an app using auto reloading in gunicorn. Gunicorn uses a master/worker forking model. When it detects that code has changed it lets the worker process(es) die and restart. Initially everything works as expected, I'm able to set breakpoints and have them hit in the worker (forked) process. If code changes and Gunicorn attempts to restart the worker, it dies with something like:

This all works fine when running things locally instead of a docker container. The difference appears to be that when running locally, IntelliJ/PyCharm is listening on a port and pydevd connects to it, it's started something like this:
python "/Users/.../Library/Application Support/IntelliJIdea2017.3/python/helpers/pydev/pydevd.py" --save-signatures --client 127.0.0.1 --port 60733 --file ...
The --client appears to put it in the mode where it connects to the process started by the IDE. When it's started in the docker container, it's started like this:
889af6cfe3b0:python -u /opt/.pycharm_helpers/pydev/pydevd.py --multiprocess --qt-support=auto --port 52269 --file ...
Since there's no --client, it looks like pydevd starts the listening port itself. The problem appears to be that when the code's reloaded, the Debugger#connect is re-executed for the new fork, which attempts to rebind to the listening port, and since the port was initially bound when the processes were started it ends up preventing the port binding.
Is there a way to configure the process to connect back to the IDE when running in docker, or some other way to prevent a new process from trying to bind to the debug port again?
Please sign in to leave a comment.
Please follow the issue on YouTrack: https://youtrack.jetbrains.com/issue/PY-28737