Skip to content

PyImport_GetModule() can return partially-initialized module #80124

Closed

Description

@pitrou
BPO 35943
Nosy @brettcannon, @atsuoishimoto, @ncoghlan, @pitrou, @vstinner, @ericsnowcurrently, @pganssle, @pablogsal, @nanjekyejoannah, @cebtenzzre
PRs
  • bpo-35943: PyImport_GetModule() can return partially-initialized module #15057
  • Files
  • importerror-sample.tgz
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/nanjekyejoannah'
    closed_at = <Date 2021-03-16.16:17:49.860>
    created_at = <Date 2019-02-08.17:13:37.200>
    labels = ['interpreter-core', 'type-bug', '3.9', '3.10']
    title = 'PyImport_GetModule() can return partially-initialized module'
    updated_at = <Date 2021-03-16.16:17:49.859>
    user = 'https://github.com/pitrou'

    bugs.python.org fields:

    activity = <Date 2021-03-16.16:17:49.859>
    actor = 'pitrou'
    assignee = 'nanjekyejoannah'
    closed = True
    closed_date = <Date 2021-03-16.16:17:49.860>
    closer = 'pitrou'
    components = ['Interpreter Core']
    creation = <Date 2019-02-08.17:13:37.200>
    creator = 'pitrou'
    dependencies = []
    files = ['49537']
    hgrepos = []
    issue_num = 35943
    keywords = ['patch']
    message_count = 19.0
    messages = ['335097', '335100', '351847', '356920', '356980', '357201', '360056', '360075', '360399', '362300', '362303', '362329', '362347', '379441', '383433', '388846', '388847', '388849', '388857']
    nosy_count = 13.0
    nosy_names = ['brett.cannon', 'ishimoto', 'gjb1002', 'ncoghlan', 'pitrou', 'vstinner', 'eric.snow', 'Big Stone', 'p-ganssle', 'pablogsal', 'nanjekyejoannah', 'Valentyn Tymofieiev', 'cebtenzzre']
    pr_nums = ['15057']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue35943'
    versions = ['Python 3.9', 'Python 3.10']

    Activity

    1. pitrou commented on Feb 8, 2019

      @pitrou
      MemberAuthor

      PyImport_GetModule() returns whatever is in sys.modules, even if the module is still importing and therefore only partially initialized.

      One possibility is to reuse the optimization already done in PyImport_ImportModuleLevelObject():

              /* Optimization: only call _bootstrap._lock_unlock_module() if
                 __spec__._initializing is true.
                 NOTE: because of this, initializing must be set *before*
                 stuffing the new module in sys.modules.
               */
              spec = _PyObject_GetAttrId(mod, &PyId___spec__);
              if (_PyModuleSpec_IsInitializing(spec)) {
                  PyObject *value = _PyObject_CallMethodIdObjArgs(interp->importlib,
                                                  &PyId__lock_unlock_module, abs_name,
                                                  NULL);
                  if (value == NULL) {
                      Py_DECREF(spec);
                      goto error;
                  }
                  Py_DECREF(value);
              }
              Py_XDECREF(spec);

      Issue originally mentioned in bpo-34572.

    2. added
      3.7 (EOL)
      3.8 (EOL)
      interpreter-core
      type-bug
      on Feb 8, 2019
    3. ericsnowcurrently commented on Feb 8, 2019

      @ericsnowcurrently
      Member

      Yeah, that makes sense.

    4. brettcannon commented on Sep 11, 2019

      @brettcannon
      Member

      New changeset 37c2220 by Brett Cannon (Joannah Nanjekye) in branch 'master':
      bpo-35943: Prevent PyImport_GetModule() from returning a partially-initialized module (GH-15057)
      37c2220

    5. ValentynTymofieiev commented on Nov 18, 2019

      ValentynTymofieievmannequin
      Mannequin

      Do we plan to backport the change by nanjekyejoannah to 3.7 branch?

    6. brettcannon commented on Nov 19, 2019

      @brettcannon
      Member

      I've assigned this to Joannah to decide if she wants to backport this.

    7. ValentynTymofieiev commented on Nov 21, 2019

      ValentynTymofieievmannequin
      Mannequin
    8. nanjekyejoannah commented on Jan 15, 2020

      @nanjekyejoannah
      Contributor

      The changes required to successfully do this backport are many and affect critical areas. I am not in a hurry to do this. If anyone else wants to take this up quickly, please do.

    9. vstinner commented on Jan 15, 2020

      @vstinner
      Member

      The changes required to successfully do this backport are many and affect critical areas. I am not in a hurry to do this. If anyone else wants to take this up quickly, please do.

      Do you mean that there is a risk that the backport introduces a regression in another part of the code? If yes, I would suggest to not backport the change to *stable* branches.

      People survived with bug. Do you really *have to* backport the fix?

      Note: this issue is closed. If you consider to backport it, I suggest to reopen the issue.

    10. nanjekyejoannah commented on Jan 21, 2020

      @nanjekyejoannah
      Contributor

      Do you mean that there is a risk that the backport introduces a regression in another part of the code? If yes, I would suggest to not backport the change to *stable* branches.

      My worry are the many changes that are required to ceval to make this back port work. Not that I think we can not successfully backport things. we can.

    11. 2 remaining items

    12. ValentynTymofieiev commented on Feb 20, 2020

      ValentynTymofieievmannequin
      Mannequin

      @gjb1002: see also https://bugs.python.org/issue38884, which demonstrates that concurrent imports are not thread-safe on Python 3.

    13. gjb1002 commented on Feb 20, 2020

      gjb1002mannequin
      Mannequin

      @Valentyn Tymofieiev - true, and thanks for the tip, though the symptoms described there are somewhat different from what I'm observing. Also, my problem seems to be dependent on zipping the Python code, which that one isn't.

    14. atsuoishimoto commented on Oct 23, 2020

      atsuoishimotomannequin
      Mannequin

      After this fix, some functions like multiprocessing.Pool cannot be used in threaded code(https://bugs.python.org/issue41567).

      importerror-sample.tgz contains simplified code to reproduce the same error without multiprocessing module. Is this an expected behaviour of this change?

      Tested with Python 3.9.0/macOS 10.15.5.

    15. BigStone commented on Dec 20, 2020

      BigStonemannequin
      Mannequin

      Is this bug causing the Dask-Jupyterlab failure ?
      dask/distributed#4168

    16. pitrou commented on Mar 16, 2021

      @pitrou
      MemberAuthor
    17. pitrou commented on Mar 16, 2021

      @pitrou
      MemberAuthor

      Ok, going through other open issues including on third-party projects, I think these changes should unfortunately be reverted. The regressions produced are far from trivial and most developers seem at a loss how to fix them.

    18. added
      3.9 (EOL)
      3.10 (EOL)
      and removed
      3.7 (EOL)
      3.8 (EOL)
      on Mar 16, 2021
    19. reopened this on Mar 16, 2021
    20. pitrou commented on Mar 16, 2021

      @pitrou
      MemberAuthor

      After analysis, it may not need reversal. There is a simple logic error it seems. Will check.

    21. pitrou commented on Mar 16, 2021

      @pitrou
      MemberAuthor

      Created a new issue + fix in bpo-43517.

    22. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Labels

    3.10 (EOL)3.9 (EOL)interpreter-coretype-bug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions