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

Py_FatalError(): dump the list of extension modules #87089

Closed
vstinner opened this issue Jan 13, 2021 · 12 comments
Closed

Py_FatalError(): dump the list of extension modules #87089

vstinner opened this issue Jan 13, 2021 · 12 comments
Labels
3.10 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs)

Comments

@vstinner
Copy link
Member

BPO 42923
Nosy @vstinner, @corona10
PRs
  • bpo-42923: Dump extension modules on fatal error #24207
  • bpo-42923: Add Py_FatalError() test in test_capi #24240
  • bpo-42923: Py_FatalError() avoids fprintf() #24242
  • bpo-42923: _Py_DumpExtensionModules() ignores stdlib ext #24254
  • bpo-42955: Add sys.modules_names #24238
  • bpo-42923: Fix _Py_DumpExtensionModules() for NULL sysdict #25262
  • 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 2021-01-19.22:36:24.746>
    created_at = <Date 2021-01-13.10:50:13.084>
    labels = ['interpreter-core', '3.10']
    title = 'Py_FatalError(): dump the list of extension modules'
    updated_at = <Date 2021-04-07.21:13:01.803>
    user = 'https://github.com/vstinner'

    bugs.python.org fields:

    activity = <Date 2021-04-07.21:13:01.803>
    actor = 'vstinner'
    assignee = 'none'
    closed = True
    closed_date = <Date 2021-01-19.22:36:24.746>
    closer = 'vstinner'
    components = ['Interpreter Core']
    creation = <Date 2021-01-13.10:50:13.084>
    creator = 'vstinner'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 42923
    keywords = ['patch']
    message_count = 12.0
    messages = ['385013', '385016', '385020', '385161', '385206', '385207', '385221', '385222', '385303', '385306', '385403', '390475']
    nosy_count = 2.0
    nosy_names = ['vstinner', 'corona10']
    pr_nums = ['24207', '24240', '24242', '24254', '24238', '25262']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue42923'
    versions = ['Python 3.10']

    @vstinner
    Copy link
    Member Author

    When Python cannot report an error and its is not possible to recover from the error, Py_FatalError() displays an error message and exit immediately Python.

    It's common that Python crashes are coming from third party extension modules, rather than Python itself. Recent example: bpo-42891 crash reported in Python, but it was a bug in lsm-db third party extension module.

    I propose to enhance Py_FatalError() to attempt to dump the list of extension modules, to ease debugging.

    I'm working on a PR.

    @vstinner vstinner added 3.10 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) labels Jan 13, 2021
    @vstinner
    Copy link
    Member Author

    Example using PR 24207, the new part the "Extension modules:" list at the end:
    ---------------

    $ ./python -X faulthandler
    Python 3.10.0a4+ (heads/master-dirty:2396614b89, Jan 13 2021, 12:09:15) 
    >>> import ctypes
    >>> ctypes.string_at(0)
    Fatal Python error: Segmentation fault

    Current thread 0x00007f0aabd09740 (most recent call first):
    File "/home/vstinner/python/master/Lib/ctypes/init.py", line 517 in string_at
    File "<stdin>", line 1 in <module>

    Extension modules:

    • sys
    • builtins
    • _imp
    • _thread
    • _warnings
    • _weakref
    • _io
    • marshal
    • posix
    • time
    • faulthandler
    • _codecs
    • _signal
    • _abc
    • _stat
    • readline
    • atexit
    • itertools
    • _operator
    • _collections
    • _functools
    • _opcode
    • _sre
    • _locale
    • _ctypes
    • _struct
      ---------------

    Here you can see that the _ctypes module is loaded.

    @vstinner
    Copy link
    Member Author

    It's common that Python crashes are coming from third party extension modules, rather than Python itself. Recent example: bpo-42891 crash (...)

    Another recent example: bpo-42879 "python: Objects/abstract.c:155: PyObject_GetItem: Assertion `(item != NULL) ^ (PyErr_Occurred() != NULL)' failed." was a bug in pygame.

    @vstinner
    Copy link
    Member Author

    @vstinner
    Copy link
    Member Author

    New changeset e232025 by Victor Stinner in branch 'master':
    bpo-42923: Add Py_FatalError() test in test_capi (GH-24240)
    e232025

    @vstinner
    Copy link
    Member Author

    New changeset 314b878 by Victor Stinner in branch 'master':
    bpo-42923: Py_FatalError() avoids fprintf() (GH-24242)
    314b878

    @vstinner
    Copy link
    Member Author

    New changeset 250035d by Victor Stinner in branch 'master':
    bpo-42923: Dump extension modules on fatal error (GH-24207)
    250035d

    @vstinner
    Copy link
    Member Author

    I created https://bugs.python.org/issue42955 to add sys.modules_names tuple: names of stdlib modules. Once it will be merged, I will updated this PR to filter the list of modules (ignore stdlib modules).

    @vstinner
    Copy link
    Member Author

    New changeset 66f77ca by Victor Stinner in branch 'master':
    bpo-42923: _Py_DumpExtensionModules() ignores stdlib ext (GH-24254)
    66f77ca

    @vstinner
    Copy link
    Member Author

    More "real world" example using cinder which imports 220 modules (ctypes is used to simulate a crash):
    ---------------

    $ ./python -m venv env
    $ env/bin/python -m pip install wheel
    $ env/bin/python -m pip install cinder
    $ env/bin/python -X dev -c 'import cinder, ctypes, sys; print(f"{len(sys.modules)=}"); print(); ctypes.string_at(0)'
    (...)
    len(sys.modules)=220

    Fatal Python error: Segmentation fault

    Current thread 0x00007fc4a88e0740 (most recent call first):
    File "/home/vstinner/python/master/Lib/ctypes/init.py", line 517 in string_at
    File "<string>", line 1 in <module>

    Extension modules: greenlet._greenlet, __original_module__thread, __original_module_select, __original_module_time, _cffi_backend (total: 5)
    Erreur de segmentation (core dumped)
    ---------------

    cinder only uses 2 third party extension modules (on a total of 220 modules): greenlet and cffi.

    Note: __original_xxx modules are aliases of stdlib modules created by eventlet monkey patching.

    So if cinder does crash, I suggest to look at Python, but *also* look at these 2 extensions ;-)

    @vstinner
    Copy link
    Member Author

    See also bpo-42955 "Add sys.module_names: list of stdlib module names (Python and extension modules)".

    @vstinner
    Copy link
    Member Author

    vstinner commented Apr 7, 2021

    New changeset 3d55aa9 by Victor Stinner in branch 'master':
    bpo-42923: Fix _Py_DumpExtensionModules() for NULL sysdict (GH-25262)
    3d55aa9

    @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.10 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs)
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant