Skip to content

Commit

Permalink
chore(di): clean up run module logic (#11082)
Browse files Browse the repository at this point in the history
We clean up the run module logic and improve the detection of the
`__main__` module. We also fix some typing.

## Checklist
- [x] PR author has checked that all the criteria below are met
- The PR description includes an overview of the change
- The PR description articulates the motivation for the change
- The change includes tests OR the PR description describes a testing
strategy
- The PR description notes risks associated with the change, if any
- Newly-added code is easy to change
- The change follows the [library release note
guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html)
- The change includes or references documentation updates if necessary
- Backport labels are set (if
[applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting))

## Reviewer Checklist
- [ ] Reviewer has checked that all the criteria below are met 
- Title is accurate
- All changes are related to the pull request's stated goal
- Avoids breaking
[API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces)
changes
- Testing strategy adequately addresses listed risks
- Newly-added code is easy to change
- Release note makes sense to a user of the library
- If necessary, author has acknowledged and discussed the performance
implications of this PR as reported in the benchmarks PR comment
- Backport labels are set in a manner that is consistent with the
[release branch maintenance
policy](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)
  • Loading branch information
P403n1x87 authored Oct 21, 2024
1 parent cf2b638 commit b6c85b1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
2 changes: 1 addition & 1 deletion ddtrace/debugging/_debugger.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ class Debugger(Service):
__logger__ = ProbeStatusLogger

@classmethod
def enable(cls, run_module: bool = False) -> None:
def enable(cls) -> None:
"""Enable dynamic instrumentation
This class method is idempotent. Dynamic instrumentation will be
Expand Down
21 changes: 14 additions & 7 deletions ddtrace/internal/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,16 +453,19 @@ def __init__(self) -> None:
super().__init__()

self._hook_map: t.DefaultDict[str, t.List[ModuleHookType]] = defaultdict(list)
self._om: t.Optional[t.Dict[str, ModuleType]] = None
# DEV: It would make more sense to make this a mapping of Path to ModuleType
# but the WeakValueDictionary causes an ignored exception on shutdown
# because the pathlib module is being garbage collected.
self._om: t.Optional[t.MutableMapping[str, ModuleType]] = None
# _pre_exec_module_hooks is a set of tuples (condition, hook) instead
# of a list to ensure that no hook is duplicated
self._pre_exec_module_hooks: t.Set[t.Tuple[PreExecHookCond, PreExecHookType]] = set()
self._import_exception_hooks: t.Set[t.Tuple[ImportExceptionHookCond, ImportExceptionHookType]] = set()

@property
def _origin_map(self) -> t.Dict[str, ModuleType]:
def modules_with_origin(modules: t.Iterable[ModuleType]) -> t.Dict[str, t.Any]:
result: wvdict = wvdict()
def _origin_map(self) -> t.MutableMapping[str, ModuleType]:
def modules_with_origin(modules: t.Iterable[ModuleType]) -> t.MutableMapping[str, ModuleType]:
result: t.MutableMapping[str, ModuleType] = wvdict()

for m in modules:
module_origin = origin(m)
Expand All @@ -479,7 +482,7 @@ def modules_with_origin(modules: t.Iterable[ModuleType]) -> t.Dict[str, t.Any]:
# information that can be used at the Python runtime level.
pass

return t.cast(t.Dict[str, t.Any], result)
return result

if self._om is None:
try:
Expand Down Expand Up @@ -526,7 +529,7 @@ def get_by_origin(cls, _origin: Path) -> t.Optional[ModuleType]:

# Check if this is the __main__ module
main_module = sys.modules.get("__main__")
if main_module is not None and origin(main_module) == path:
if main_module is not None and origin(main_module) == resolved_path:
# Register for future lookups
instance._origin_map[path] = main_module

Expand Down Expand Up @@ -557,7 +560,11 @@ def register_origin_hook(cls, origin: Path, hook: ModuleHookType) -> None:
instance = t.cast(ModuleWatchdog, cls._instance)
instance._hook_map[path].append(hook)
try:
module = instance._origin_map[path]
module = instance.get_by_origin(resolved_path)
if module is None:
# The path was resolved but we still haven't seen any module
# that has it as origin. Nothing more we can do for now.
return
# Sanity check: the module might have been removed from sys.modules
# but not yet garbage collected.
try:
Expand Down

0 comments on commit b6c85b1

Please sign in to comment.