Skip to content

[BUG]: Lazy import of debugging product is broken with Python 3.11 and bytecode < 0.15.1 #12697

Open
@oranav

Description

@oranav

Tracer Version(s)

3.2.0

Python Version(s)

3.11.11

Pip Version(s)

24.3.1

Bug Report

This was triggered by #12385, but #12467 was insufficient since the same error happens with Python 3.11 as well.
Essentially the lazy loading of the plugins fails. See the error message below.
We need to update the dependency for 3.11 (and possible other Python versions, I haven't checked) to >= 0.15.1.

Reproduction Code

DD_TRACE_ENABLED=true ddtrace-run python3

Error Logs

Discovered product plugin 'appsec'
Product plugin 'appsec' loaded successfully
Discovered product plugin 'code-origin-for-spans'
Failed to load product plugin 'code-origin-for-spans'
Traceback (most recent call last):
  File "REDACTED/.venv/lib/python3.11/site-packages/ddtrace/internal/products.py", line 59, in __init__
    product: Product = product_plugin.load()
                       ^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/python@3.11/3.11.11/Frameworks/Python.framework/Versions/3.11/lib/python3.11/importlib/metadata/__init__.py", line 202, in load
    module = import_module(match.group('module'))
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/python@3.11/3.11.11/Frameworks/Python.framework/Versions/3.11/lib/python3.11/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1204, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1126, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1204, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1126, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1204, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1126, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1204, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 940, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "REDACTED/.venv/lib/python3.11/site-packages/ddtrace/debugging/__init__.py", line 44, in <module>
    @lazy
     ^^^^
  File "REDACTED/.venv/lib/python3.11/site-packages/ddtrace/internal/module.py", line 716, in lazy
    LazyWrappingContext(t.cast(FunctionType, f)).wrap()
  File "REDACTED/.venv/lib/python3.11/site-packages/ddtrace/internal/wrapping/context.py", line 397, in wrap
    t.cast(_UniversalWrappingContext, _UniversalWrappingContext.wrapped(self.__wrapped__)).register(self)
                                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "REDACTED/.venv/lib/python3.11/site-packages/ddtrace/internal/wrapping/context.py", line 349, in wrapped
    context.wrap()
  File "REDACTED/.venv/lib/python3.11/site-packages/ddtrace/internal/wrapping/context.py", line 564, in wrap
    f.__code__ = bc.to_code()
                 ^^^^^^^^^^^^
  File "REDACTED/.venv/lib/python3.11/site-packages/bytecode/bytecode.py", line 316, in to_code
    return bc.to_code(
           ^^^^^^^^^^^
  File "REDACTED/.venv/lib/python3.11/site-packages/bytecode/concrete.py", line 871, in to_code
    self._assemble_exception_table(),
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "REDACTED/.venv/lib/python3.11/site-packages/bytecode/concrete.py", line 807, in _assemble_exception_table
    table.extend(self._encode_varint(entry.start_offset, True))
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "REDACTED/.venv/lib/python3.11/site-packages/bytecode/concrete.py", line 799, in _encode_varint
    temp[-1] |= 128
    ~~~~^^^^
IndexError: list index out of range

Libraries in Use

bytecode == 0.14.0
ddtrace == 3.2.0

Operating System

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions