Skip to content

Commit c0bcf90

Browse files
authored
fix: guard against potential pkg_resources race (#14456)
We handle a potential race condition with the import of pkg_resources that can happen when a module watchdog is enabled while the import of said module is in progress. Addresses #14449. ## Checklist - [ ] 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)
1 parent a04c67b commit c0bcf90

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

ddtrace/internal/module.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,23 @@ def __init__(self) -> None:
331331
# NotImplementedError: Can't perform this operation for unregistered
332332
pkg_resources = sys.modules.get("pkg_resources")
333333
if pkg_resources is not None:
334-
pkg_resources.register_loader_type(_ImportHookChainedLoader, pkg_resources.DefaultProvider)
334+
for _ in range(5):
335+
# The package might be in the process of being imported by
336+
# another thread by the time this module watchdog is created. If
337+
# we fail to find the method we try again after a short wait.
338+
# This is likely to happen when a module watchdog is installed
339+
# lazily after boot, e.g. as a response to a RC enablement
340+
# record. This means that we are unlikely to add delays to the
341+
# main or any other use threads.
342+
try:
343+
pkg_resources.register_loader_type(_ImportHookChainedLoader, pkg_resources.DefaultProvider)
344+
break
345+
except AttributeError:
346+
from time import sleep
347+
348+
sleep(0.1)
349+
else:
350+
log.warning("Cannot ensure correct support with pkg_resources")
335351

336352
def _add_to_meta_path(self) -> None:
337353
sys.meta_path.insert(0, self) # type: ignore[arg-type]
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
fixes:
3+
- |
4+
Fixed an issue that could have caused some products to fail to start
5+
properly in applications that use ``pkg_resources``, either directly or
6+
indirectly.

0 commit comments

Comments
 (0)