Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't "wipe" builtins at shutdown #63454

Closed
serhiy-storchaka opened this issue Oct 13, 2013 · 21 comments
Closed

Don't "wipe" builtins at shutdown #63454

serhiy-storchaka opened this issue Oct 13, 2013 · 21 comments
Assignees
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@serhiy-storchaka
Copy link
Member

BPO 19255
Nosy @brettcannon, @ncoghlan, @pitrou, @vstinner, @tiran, @ericsnowcurrently, @serhiy-storchaka
Files
  • 19255.patch
  • builtins_cleanup.patch
  • modules_cleanup.patch
  • modules_cleanup-3.3.patch
  • issue19255-coverity.patch
  • 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/serhiy-storchaka'
    closed_at = <Date 2014-03-20.08:56:33.786>
    created_at = <Date 2013-10-13.21:34:15.295>
    labels = ['interpreter-core', 'type-bug']
    title = 'Don\'t "wipe" builtins at shutdown'
    updated_at = <Date 2014-03-20.08:56:33.785>
    user = 'https://github.com/serhiy-storchaka'

    bugs.python.org fields:

    activity = <Date 2014-03-20.08:56:33.785>
    actor = 'serhiy.storchaka'
    assignee = 'serhiy.storchaka'
    closed = True
    closed_date = <Date 2014-03-20.08:56:33.786>
    closer = 'serhiy.storchaka'
    components = ['Interpreter Core']
    creation = <Date 2013-10-13.21:34:15.295>
    creator = 'serhiy.storchaka'
    dependencies = []
    files = ['33305', '33350', '34006', '34022', '34048']
    hgrepos = []
    issue_num = 19255
    keywords = ['patch']
    message_count = 21.0
    messages = ['199810', '199816', '207259', '207477', '207478', '207581', '210739', '210845', '210847', '210848', '210849', '210850', '210851', '210973', '211003', '211014', '211023', '211025', '211030', '211059', '213818']
    nosy_count = 10.0
    nosy_names = ['brett.cannon', 'ncoghlan', 'pitrou', 'vstinner', 'christian.heimes', 'Arfrever', 'python-dev', 'gennad', 'eric.snow', 'serhiy.storchaka']
    pr_nums = []
    priority = 'critical'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue19255'
    versions = ['Python 3.4']

    @serhiy-storchaka
    Copy link
    Member Author

    Many __del__ methods expect that builtins are existing at the time of executing. But when the object survives until the builtins module is wiped, __del__ will fail with unexpected AttributeError (see bpo-19021 for example). We can change __del__'s in the stdlib to not use non-cached builtins, but third party code will be broken.

    Perhaps we should special case shutdown of the builtins module. E.g. reset it to initial value (all initial members of the builtins module are immutable and can't create resource loops) instead wiping.

    @serhiy-storchaka serhiy-storchaka added interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error labels Oct 13, 2013
    @pitrou
    Copy link
    Member

    pitrou commented Oct 13, 2013

    Perhaps we should special case shutdown of the builtins module. E.g.
    reset it to initial value (all initial members of the builtins module
    are immutable and can't create resource loops) instead wiping.

    This sounds like a reasonable approach.

    @gennad
    Copy link
    Mannequin

    gennad mannequin commented Jan 4, 2014

    I'm not 100% sure that this is what intended, but probably this patch can fix it? Please review it.

    @pitrou
    Copy link
    Member

    pitrou commented Jan 6, 2014

    Thanks for the patch, Gennadiy. Two comments:

    1. I don't think re-doing the whole initialization like that is ok. It's too brutal and may also not work as you want.
    2. To validate your changes, you should add a unit test anyway (in test_builtin perhaps).

    @pitrou
    Copy link
    Member

    pitrou commented Jan 6, 2014

    Also, since such changes are subtle and error-prone, I think it's better to defer until 3.5 now that 3.4b2 has been released. Serhiy, what do you think?

    @pitrou
    Copy link
    Member

    pitrou commented Jan 7, 2014

    Here is simpler patch with a test.

    @serhiy-storchaka
    Copy link
    Member Author

    Unfortunately this patch doesn't fix bpo-19021. Popen.__del__ is called when whiping the idlelib.rpc module which was collected in a weaklist. But the builtins module also was collected in a weaklist and wiped before idlelib.rpc.

    Here is revised patch which fixes bpo-19021. It defers wiping of the builtins and sys modules (this partially restores 3.3 algorithm) and fixes other related issues (some of these fixes should be backported to older versions).

    @serhiy-storchaka
    Copy link
    Member Author

    Here is backported to 3.3 patch. It includes:

    1. Operates not with sys.modules['builtins'].__dict__ and sys.modules['sys'].__dict__, but with cached interp->builtins and interp->sysdict, because sys.modules['builtins'] and sys.modules['sys'] can be changed, but builtins and standard streams for builtins are retrieved from cached interp->builtins and interp->sysdict.

    2. interp->builtins is restored to saved copy.

    3. Backported test. Actually it passed with current code, but can prevent future bugs.

    I'm not sure about (2), but may be (1) should be applied.

    @pitrou
    Copy link
    Member

    pitrou commented Feb 10, 2014

    I'm not convinced this should be backported. It's always a bit risky to change interpreter shutdown, and it fixes an issue which has been well-known (and worked around) for many years.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Feb 10, 2014

    New changeset 6a1711c96fa6 by Serhiy Storchaka in branch 'default':
    Issue bpo-19255: The builtins module is restored to initial value before
    http://hg.python.org/cpython/rev/6a1711c96fa6

    @serhiy-storchaka
    Copy link
    Member Author

    I agree that it is risky and don't insist. We can return to this patch when encounter more serious bug.

    Last commit breaks test_create_at_shutdown_without_encoding in test_io for Python implementation of io.

    This happens because Python implementation of TextIOWrapper.__init__() imports the locale module if encoding is not specified. Then _find_spec() in Lib/importlib/_bootstrap.py iterates sys.meta_path, but "meta_path" is one of names cleared in sys in PyImport_Cleanup().

    @pitrou
    Copy link
    Member

    pitrou commented Feb 10, 2014

    This happens because Python implementation of TextIOWrapper.__init__()
    imports the locale module if encoding is not specified. Then
    _find_spec() in Lib/importlib/_bootstrap.py iterates sys.meta_path,
    but "meta_path" is one of names cleared in sys in PyImport_Cleanup().

    Then perhaps PyImport_Cleanup should call _PyImportHooks_Init instead of
    setting import-related attributes to None.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Feb 10, 2014

    New changeset fa160c8145e5 by Serhiy Storchaka in branch 'default':
    Temporary silence test broken by bpo-19255.
    http://hg.python.org/cpython/rev/fa160c8145e5

    @vstinner
    Copy link
    Member

    test_cleanup() of test_builtin fails: I opened issue bpo-20599 to track this bug.

    @pitrou
    Copy link
    Member

    pitrou commented Feb 11, 2014

    Turns out call _PyImportHooks_Init is not enough, see bpo-20602 and bpo-20603.

    @tiran
    Copy link
    Member

    tiran commented Feb 11, 2014

    Your checkin 6a1711c96fa6 caused a Coverity complain:

    ** CID 1171506: Unchecked return value (CHECKED_RETURN)
    /Objects/moduleobject.c: 352 in _PyModule_ClearDict()
    /Objects/moduleobject.c: 333 in _PyModule_ClearDict()

    ________________________________________________________________________________________________________
    *** CID 1171506:  Unchecked return value  (CHECKED_RETURN)
    /Objects/moduleobject.c: 352 in _PyModule_ClearDict()
    346                         const char *s = _PyUnicode_AsString(key);
    347                         if (s != NULL)
    348                             PySys_WriteStderr("#   clear[2] %s\n", s);
    349                         else
    350                             PyErr_Clear();
    351                     }
    >>>     CID 1171506:  Unchecked return value  (CHECKED_RETURN)
    >>>     No check of the return value of "PyDict_SetItem(d, key, &_Py_NoneStruct)".
    352                     PyDict_SetItem(d, key, Py_None);
    353                 }
    354             }
    355         }
    356     
    357         /* Note: we leave __builtins__ in place, so that destructors
    /Objects/moduleobject.c: 333 in _PyModule_ClearDict()
    327                         const char *s = _PyUnicode_AsString(key);
    328                         if (s != NULL)
    329                             PySys_WriteStderr("#   clear[1] %s\n", s);
    330                         else
    331                             PyErr_Clear();
    332                     }
    >>>     CID 1171506:  Unchecked return value  (CHECKED_RETURN)
    >>>     No check of the return value of "PyDict_SetItem(d, key, &_Py_NoneStruct)".
    333                     PyDict_SetItem(d, key, Py_None);
    334                 }
    335             }
    336         }
    337     
    338         /* Next, clear all names except for __builtins__ */

    @tiran
    Copy link
    Member

    tiran commented Feb 11, 2014

    The two problematic code paths were marked as "intentional" and "ignore" in the past. There is no point in reporting errors that late in the shutdown phase. Also the code paths are very unlikely to trigger an error.

    @serhiy-storchaka
    Copy link
    Member Author

    Your checkin 6a1711c96fa6 caused a Coverity complain:

    ** CID 1171506: Unchecked return value (CHECKED_RETURN)
    /Objects/moduleobject.c: 352 in _PyModule_ClearDict()
    /Objects/moduleobject.c: 333 in _PyModule_ClearDict()

    Actually this was in old code. May now the code is just simpler for analysis by Coverity.

    What can we do with this? Only call PyErr_Clear() as for other errors.

    @tiran
    Copy link
    Member

    tiran commented Feb 11, 2014

    It's fine. We don't *want* to check the error and fail here.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Feb 12, 2014

    New changeset c6e7ba7eee88 by Serhiy Storchaka in branch '2.7':
    Issue bpo-19255: Clear error after failed PyDict_SetItem() on shutdown.
    http://hg.python.org/cpython/rev/c6e7ba7eee88

    New changeset ffc4f03adcac by Serhiy Storchaka in branch '3.3':
    Issue bpo-19255: Clear error after failed PyDict_SetItem() on shutdown.
    http://hg.python.org/cpython/rev/ffc4f03adcac

    New changeset efaf12106d68 by Serhiy Storchaka in branch 'default':
    Issue bpo-19255: Clear error after failed PyDict_SetItem() on shutdown.
    http://hg.python.org/cpython/rev/efaf12106d68

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Mar 17, 2014

    New changeset 5d3b9862f1bc by Serhiy Storchaka in branch '3.4':
    Issue bpo-19255: The builtins module is restored to initial value before
    http://hg.python.org/cpython/rev/5d3b9862f1bc

    New changeset a0bc45a50e43 by Serhiy Storchaka in branch '3.4':
    Temporary silence test broken by bpo-19255.
    http://hg.python.org/cpython/rev/a0bc45a50e43

    New changeset af8f856db136 by Serhiy Storchaka in branch '3.4':
    Issue bpo-19255: Clear error after failed PyDict_SetItem() on shutdown.
    http://hg.python.org/cpython/rev/af8f856db136

    @serhiy-storchaka serhiy-storchaka self-assigned this Mar 20, 2014
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    4 participants