Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(gevent): unload time but not typing modules for module cloning #5135

Merged
merged 7 commits into from
Feb 16, 2023
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):
Yun-Kim marked this conversation as resolved.
Show resolved Hide resolved
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