def _monkey_patch(): # NOTE(mdbooth): Anything imported here will not be monkey patched. It is # important to take care not to import anything here which requires monkey # patching. # NOTE(artom) eventlet processes environment variables at import-time. # as such any eventlet configuration should happen here if needed. import eventlet import sys # NOTE(mdbooth): Imports only sys (2019-01-30). Other modules imported at # runtime on execution of debugger.init(). from nova import debugger # Note any modules with known monkey-patching issues which have been # imported before monkey patching. # urllib3: https://bugs.launchpad.net/nova/+bug/1808951 # oslo_context.context: https://bugs.launchpad.net/nova/+bug/1773102 problems = (set(['urllib3', 'oslo_context.context']) & set(sys.modules.keys())) if debugger.enabled(): # turn off thread patching to enable the remote debugger eventlet.monkey_patch(thread=False) elif os.name == 'nt': # for nova-compute running on Windows(Hyper-v) # pipes don't support non-blocking I/O eventlet.monkey_patch(os=False) else: eventlet.monkey_patch() # Monkey patch the original current_thread to use the up-to-date _active # global variable. See https://bugs.launchpad.net/bugs/1863021 and # https://github.com/eventlet/eventlet/issues/592 import __original_module_threading as orig_threading import threading orig_threading.current_thread.__globals__['_active'] = threading._active # NOTE(mdbooth): Log here instead of earlier to avoid loading oslo logging # before monkey patching. # NOTE(mdbooth): Ideally we would raise an exception here, as this is # likely to cause problems when executing nova code. However, some non-nova # tools load nova only to extract metadata and do not execute it. Two # examples are oslopolicy-policy-generator and sphinx, both of which can # fail if we assert here. It is not ideal that these utilities are monkey # patching at all, but we should not break them. # TODO(mdbooth): If there is any way to reliably determine if we are being # loaded in that kind of context without breaking existing callers, we # should do it and bypass monkey patching here entirely. if problems: from oslo_log import log as logging LOG = logging.getLogger(__name__) LOG.warning( "Modules with known eventlet monkey patching issues were " "imported prior to eventlet monkey patching: %s. This " "warning can usually be ignored if the caller is only " "importing and not executing nova code.", ', '.join(problems))
def monkey_patch(): 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) # NOTE(rgerganov): oslo.context is storing a global thread-local variable # which keeps the request context for the current thread. If oslo.context # is imported before calling monkey_patch(), then this thread-local won't # be green. To workaround this, reload the module after calling # monkey_patch() reload_module(importutils.import_module('oslo_context.context'))
def monkey_patch(): if debugger.enabled(): # turn off thread patching to enable the remote debugger eventlet.monkey_patch(thread=False) elif os.name == 'nt': # for nova-compute running on Windows(Hyper-v) # pipes don't support non-blocking I/O eventlet.monkey_patch(os=False) else: eventlet.monkey_patch() # NOTE(rgerganov): oslo.context is storing a global thread-local variable # which keeps the request context for the current thread. If oslo.context # is imported before calling monkey_patch(), then this thread-local won't # be green. To workaround this, reload the module after calling # monkey_patch() reload_module(importutils.import_module('oslo_context.context'))
def _monkey_patch(): # NOTE(mdbooth): Anything imported here will not be monkey patched. It is # important to take care not to import anything here which requires monkey # patching. import eventlet import sys # NOTE(mdbooth): Imports only sys (2019-01-30). Other modules imported at # runtime on execution of debugger.init(). from nova import debugger # Note any modules with known monkey-patching issues which have been # imported before monkey patching. # urllib3: https://bugs.launchpad.net/nova/+bug/1808951 # oslo_context.context: https://bugs.launchpad.net/nova/+bug/1773102 problems = (set(['urllib3', 'oslo_context.context']) & set(sys.modules.keys())) # See https://bugs.launchpad.net/nova/+bug/1164822 # TODO(mdbooth): This feature was deprecated and removed in eventlet at # some point but brought back in version 0.21.0, presumably because some # users still required it to work round issues. However, there have been a # number of greendns fixes in eventlet since then. Specifically, it looks # as though the originally reported IPv6 issue may have been fixed in # version 0.24.0. We should remove this when we can confirm that the # original issue is fixed. os.environ['EVENTLET_NO_GREENDNS'] = 'yes' if debugger.enabled(): # turn off thread patching to enable the remote debugger eventlet.monkey_patch(thread=False) elif os.name == 'nt': # for nova-compute running on Windows(Hyper-v) # pipes don't support non-blocking I/O eventlet.monkey_patch(os=False) else: eventlet.monkey_patch() # NOTE(rpodolyaka): import oslo_service first, so that it makes eventlet # hub use a monotonic clock to avoid issues with drifts of system time (see # LP 1510234 for details) # NOTE(mdbooth): This was fixed in eventlet 0.21.0. Remove when bumping # eventlet version. import oslo_service # noqa eventlet.hubs.use_hub("oslo_service:service_hub") # NOTE(mdbooth): Log here instead of earlier to avoid loading oslo logging # before monkey patching. # NOTE(mdbooth): Ideally we would raise an exception here, as this is # likely to cause problems when executing nova code. However, some non-nova # tools load nova only to extract metadata and do not execute it. Two # examples are oslopolicy-policy-generator and sphinx, both of which can # fail if we assert here. It is not ideal that these utilities are monkey # patching at all, but we should not break them. # TODO(mdbooth): If there is any way to reliably determine if we are being # loaded in that kind of context without breaking existing callers, we # should do it and bypass monkey patching here entirely. if problems: from oslo_log import log as logging LOG = logging.getLogger(__name__) LOG.warning("Modules with known eventlet monkey patching issues were " "imported prior to eventlet monkey patching: %s. This " "warning can usually be ignored if the caller is only " "importing and not executing nova code.", ', '.join(problems))
# TODO(mikal): move eventlet imports to nova.__init__ once we move to PBR import os import sys import traceback # NOTE(mikal): All of this is because if dnspython is present in your # environment then eventlet monkeypatches socket.getaddrinfo() with an # implementation which doesn't work for IPv6. What we're checking here is # that the magic environment variable was set when the import happened. # NOTE(dims): Prevent this code from kicking in under docs generation # as it leads to spurious errors/warning. stack = traceback.extract_stack() if ('eventlet' in sys.modules and os.environ.get('EVENTLET_NO_GREENDNS', '').lower() != 'yes' and (len(stack) < 2 or 'sphinx' not in stack[-2][0])): raise ImportError('eventlet imported before nova/cmd/__init__ ' '(env var set to %s)' % os.environ.get('EVENTLET_NO_GREENDNS')) os.environ['EVENTLET_NO_GREENDNS'] = 'yes' 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)
# Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. # TODO(mikal): move eventlet imports to nova.__init__ once we move to PBR import os import sys # NOTE(mikal): All of this is because if dnspython is present in your # environment then eventlet monkeypatches socket.getaddrinfo() with an # implementation which doesn't work for IPv6. What we're checking here is # that the magic environment variable was set when the import happened. if ('eventlet' in sys.modules and os.environ.get('EVENTLET_NO_GREENDNS', '').lower() != 'yes'): raise ImportError('eventlet imported before nova/cmd/__init__ ' '(env var set to %s)' % os.environ.get('EVENTLET_NO_GREENDNS')) os.environ['EVENTLET_NO_GREENDNS'] = 'yes' 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)