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

module can set an exception in tp_clear #77895

Closed
serhiy-storchaka opened this issue May 31, 2018 · 6 comments
Closed

module can set an exception in tp_clear #77895

serhiy-storchaka opened this issue May 31, 2018 · 6 comments
Labels
3.7 (EOL) end of life 3.8 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-crash A hard crash of the interpreter, possibly with a core dump

Comments

@serhiy-storchaka
Copy link
Member

BPO 33714
Nosy @vstinner, @encukou, @serhiy-storchaka, @miss-islington, @AWhetter
PRs
  • bpo-33714: Output an exception raised in module's m_clear(). #16592
  • Files
  • breaky.c: Module that raises an exception in tp_clear
  • 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 2019-10-08.10:47:11.099>
    created_at = <Date 2018-05-31.12:21:45.371>
    labels = ['interpreter-core', '3.7', '3.8', 'type-crash']
    title = 'module can set an exception in tp_clear'
    updated_at = <Date 2019-10-08.10:47:11.097>
    user = 'https://github.com/serhiy-storchaka'

    bugs.python.org fields:

    activity = <Date 2019-10-08.10:47:11.097>
    actor = 'petr.viktorin'
    assignee = 'none'
    closed = True
    closed_date = <Date 2019-10-08.10:47:11.099>
    closer = 'petr.viktorin'
    components = ['Interpreter Core']
    creation = <Date 2018-05-31.12:21:45.371>
    creator = 'serhiy.storchaka'
    dependencies = []
    files = ['48642']
    hgrepos = []
    issue_num = 33714
    keywords = ['patch']
    message_count = 6.0
    messages = ['318289', '325075', '353971', '353980', '354182', '354184']
    nosy_count = 5.0
    nosy_names = ['vstinner', 'petr.viktorin', 'serhiy.storchaka', 'miss-islington', 'AWhetter']
    pr_nums = ['16592']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'crash'
    url = 'https://bugs.python.org/issue33714'
    versions = ['Python 2.7', 'Python 3.6', 'Python 3.7', 'Python 3.8']

    @serhiy-storchaka
    Copy link
    Member Author

    The tp_clear handler of the module object calls a custom clear function if the PyModuleDef.m_clear field is set. This function can set an exception which will be leaked to the garbage collector. An exception in tp_clear is not expected and caused a crash in the garbage collector. In the master branch it will cause just writing a traceback to stderr (see bpo-33622), but in any case it would be better to handle the failure locally in the module's tp_clear.

    @serhiy-storchaka serhiy-storchaka added 3.7 (EOL) end of life 3.8 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-crash A hard crash of the interpreter, possibly with a core dump labels May 31, 2018
    @encukou
    Copy link
    Member

    encukou commented Sep 11, 2018

    I'm not sure what tp_clear should do in this situation.
    Other than propagating the exception to the GC, the most reasonable behavior seems to be to write the exception to stderr and ignore it -- but I think having the GC do that would be more robust.

    IOW, I think raising an exception from tp_clear is reasonable, and if that caused (causes?) a crash, it's a bug.

    @AWhetter
    Copy link
    Mannequin

    AWhetter mannequin commented Oct 4, 2019

    I've attached a reproduction case. Here's the setup.py that I used:

    from distutils.core import Extension, setup
    
    extension = Extension("breaky", ["breaky.c"])
    
    setup(
        name="tp_clear",
        version="0.1.0",
        ext_modules=[extension],
    )
    

    which I built and ran with:

    python setup.py build_ext --inplace
    python -c "import breaky, gc; a = breaky.Breaky(); a = 1"
    

    @AWhetter
    Copy link
    Mannequin

    AWhetter mannequin commented Oct 5, 2019

    I've just realised that this issue was specific to tp_clear on a module and not on objects. tp_clear on modules has already been fixed in https://bugs.python.org/issue33622.
    I think this is closable.

    @miss-islington
    Copy link
    Contributor

    New changeset d7c3873 by Miss Islington (bot) (Serhiy Storchaka) in branch 'master':
    bpo-33714: Output an exception raised in module's m_clear(). (GH-16592)
    d7c3873

    @encukou
    Copy link
    Member

    encukou commented Oct 8, 2019

    Indeed, it can be closed.
    Thanks Serhiy for the better error message!

    @encukou encukou closed this as completed Oct 8, 2019
    @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
    3.7 (EOL) end of life 3.8 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-crash A hard crash of the interpreter, possibly with a core dump
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants