Skip to content

Hold back post import hooks registered during an import in another thread - #372

Open
GrahamDumpleton wants to merge 1 commit into
developfrom
bugfix/parallel-import-hooks
Open

GrahamDumpleton wants to merge 1 commit into
developfrom
bugfix/parallel-import-hooks

Conversation

@GrahamDumpleton

Copy link
Copy Markdown
Owner

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.modules before executing its body. wrapt.register_post_import_hook() treats a module present in sys.modules as already imported, and so calls the hook immediately. That means a hook can be passed a partially initialized module in two situations:

  • Circular import. The body of the target module directly or indirectly imports the module that registers the hook.
  • Concurrent import. The hook is registered from one thread while another thread is still executing the body of the target module.

What changes

  • Concurrent import while wrapt is watching. If a hook was already registered for the module when its import began, 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 the importing thread calls it once the body has completed. If the import fails, the hook stays registered for a later import. Previously it was called immediately, even though wrapt was about to be notified of the completed import anyway.
  • Circular import: unchanged, deliberately. A hook registered from the importing thread itself is still called immediately with the partially initialized module. That is the same as what an import statement 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.
  • Documentation.
    • A new "Hooks and partially initialized modules" section in docs/monkey.rst covers both situations.
    • The docs now recommend registering post import hooks from the main thread before any other threads are started, and explain how to write hooks that tolerate a partially initialized module where that isn't possible.
    • The docstrings and the API summary are updated to match.
    • A stale comment in importer.py about 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._initializing attribute.

Limitations

This narrows the concurrent import problem but does not eliminate it:

  • The window before the body starts. Python inserts the module into sys.modules before calling exec_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, from create_module(), would close the window for a normal import. However, loaders such as importlib.util.LazyLoader call create_module() and then delay exec_module(), possibly forever, which would leave the marker set.
  • Imports wrapt isn't watching. If no hook was registered for the module before its import began, wrapt plays no part in that import and has no reliable way to know when it completes. The hook is still called immediately with the partially initialized module.
  • Circular imports. As above, these are unchanged by design.

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

  • New tests in tests/core/test_post_import_hooks.py cover:
    • a hook registered from another thread while the body is executing;
    • a failed import followed by a successful one;
    • a thread started by the module body that registers a hook, which must not deadlock;
    • the unchanged circular import behaviour.
  • Against the previous importer, the three threaded tests fail.
  • just test passed across 3.10 to 3.15, 3.14t and 3.15t, in all three C extension modes.
  • just test-mypy-version 3.13 passed, and the docs build without warnings.

…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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant