Skip to content

Commit

Permalink
Merge branch '2.12' into backport-10423-to-2.12
Browse files Browse the repository at this point in the history
  • Loading branch information
romainkomorndatadog authored Aug 30, 2024
2 parents e2ed335 + 72e355e commit cf76018
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
7 changes: 5 additions & 2 deletions ddtrace/internal/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ def call_back(self, module: ModuleType) -> None:
except (AttributeError, TypeError):
pass
try:
module.spec.loader = self.loader
object.__getattribute__(module, "spec").loader = self.loader
except (AttributeError, TypeError):
pass

Expand Down Expand Up @@ -313,7 +313,10 @@ def get_code(_loader, fullname):
exception_hook(self, module)
raise

self.call_back(module)
try:
self.call_back(module)
except Exception:
log.exception("Failed to call back on module %s", module)


class BaseModuleWatchdog(abc.ABC):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
fixes:
- |
Fix for a side-effect issue with module import callbacks that could cause
a runtime exception.
11 changes: 11 additions & 0 deletions tests/internal/side_effect_module.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import sys
from types import ModuleType


class SideEffectModule(ModuleType):
def __getattribute__(self, name):
if name in {"spec"}:
raise RuntimeError("Attribute lookup side effect")


sys.modules[__name__].__class__ = SideEffectModule
7 changes: 7 additions & 0 deletions tests/internal/test_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,3 +526,10 @@ def test_module_watchdog_reloads_dont_cause_errors():
sys.setrecursionlimit(1000)
for _ in range(sys.getrecursionlimit() * 2):
reload(ns_module)


@pytest.mark.subprocess(ddtrace_run=True)
def test_module_import_side_effect():
# Test that we can import a module that raises an exception during specific
# attribute lookups.
import tests.internal.side_effect_module # noqa:F401

0 comments on commit cf76018

Please sign in to comment.