Skip to content

Commit

Permalink
fix(gevent): unload time but not typing modules for module cloning (#…
Browse files Browse the repository at this point in the history
…5135)

This PR makes a fix to #4863 which inadvertently removed unloading the
`time` module as well as removing the `typing` module from the list of
standard library modules to not unload for module cloning. We found that
the `time` module is a special case (which is important to unload due to
interactions with gevent patching) as it is implicitly imported by
recent versions of CPython on interpreter startup, so it may be present
in `sys.modules` already.
The `typing` module is also problematic on being reloaded for older
versions of Python < 3.7, so we should not unload/reload this module as
well.
  • Loading branch information
Yun-Kim authored Feb 16, 2023
1 parent 6ff460e commit bd20c55
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions ddtrace/bootstrap/sitecustomize.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@
import os # noqa


"""
The following modules cause problems when being unloaded/reloaded in module cloning.
Notably, unloading the atexit module will remove all registered hooks which we use for cleaning up on tracer shutdown.
The other listed modules internally maintain some state that does not coexist well if reloaded.
"""
MODULES_TO_NOT_CLEANUP = {"atexit", "asyncio", "attr", "concurrent", "ddtrace", "logging"}
if sys.version_info < (3, 7):
MODULES_TO_NOT_CLEANUP |= {"typing"} # required by older versions of Python
if sys.version_info <= (2, 7):
MODULES_TO_NOT_CLEANUP |= {"encodings", "codecs"}
import imp
Expand Down Expand Up @@ -72,6 +79,9 @@ def cleanup_loaded_modules(aggressive=False):
break
else:
del sys.modules[module_name]
# Some versions of CPython import the time module during interpreter startup, which needs to be unloaded.
if "time" in sys.modules:
del sys.modules["time"]


will_run_module_cloning = should_cleanup_loaded_modules()
Expand Down

0 comments on commit bd20c55

Please sign in to comment.