PyCharm 调试器在短暂空闲时间后与远程主机断开连接

PyCharm debugger disconnects from remote host after short idle time

多年来我一直在使用 PyDev/PyCharm 调试器的远程调试功能,而且效果很好。在过去一年左右的时间里,我一直在将它用于一个基于 Django 的项目,该项目在 Docker 容器中运行,并且它也能很好地工作。

但是自从几个月前(可能是由于我的代码商店切换到 Python 3?)以来,我一直对它感到沮丧,因为调试器无缘无故地断开连接。我将处于调试会话的中间,该会话进行得很好,但是如果我让系统空闲大约一分钟,调试会话就会突然终止,并且在没有我的情况下继续执行请求。

当发生这种情况时,我会看到控制台输出从:

Connected to pydev debugger (build 173.3942.36)

收件人:

Connected to pydev debugger (build 173.3942.36)
Waiting for process connection...

一旦发生这种情况,让调试器重新连接到我容器内的 运行 Django 服务器的唯一解决方案是重新启动 gunicorn,通过编辑文件使 gunicorn 重新加载自身,或者通过重新启动kill -HUP 的 gunicorn 应用程序。除非我这样做,否则对服务器发出的进一步请求不会触发任何断点。

此外,一旦发生这种情况,进一步的调试会导致我的容器日志中出现数十行这样的行,无论我何时点击 "Resume Program":

container-name    | Could not find thread pid_81_id_140102606217792
container-name    | Available: ['pid_232_id_140052765045312', 'pid_232_id_140052676863480', 'pid_232_id_140052644157928', 'pid_232_id_140052644157200', 'pid_232_id_140052660987328', 'pid_232_id_140052643054984']
container-name    | Could not find thread pid_81_id_140102606217792
container-name    | Available: ['pid_240_id_140052765045312', 'pid_240_id_140052676863480', 'pid_240_id_140052643056888', 'pid_240_id_140052644157200', 'pid_240_id_140052660987328', 'pid_240_id_140052643054984']
container-name    | Could not find thread pid_52_id_140052676829080
container-name    | Available: ['pid_240_id_140052765045312', 'pid_240_id_140052676863480', 'pid_240_id_140052643056888', 'pid_240_id_140052644157200', 'pid_240_id_140052660987328', 'pid_240_id_140052643054984']
container-name    | Could not find thread pid_232_id_140052765045312
container-name    | Available: ['pid_240_id_140052765045312', 'pid_240_id_140052676863480', 'pid_240_id_140052643056888', 'pid_240_id_140052644157200', 'pid_240_id_140052660987328', 'pid_240_id_140052643054984']
container-name    | Could not find thread pid_223_id_140052714764496
container-name    | Available: ['pid_240_id_140052765045312', 'pid_240_id_140052676863480', 'pid_240_id_140052643056888', 'pid_240_id_140052644157200', 'pid_240_id_140052660987328', 'pid_240_id_140052643054984']

唯一阻止这种情况发生的是重新启动 PyCharm 调试器(这需要重新启动 gunicorn,因为我卡在 "Waiting for process connection...")。

也许我的 pydevd.settrace() 位置不对?它在我的 wsgi.py:

import os
import sys
from django.core.wsgi import get_wsgi_application
from djunk.utils import getenv

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'multitenant.settings.settings')

# If the environment is configured to enable remote debugging, attach to the configured remote debug server.
# If REMOTE_DEBUG_ENABLED set to True, REMOTE_DEBUG_HOST and REMOTE_DEBUG_PORT are required.
if getenv('REMOTE_DEBUG_ENABLED', False):
    print('Debugging Enabled')
    # We keep this import inside the REMOTE_DEBUG_ENABLED check because simply doing the import slows down the process,
    # even if we don't call settrace().

    sys.path.insert(0, '/debug/pycharm-debug-py3k.egg')
    import pydevd
    # Attach to a Remote Debugger session running in PyCharm or PyDev on the configured host and port.
    # NOTE: If no remote debug server is running, this call will crash and the exception handler will also crash. 
    # Be aware of this!
    pydevd.settrace(
        host=getenv('REMOTE_DEBUG_HOST'),
        port=getenv('REMOTE_DEBUG_PORT'),
        suspend=False
    )

application = get_wsgi_application()

我应该采取不同的做法吗?

编辑:比我想象的还要糟糕。在看到 "Connected to pydev debugger (build 173.4301.16)" 后让系统闲置 40 秒,无论调试器是否真正遇到断点,连接都会终止并返回到 "Waiting for process connection..."

我完全是偶然发现了这个问题的根源。事实证明,在最近的边缘构建中,Docker 为 Mac 开发人员犯了一个配置错误。他们在今天刚刚发布的最新版本中修复了它。当我检查变更日志时,我注意到一个错误修复,这让我觉得 "hmmm, that could be the source of this debugger timeout issue..." 安装新版本实际上解决了这个问题。

发生的事情是,他们在 Docker 中为 Mac 使用的 vpnkit 软件被错误地配置为在 30 秒空闲时间后终止 TCP 连接。该修复程序将该数字推回到 300 秒,这对于调试时的超时来说是可以忍受的。

我已经留下这个问题并回答了它,以防万一其他人遇到这个问题。修复此问题的 Docker for Mac 的版本是 18.01.0-ce-mac48 (22004),目前可在 Edge 频道获得。