Skip to content

undefined behavior and crashes in case of a bad sys.modules #75585

Closed

Description

@orenmn
mannequin
BPO 31404
Nosy @brettcannon, @ncoghlan, @ericsnowcurrently, @serhiy-storchaka, @orenmn
PRs
  • bpo-31404: Revert "remove modules from Py_InterpreterState (#1638)" #3565
  • bpo-28411: Remove PyInterpreterState.modules. #3606
  • 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/ericsnowcurrently'
    closed_at = <Date 2017-09-15.22:50:59.825>
    created_at = <Date 2017-09-09.17:36:25.988>
    labels = ['interpreter-core', '3.7', 'type-crash']
    title = 'undefined behavior and crashes in case of a bad sys.modules'
    updated_at = <Date 2017-09-15.22:51:14.239>
    user = 'https://github.com/orenmn'

    bugs.python.org fields:

    activity = <Date 2017-09-15.22:51:14.239>
    actor = 'eric.snow'
    assignee = 'eric.snow'
    closed = True
    closed_date = <Date 2017-09-15.22:50:59.825>
    closer = 'eric.snow'
    components = ['Interpreter Core']
    creation = <Date 2017-09-09.17:36:25.988>
    creator = 'Oren Milman'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 31404
    keywords = ['patch']
    message_count = 7.0
    messages = ['301783', '301926', '301936', '301949', '302001', '302027', '302147']
    nosy_count = 5.0
    nosy_names = ['brett.cannon', 'ncoghlan', 'eric.snow', 'serhiy.storchaka', 'Oren Milman']
    pr_nums = ['3565', '3606']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'crash'
    url = 'https://bugs.python.org/issue31404'
    versions = ['Python 3.7']

    Activity

    1. orenmn commented on Sep 9, 2017

      orenmnmannequin
      MannequinAuthor

      at least on my Windows, the following code:

      import sys
      sys.modules = []
      • when run interactively, causes weird behavior, e.g. exit() doesn't exit the
        interpreter, and print() doesn't print.
        then, pressing Ctrl+C causes 'Assertion failed: !PyErr_Occurred(), file ..\Objects\call.c, line 803'

      • when run as a script, causes PyImport_Cleanup() to raise a negative ref count
        Fatal Python error.

        (this is because PyImport_Cleanup() (in Python/import.c) assumes that
        PyImport_GetModuleDict() returned a dict.)

      IIUC, this bug was introduced in #1638
      (which resolved bpo-28411).

    2. added
      interpreter-core(Objects, Python, Grammar, and Parser dirs)
      type-crashA hard crash of the interpreter, possibly with a core dump
      on Sep 9, 2017
    3. ericsnowcurrently commented on Sep 11, 2017

      @ericsnowcurrently
      Member

      I'm looking into this.

    4. ericsnowcurrently commented on Sep 12, 2017

      @ericsnowcurrently
      Member

      This is pretty messy. :( Ideally we would disallow setting sys.modules to anything except a dict (or perhaps any mapping). However, we don't have that option currently (see https://github.com/ericsnowcurrently/cpython/tree/sys-module). In the meantime we have to fix up all the places that are expecting a mapping.

      Part of the problem here is that a number of places fail silently, as evidenced by the failures Oren pointed out...

    5. serhiy-storchaka commented on Sep 12, 2017

      @serhiy-storchaka
      Member

      I haven't finished reviewing PR 1638. I'm not sure that this change is worth. It breaks a code that assumes that sys.module is a dict, and I afraid it slows down importing. Maybe revert this change until resolving all problems?

    6. ericsnowcurrently commented on Sep 12, 2017

      @ericsnowcurrently
      Member

      I haven't finished reviewing PR 1638. I'm not sure that this change
      is worth. It breaks a code that assumes that sys.module is a dict,
      and I afraid it slows down importing. Maybe revert this change until
      resolving all problems?

      Yeah, I'm leaning that way myself. I'll take another look later today or tomorrow. FWIW, the likelihood of this causing problems to actual users is extremely low, so the urgency to fix this isn't high. Still, I don't want this to linger.

      As I said before, ideally we would prevent non-mappings from getting assigned to sys.modules. One alternative would be to introduce sys.__modules__ and fall back to it if sys.modules is invalid.

    7. ncoghlan commented on Sep 13, 2017

      @ncoghlan
      Contributor

      Introducing sys.__modules__ doesn't solve the problem, since you'd be able to recreate the problems Oren reports just by messing with that as well as with sys.modules.

      And we try reasonable hard to protect users from completely breaking the interpreter just by assigning nonsense to sys module attributes.

      So my recommendation would be to revert bpo-28411, and make it clearer that the purpose of the interpreter state attribute is to defend against nonsensical rebinding of sys.modules, and that it can't be removed unless/until the sys module is switched to a custom type that enforces type checks on some of its attributes.

    8. ericsnowcurrently commented on Sep 14, 2017

      @ericsnowcurrently
      Member

      New changeset 93c92f7 by Eric Snow in branch 'master':
      bpo-31404: Revert "remove modules from Py_InterpreterState (bpo-1638)" (bpo-3565)
      93c92f7

    9. 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.7 (EOL)end of lifeinterpreter-core(Objects, Python, Grammar, and Parser dirs)type-crashA hard crash of the interpreter, possibly with a core dump

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions