From bf6f5d6e2ce0224c7e4d0e024ecaa7120f7e9fd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ferenc=20G=C3=A9czi?= Date: Wed, 12 Jun 2024 12:00:00 +0000 Subject: [PATCH] fix: Prevent crash with 'gevent' has no attribute 'version_info' MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If the `gevent` module is not installed, but there is a directory called `gevent` or a `gevent.py` file in the current directory, then that is already importable and the statment `if sys.modules['gevent'].version_info < (1, 4):` fails with `AttributeError: module 'gevent' has no attribute 'version_info'` This can happen easily since many libraries like `celery`, `grpc`, `opentracing` etc, deliver their own file called `gevent.py`. Signed-off-by: Ferenc Géczi --- src/instana/instrumentation/gevent_inst.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/instana/instrumentation/gevent_inst.py b/src/instana/instrumentation/gevent_inst.py index 322f0b1ca..93ed38001 100644 --- a/src/instana/instrumentation/gevent_inst.py +++ b/src/instana/instrumentation/gevent_inst.py @@ -37,10 +37,11 @@ def spawn_callback(new_greenlet): logger.debug("instrument_gevent: ", exc_info=True) -if 'gevent' in sys.modules: - if sys.modules['gevent'].version_info < (1, 4): - logger.debug("gevent < 1.4 detected. The Instana package supports gevent versions 1.4 and greater.") - else: - instrument_gevent() -else: +if not 'gevent' in sys.modules: logger.debug("Instrumenting gevent: gevent not detected or loaded. Nothing done.") +elif not hasattr(sys.modules['gevent'], 'version_info'): + logger.debug("gevent module has no 'version_info'. Skipping instrumentation.") +elif sys.modules['gevent'].version_info < (1, 4): + logger.debug("gevent < 1.4 detected. The Instana package supports gevent versions 1.4 and greater.") +else: + instrument_gevent()