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

Django PyCharm调试器在短空闲时间后与远程主机断开连接,django,debugging,pycharm,pydev,Django,Debugging,Pycharm,Pydev,多年来,我一直在使用PyDev/PyCharm调试器的远程调试功能,它工作得很好。在过去的一年左右的时间里,我一直在一个基于Django的项目中使用它,该项目在Docker容器中运行,它也可以很好地工作 但从几个月前开始(可能是因为我的代码商店切换到了Python3?),我一直对它感到沮丧,因为调试器一直在无缘无故地断开连接。我将处于调试阶段,这将是很好的,但如果我让系统闲置大约一分钟,调试会话突然死亡,执行的请求没有我继续下去。 当这种情况发生时,我将看到控制台输出从: Connected t

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

但从几个月前开始(可能是因为我的代码商店切换到了Python3?),我一直对它感到沮丧,因为调试器一直在无缘无故地断开连接。我将处于调试阶段,这将是很好的,但如果我让系统闲置大约一分钟,调试会话突然死亡,执行的请求没有我继续下去。 当这种情况发生时,我将看到控制台输出从:

Connected to pydev debugger (build 173.3942.36)
致:

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

此外,一旦发生这种情况,进一步的调试会导致在我的容器日志中出现几十行这样的行,而我从未点击过“Resume Program”:

唯一能阻止这种情况发生的是重新启动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调试器(build 173.4301.16)”后,只需让系统闲置40秒,不管调试器是否真的遇到断点,连接就会终止并返回到“等待进程连接…”状态。

我完全通过偶然发现找到了这个问题的根源。事实证明,Docker在最近的edge构建中为Mac开发人员犯了一个配置错误。他们在今天刚刚发布的最新版本中修复了它。当我检查变更日志时,我注意到一个错误修复,它让我想到“嗯,这可能是调试器超时问题的根源…”安装新版本实际上解决了这个问题

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


我把这个问题留待以后再回答,以防其他人遇到这个问题。修复此问题的Docker for Mac版本为18.01.0-ce-mac48(22004),目前可在边缘频道上获得。

您使用的是专业版还是社区版?@Batman professional edition。感谢您在@CoreDumpError!中保留此选项!这对我的问题有很大帮助。出于好奇,您是否仍然遇到
找不到线程…
消息?似乎是在我这边,但似乎没有影响任何事情。@woootcl我不记得最近见过他们,但我一直在使用一个垃圾邮件较多的docker日志过滤器,所以我可能错过了他们。然而,自从写这篇文章以来,我也切换到了每请求调试器连接,而不是在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']
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()