fix(internal): resolve shared loader get_code via active exec context #22131
All Vouch review confidence checks passed
Residual risk 0.24 = Change Risk (0.46) × (1 − Review Confidence (0.47)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.46 · 464 lines diff across 5 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.367
Shallow Review Depth
shallow_review_depth
0.344
Real PR Conversation Timeline
1 comments
"Review active on #22131"
general
weight: 0.35
gnufede
Description
_ImportHookChainedLoader._exec_modulewraps the imported module's loaderget_codeso registered transformers (code transformations / bytecode instrumentation) run on the code that gets executed. When a loader is shared by several modules — e.g. azipimporterserving every module of a zip application or bundled deployment — that wrapper was bound to the module of whichever import happened to install it. As a result:get_code()caller (e.g.runpy,inspect,linecache) received transformed code;importlib.reload()left the wrapper in a state that kept transforming code fetched afterwards.This PR replaces the captured-module design with a context-local active-exec marker:
get_code, resolved via the bound method's__self__. This works even whenself.loaderis a proxy (e.g. wrapt's_ImportHookChainedLoaderwrapping the shared loader) and whenget_codewas already wrapped by a previous import._exec_modulerecords(loader, chained_loader, module)in acontextvars.ContextVar. The wrapper transforms the code only when the fetch is for that exact loader/module during that execution (consuming the marker once, so aget_code()call made from the module body itself returns untransformed code), and returns untransformed code to every other caller.finally, so failures (e.g. a module raising during reload) cannot poison laterget_code()calls, and after-import callbacks run after it is reset.After-import hooks (
after_import) are unaffected: only code transformations / bytecode instrumentation were impacted.Changelog
Tests / Validation
New
tests/internal/test_module_shared_loader.py(subprocess tests, skipped below Python 3.10 wherezipimporterhas noexec_module):get_code()for other modules — and from the module's own body, and after a failed reload — returns untransformed code;module_from_spec/spec.loader.exec_modulewithout enteringsys.modulesare still transformed;sys.meta_pathorderings (watchdog loader wrappingwrapt(zipimporter), and wrapt's chained loader wrappingdd(zipimporter)) transform on first import and on a laterimportlib.reload()(with explicitsys.meta_path.index(...)assertions so each variant proves which chain ran).Validation performed:
scripts/lint fmt,scripts/lint typing,scripts/lint type-comments-check --base-ref origin/main,scripts/lint checks— all pass;tests/internal/suite (Python 3.14): 3391 passed, 38 skipped, 14 xfailed, 15 xpassed (xpasses pre-existing intest_native_logger.py);tests/internal/test_module.py+tests/internal/test_module_shared_loader.pyon Python 3.14 (wrapt 2.x) and Python 3.13 (wrapt 1.x) — all pass;main, 5 of the 8 new tests fail, confirming they cover the regressions.Risks
Low: the change is confined to the
get_codewrapping path inddtrace/internal/module.py; loaders withoutget_codeare untouched, and the wrapper's behavior for non-shared loaders is unchanged.