fix(tracing): keep numpy loaded during module cleanup#18919
fix(tracing): keep numpy loaded during module cleanup#18919arpitjain099 wants to merge 4 commits into
Conversation
numpy 2.4 added a guard (numpy/numpy#29030) that raises "ImportError: cannot load module more than once per process" when its C extension is unloaded and re-imported. cleanup_loaded_modules() drops every module that is not in KEEP_MODULES, so an app that imports numpy before ddtrace.auto and again afterwards crashes on the second import. Add numpy to KEEP_MODULES so it survives cleanup. This is the same handling already in place for google.protobuf (PR DataDog#6346), whose upb backend has the same reload restriction. Fixes DataDog#18276 Signed-off-by: arpitjain099 <arpitjain099@gmail.com>
…a specific Apply the suggested release-note text so it no longer references internal module-cleanup handling, and change the bare test noqa comments to noqa: F401 (unused-import) so they suppress a specific rule. Signed-off-by: Arpit Jain <arpitjain099@gmail.com>
|
Thanks for the review, @KowalskiThomas. I've applied your suggested release-note wording so it no longer mentions the internal module-cleanup handling, and made the test noqa comments specific to |
This comment has been minimized.
This comment has been minimized.
emmettbutler
left a comment
There was a problem hiding this comment.
I think the risk is sufficiently low. Thanks for the contribution.
|
Just pushed the latest version of your branch to run on the internal CI. ⌛ |
|
@arpitjain099 for some reason, the internal CI isn't passing on your commit. Can you please merge |
|
Done, merged main into the branch (07f917b). Should be ready for you to sync into your replica and re-run CI. Thanks! |
|
@arpitjain099 may I ask a few more details about your current setup, please? In particular I'm interested to understand why numpy would be loaded before |
|
Thanks @P403n1x87, fair question. Importing That said, I hear the maintenance concern. If the unconditional keep is the sticking point, I'm happy to narrow it, for example only skipping cleanup for numpy when it was already imported at instrumentation time, or gating it behind whatever check you'd want for native modules generally. Happy to go whichever way fits how you want to maintain this. |
Description
When numpy 2.4+ is imported both before and after
ddtrace.auto, the second import crashes withImportError: cannot load module more than once per process. numpy 2.4 added a guard (numpy/numpy#29030) that refuses to re-initialize its C extension, andcleanup_loaded_modules()drops numpy fromsys.modulesbecause it is not inKEEP_MODULES, so re-importing it afterwards trips that guard. This adds numpy toKEEP_MODULES, which is the same fix already applied togoogle.protobufin #6346 for the identical reload restriction. Fixes #18276.Testing
Added
test_numpy_not_unloadedintests/internal/test_auto.py(guarded so it is a no-op when numpy is not installed) that asserts numpy stays insys.modulesafterddtrace.auto. I could not run the full native build locally, so I verified this by reading the cleanup logic and matching the existinggoogle.protobufprecedent, and I am relying on CI to exercise the test.Risks
Low. The change only keeps numpy and its submodules from being unloaded during startup cleanup, mirroring how
google.protobufis already handled.Additional Notes
Thanks to the reporter for the precise diagnosis and for pointing at the
google.protobuffix.