Openstack nova协程导致远程DEBUG失败解决方法

在调试Nova代码时,代码中加入了远程pydevd断点,如下:

    def schedule_and_build_instances(self, context, build_requests,
                                     request_specs, image,
                                     admin_password, injected_files,
                                     requested_networks, block_device_mapping,
                                     tags=None):
        # Add all the UUIDs for the instances
        import pydevd
        pydevd.settrace('localhost', port=12345, stdoutToServer=True, stderrToServer=True)
        print "start schedule_and_build_instances"
        instance_uuids = [spec.instance_uuid for spec in request_specs]
        try:
            host_lists = self._schedule_instances(context, request_specs[0],
                    instance_uuids, return_alternates=True)

在pycharm中显示DEBUG连接成功,但无法进入断点,这种情况是由于nova中使用了协程,协程(https://www.cnblogs.com/gorlf/p/4246659.html)得在代码入口处就使用,出现在/nova/cmd/__init__.py中,如下:

import eventlet

from nova import debugger

if debugger.enabled():
    # turn off thread patching to enable the remote debugger
    eventlet.monkey_patch(os=False, thread=False)
else:
    eventlet.monkey_patch(os=False)

解决协程导致调试问题,将上述代码修改如下:

import eventlet

from nova import debugger

if debugger.enabled():
    # turn off thread patching to enable the remote debugger
    eventlet.monkey_patch(os=False, thread=False)
else:
    eventlet.monkey_patch(os=False, thread=False)

注:由于上述步骤中,将协程关闭了,所以之后如果涉及到多线程协作流程(例如一次性创建多个虚拟机),将可能出现错误。

原文地址:https://www.cnblogs.com/hurongpu/p/8421194.html