Repository navigation
Hold back post import hooks registered during an import in another thread - #372
Open
GrahamDumpleton wants to merge 1 commit into
Open
GrahamDumpleton wants to merge 1 commit into
GrahamDumpleton wants to merge 1 commit into
Conversation
…read. Python adds a module to sys.modules before executing its body, so a post import hook registered from one thread while another thread was still importing the target module was called immediately with the partially initialized module. This happened even when wrapt was already watching that import and would itself be notified once the module body completed. The wrapt loader now records which thread is executing the module body. A hook registered from a different thread during that time is queued and called by the importing thread once the body has completed, and remains registered if the import fails. A hook registered from the importing thread itself, as the result of a circular import, is still called immediately with the partially initialized module, matching what an import statement returns in that situation. This narrows rather than eliminates the problem. Python adds the module to sys.modules slightly before the body starts executing, and a hook registered from another thread in that brief interval is still called immediately. Nor can anything be done when wrapt was not already watching the import. The documentation now explains when a hook can see a partially initialized module, recommends registering post import hooks from the main thread before any other threads are started, and describes how to write hooks which tolerate a partially initialized module. Addresses part of #366. See also #367.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Addresses part of #366, raised by x42005e1f. Thanks also to Rutvik552k for #367, which explored a fix and was the basis for the threaded regression test.
This PR does not try to fix everything raised in #366. It covers only the part that can feasibly be addressed without relying on CPython internals, and documents the rest.
Background
Python adds a module to
sys.modulesbefore executing its body.wrapt.register_post_import_hook()treats a module present insys.modulesas already imported, and so calls the hook immediately. That means a hook can be passed a partially initialized module in two situations:What changes
importstatement returns in that situation. Deferring it only when wrapt happens to be watching would make the same code behave differently depending on unrelated registrations elsewhere.docs/monkey.rstcovers both situations.importer.pyabout how registered hooks are tracked is corrected.The tracking uses wrapt's own state under the existing lock. It doesn't use the private
ModuleSpec._initializingattribute.Limitations
This narrows the concurrent import problem but does not eliminate it:
sys.modulesbefore callingexec_module()on the wrapt loader, so for a moment the empty module is present but not yet marked as executing. A hook registered from another thread in that window is still called immediately, with a module whose body hasn't run at all. Setting the marker earlier, fromcreate_module(), would close the window for a normal import. However, loaders such asimportlib.util.LazyLoadercallcreate_module()and then delayexec_module(), possibly forever, which would leave the marker set.These limitations are why the documentation now recommends registering hooks from the main thread before starting other threads.
Behaviour change
A hook that is held back now runs in the importing thread rather than the registering thread. An exception it raises causes the import to fail in the importing thread. Hooks registered before the module was imported already behaved this way.
Testing
tests/core/test_post_import_hooks.pycover:just testpassed across 3.10 to 3.15, 3.14t and 3.15t, in all three C extension modes.just test-mypy-version 3.13passed, and the docs build without warnings.