apm-agent-python icon indicating copy to clipboard operation
apm-agent-python copied to clipboard

capture_span return None, which is not handled correctly

Open boryszef opened this issue 1 year ago • 1 comments

Describe the bug:

I run into an issue, where the KafkaInstrumentation class throws an exception in line 146 of https://github.com/elastic/apm-agent-python/blob/main/elasticapm/instrumentation/packages/kafka.py:

AttributeError: 'NoneType' object has no attribute 'cancel'

this happens when iterating over KafkaConsumer. Indeed, looking at the code, in https://github.com/elastic/apm-agent-python/blob/main/elasticapm/instrumentation/packages/kafka.py, line 132 a context manager is created; the capture_span class calls handle_enter (in https://github.com/elastic/apm-agent-python/blob/main/elasticapm/traces.py, line 1085), which might return None, but inside of the with block the check (if span) is done only in the subsequent line.

Environment (please complete the following information)

  • OS: Linux
  • Python version: 3.9.17
  • Framework and version [e.g. Django 2.1]: Django 3.2.19
  • Agent version: 6.15.1

boryszef avatar Jun 27 '24 09:06 boryszef

Thanks for reporting and for the analysis.

xrmx avatar Jun 27 '24 09:06 xrmx