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

Disallow calling PyDict_GetItem() with the GIL released #85016

Closed
vstinner opened this issue Jun 1, 2020 · 5 comments
Closed

Disallow calling PyDict_GetItem() with the GIL released #85016

vstinner opened this issue Jun 1, 2020 · 5 comments

Comments

@vstinner
Copy link
Member

@vstinner vstinner commented Jun 1, 2020

BPO 40839
Nosy @ncoghlan, @vstinner, @ericsnowcurrently
PRs
  • #20580
  • 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 2020-06-02.12:04:10.470>
    created_at = <Date 2020-06-01.18:46:37.644>
    labels = ['expert-C-API', '3.10']
    title = 'Disallow calling PyDict_GetItem() with the GIL released'
    updated_at = <Date 2021-02-21.11:08:13.726>
    user = 'https://github.com/vstinner'

    bugs.python.org fields:

    activity = <Date 2021-02-21.11:08:13.726>
    actor = 'vstinner'
    assignee = 'none'
    closed = True
    closed_date = <Date 2020-06-02.12:04:10.470>
    closer = 'vstinner'
    components = ['C API']
    creation = <Date 2020-06-01.18:46:37.644>
    creator = 'vstinner'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 40839
    keywords = ['patch']
    message_count = 5.0
    messages = ['370572', '370573', '370574', '370606', '387452']
    nosy_count = 3.0
    nosy_names = ['ncoghlan', 'vstinner', 'eric.snow']
    pr_nums = ['20580']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue40839'
    versions = ['Python 3.10']

    @vstinner
    Copy link
    Member Author

    @vstinner vstinner commented Jun 1, 2020

    For historical reasons, it was allowed to call the PyDict_GetItem() function with the GIL released.

    I propose to change PyDict_GetItem() to fail with a fatal error if it's called with the GIL released.

    To help C extension modules authors, I propose to keep a check at the runtime even in release build. Later, we may drop this check in release mode and only keep it in debug mode.

    In Python 3.8 and then 3.9, some functions started to crash when called without holding the GIL. It caused some bad surprises to C extension modules authors. Example: gdb developers with bpo-40826. In my opinion, holding the GIL was always required even if it is not very explicit in the documentation of the C API (only the documentation of few functions are explicit about the GIL).

    @vstinner
    Copy link
    Member Author

    @vstinner vstinner commented Jun 1, 2020

    Current comment in Objects/dictobject.c:

    /* We can arrive here with a NULL tstate during initialization: try
       running "python -Wi" for an example related to string interning.
       Let's just hope that no exception occurs then...  This must be
       _PyThreadState_GET() and not PyThreadState_Get() because the latter
       abort Python if tstate is NULL. */
    

    PyDict_GetItem() is no longer called before Py_Initialize(). I reworked the Python startup to no longer use Python objects before Py_Initialize(): see PEP-587 (PyConfig).

    To help C extension modules authors, I propose to keep a check at the runtime even in release build. Later, we may drop this check in release mode and only keep it in debug mode.

    Hum, since the whole test pass with the change and it was not documented that it was possible to call the function with the GIL released, I changed my mind and only kept the runtime check in debug mode.

    @vstinner
    Copy link
    Member Author

    @vstinner vstinner commented Jun 1, 2020

    Note: I created this issue while working on bpo-40826 "PyOS_InterruptOccurred() now requires to hold the GIL: PyOS_Readline() crash".

    @vstinner
    Copy link
    Member Author

    @vstinner vstinner commented Jun 2, 2020

    New changeset 59d3dce by Victor Stinner in branch 'master':
    bpo-40839: PyDict_GetItem() requires the GIL (GH-20580)
    59d3dce

    @vstinner
    Copy link
    Member Author

    @vstinner vstinner commented Feb 21, 2021

    I made a similar change in _PyDict_GetItemHint():

    commit d5fc998 (HEAD -> master, upstream/master)
    Author: Victor Stinner <vstinner@python.org>
    Date: Sun Feb 21 12:02:04 2021 +0100

    bpo-42093: Cleanup _PyDict_GetItemHint() (GH-24582)
    
    * No longer save/restore the current exception. It is no longer used
      with an exception raised.
    * No longer clear the current exception on error: it's now up to the
      caller.
    

    @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
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant