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

Python crashes when a warning is emitted during shutdown #63641

Closed
vstinner opened this issue Oct 29, 2013 · 6 comments
Closed

Python crashes when a warning is emitted during shutdown #63641

vstinner opened this issue Oct 29, 2013 · 6 comments
Labels
type-crash A hard crash of the interpreter, possibly with a core dump

Comments

@vstinner
Copy link
Member

BPO 19442
Nosy @vstinner
Files
  • warn_shutdown.py
  • 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 = None
    closed_at = <Date 2013-10-31.23:55:45.230>
    created_at = <Date 2013-10-29.23:31:53.167>
    labels = ['type-crash']
    title = 'Python crashes when a warning is emitted during shutdown'
    updated_at = <Date 2013-10-31.23:55:45.229>
    user = 'https://github.com/vstinner'

    bugs.python.org fields:

    activity = <Date 2013-10-31.23:55:45.229>
    actor = 'python-dev'
    assignee = 'none'
    closed = True
    closed_date = <Date 2013-10-31.23:55:45.230>
    closer = 'python-dev'
    components = []
    creation = <Date 2013-10-29.23:31:53.167>
    creator = 'vstinner'
    dependencies = []
    files = ['32418']
    hgrepos = []
    issue_num = 19442
    keywords = []
    message_count = 6.0
    messages = ['201695', '201696', '201699', '201700', '201861', '201862']
    nosy_count = 3.0
    nosy_names = ['vstinner', 'Arfrever', 'python-dev']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'crash'
    url = 'https://bugs.python.org/issue19442'
    versions = ['Python 3.4']

    @vstinner
    Copy link
    Member Author

    Because of a bug, warnings were not emitted during Python shutdown. I fixed the bug in issue bpo-19424, and now Python crashs when a warning is emitted during shutdown.

    See also issue bpo-19421: "FileIO destructor imports indirectly the io module at exit".

    The warnings module uses globals()['__name__'] to get the name of the current module. The bug was the during Python shutdown, the module is None and _PyUnicode_AsString(None) was raising an exception (in setup_context() of _warnings.c) and so the warning was not emitted. For example, FileIO destructor emits a warning when a unclosed file is destroyed. The destructor removes the exception raised the warning module.

    @vstinner vstinner added the type-crash A hard crash of the interpreter, possibly with a core dump label Oct 29, 2013
    @Arfrever Arfrever mannequin changed the title Python crashs when a warning is emitted during shutdown Python crashes when a warning is emitted during shutdown Oct 29, 2013
    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Oct 29, 2013

    New changeset 1787277915e9 by Victor Stinner in branch 'default':
    Issue bpo-19442: Fix warnings emitted during Python shutdown
    http://hg.python.org/cpython/rev/1787277915e9

    @vstinner
    Copy link
    Member Author

    The changeset 1787277915e9 is closer to a workaround than a real fix:

    + if (module != Py_None) {
    + res = warn_explicit(category, message, filename, lineno, module, registry,
    + NULL);
    + }
    + else {
    + /* FIXME: emitting warnings at exit does crash Python */
    + res = Py_None;
    + Py_INCREF(res);
    + }

    @vstinner
    Copy link
    Member Author

    warn_shutdown.py: example of script emitting warning at shutdown. Depending on the timing, the warning is shown or not (replace "if 0:" with "if 1:" to always display the warning). This example is a simplified example of the original crasher: test_threading.test_4_daemon_threads().

    @vstinner
    Copy link
    Member Author

    warn_shutdown.py does not emit warnings because the thread states are destroyed too late, when the modules have been destroyed.

    I opened a new issue bpo-19466 "Clear state of threads earlier in Python shutdown" which would permit to emit properly warnings, at least for the specific case of warn_shutdown.py.

    The changeset 1787277915e9 is closer to a workaround than a real fix: ...

    If the module is None, it's too late: modules have been destroyed, so it's safer to not call warn_explicit() and do nothing.

    I tested to fail with an assertion error if a warning is emitted too late (ie. when module is None). test_threading.test_4_daemon_threads() does fail in this case. Using the patch attached to bpo-19466, the full test suite pass correctly.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Oct 31, 2013

    New changeset 13a05ed33cf7 by Victor Stinner in branch 'default':
    Close bpo-19442: warn_explicit() does nothing when called late during Python shutdown
    http://hg.python.org/cpython/rev/13a05ed33cf7

    @python-dev python-dev mannequin closed this as completed Oct 31, 2013
    @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
    type-crash A hard crash of the interpreter, possibly with a core dump
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant