|
| 1 | +# Copyright 2010 United States Government as represented by the |
| 2 | +# Administrator of the National Aeronautics and Space Administration. |
| 3 | +# Copyright 2011 Justin Santa Barbara |
| 4 | +# Copyright 2019 Red Hat, Inc. |
| 5 | +# All Rights Reserved. |
| 6 | +# |
| 7 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 8 | +# not use this file except in compliance with the License. You may obtain |
| 9 | +# a copy of the License at |
| 10 | +# |
| 11 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +# |
| 13 | +# Unless required by applicable law or agreed to in writing, software |
| 14 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 16 | +# License for the specific language governing permissions and limitations |
| 17 | +# under the License. |
| 18 | + |
| 19 | +"""Enable eventlet monkey patching.""" |
| 20 | + |
| 21 | +import os |
| 22 | + |
| 23 | + |
| 24 | +def _monkey_patch(): |
| 25 | + # NOTE(mdbooth): Anything imported here will not be monkey patched. It is |
| 26 | + # important to take care not to import anything here which requires monkey |
| 27 | + # patching. |
| 28 | + import eventlet |
| 29 | + import sys |
| 30 | + |
| 31 | + # NOTE(mdbooth): Imports only sys (2019-01-30). Other modules imported at |
| 32 | + # runtime on execution of debugger.init(). |
| 33 | + from nova import debugger |
| 34 | + |
| 35 | + # Note any modules with known monkey-patching issues which have been |
| 36 | + # imported before monkey patching. |
| 37 | + # urllib3: https://bugs.launchpad.net/nova/+bug/1808951 |
| 38 | + # oslo_context.context: https://bugs.launchpad.net/nova/+bug/1773102 |
| 39 | + problems = (set(['urllib3', 'oslo_context.context']) & |
| 40 | + set(sys.modules.keys())) |
| 41 | + |
| 42 | + # See https://bugs.launchpad.net/nova/+bug/1164822 |
| 43 | + # TODO(mdbooth): This feature was deprecated and removed in eventlet at |
| 44 | + # some point but brought back in version 0.21.0, presumably because some |
| 45 | + # users still required it to work round issues. However, there have been a |
| 46 | + # number of greendns fixes in eventlet since then. Specifically, it looks |
| 47 | + # as though the originally reported IPv6 issue may have been fixed in |
| 48 | + # version 0.24.0. We should remove this when we can confirm that the |
| 49 | + # original issue is fixed. |
| 50 | + os.environ['EVENTLET_NO_GREENDNS'] = 'yes' |
| 51 | + |
| 52 | + if debugger.enabled(): |
| 53 | + # turn off thread patching to enable the remote debugger |
| 54 | + eventlet.monkey_patch(thread=False) |
| 55 | + elif os.name == 'nt': |
| 56 | + # for nova-compute running on Windows(Hyper-v) |
| 57 | + # pipes don't support non-blocking I/O |
| 58 | + eventlet.monkey_patch(os=False) |
| 59 | + else: |
| 60 | + eventlet.monkey_patch() |
| 61 | + |
| 62 | + # NOTE(rpodolyaka): import oslo_service first, so that it makes eventlet |
| 63 | + # hub use a monotonic clock to avoid issues with drifts of system time (see |
| 64 | + # LP 1510234 for details) |
| 65 | + # NOTE(mdbooth): This was fixed in eventlet 0.21.0. Remove when bumping |
| 66 | + # eventlet version. |
| 67 | + import oslo_service # noqa |
| 68 | + eventlet.hubs.use_hub("oslo_service:service_hub") |
| 69 | + |
| 70 | + # NOTE(mdbooth): Log here instead of earlier to avoid loading oslo logging |
| 71 | + # before monkey patching. |
| 72 | + # NOTE(mdbooth): Ideally we would raise an exception here, as this is |
| 73 | + # likely to cause problems when executing nova code. However, some non-nova |
| 74 | + # tools load nova only to extract metadata and do not execute it. Two |
| 75 | + # examples are oslopolicy-policy-generator and sphinx, both of which can |
| 76 | + # fail if we assert here. It is not ideal that these utilities are monkey |
| 77 | + # patching at all, but we should not break them. |
| 78 | + # TODO(mdbooth): If there is any way to reliably determine if we are being |
| 79 | + # loaded in that kind of context without breaking existing callers, we |
| 80 | + # should do it and bypass monkey patching here entirely. |
| 81 | + if problems: |
| 82 | + from oslo_log import log as logging |
| 83 | + |
| 84 | + LOG = logging.getLogger(__name__) |
| 85 | + LOG.warning("Modules with known eventlet monkey patching issues were " |
| 86 | + "imported prior to eventlet monkey patching: %s. This " |
| 87 | + "warning can usually be ignored if the caller is only " |
| 88 | + "importing and not executing nova code.", |
| 89 | + ', '.join(problems)) |
| 90 | + |
| 91 | +# NOTE(mdbooth): This workaround is required to avoid breaking sphinx. See |
| 92 | +# separate comment in doc/source/conf.py. It may also be useful for other |
| 93 | +# non-nova utilities. Ideally the requirement for this workaround will be |
| 94 | +# removed as soon as possible, so do not rely on, or extend it. |
| 95 | +if (os.environ.get('OS_NOVA_DISABLE_EVENTLET_PATCHING', '').lower() |
| 96 | + not in ('1', 'true', 'yes')): |
| 97 | + _monkey_patch() |
0 commit comments