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



多年来我一直在使用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 应用程序。除非我这样做,否则对服务器发出的进一步请求不会触发任何断点。

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

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,因为我卡在"等待进程连接...")。

也许我的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()

我应该以不同的方式做这件事吗?

编辑:这比我意识到的要糟糕。只需让系统在看到"连接到 pydev 调试器(内部版本 173.4301.16)"后闲置 40 秒,无论调试器是否实际命中断点,连接都会失效并返回到"正在等待进程连接...">

我纯粹通过偶然发现了这个问题的根源。事实证明,适用于Mac的Docker开发人员在最近的边缘构建中犯了一个配置错误。他们在今天刚刚发布的最新版本中修复了它。当我检查更改日志时,我注意到一个错误修复,这让我想"嗯,这可能是这个调试器超时问题的根源......"事实上,安装新版本确实解决了这个问题。

发生的事情是,他们在 Docker for Mac 中使用的vpnkit软件被错误地配置为在空闲 30 秒后终止 TCP 连接。该修复将该数字推回了 300 秒,这在调试时更容易忍受超时。

我把这个问题留了下来,并回答了它,以防万一其他人遇到这个问题。解决此问题的 Docker for Mac 版本是 18.01.0-ce-mac48 (22004),目前在 Edge 通道上可用。

最新更新