classification
Title: Disallow calling PyDict_GetItem() with the GIL released
Type: Stage: resolved
Components: C API Versions: Python 3.10
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: eric.snow, ncoghlan, vstinner
Priority: normal Keywords: patch

Created on 2020-06-01 18:46 by vstinner, last changed 2021-02-21 11:08 by vstinner. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 20580 merged vstinner, 2020-06-01 18:53
Messages (5)
msg370572 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2020-06-01 18:46
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).
msg370573 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2020-06-01 18:56
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.
msg370574 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2020-06-01 18:56
Note: I created this issue while working on bpo-40826 "PyOS_InterruptOccurred() now requires to hold the GIL: PyOS_Readline() crash".
msg370606 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2020-06-02 12:03
New changeset 59d3dce69b0a4f6ee17578ae68037cc7ae90936f by Victor Stinner in branch 'master':
bpo-40839: PyDict_GetItem() requires the GIL (GH-20580)
https://github.com/python/cpython/commit/59d3dce69b0a4f6ee17578ae68037cc7ae90936f
msg387452 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2021-02-21 11:08
I made a similar change in _PyDict_GetItemHint():

commit d5fc99873769f0d0d5c5d5d99059177a75a4e46e (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.
History
Date User Action Args
2021-02-21 11:08:13vstinnersetmessages: + msg387452
2020-06-02 12:04:10vstinnersetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2020-06-02 12:03:33vstinnersetmessages: + msg370606
2020-06-01 18:56:58vstinnersetmessages: + msg370574
2020-06-01 18:56:17vstinnersetnosy: + ncoghlan, eric.snow
messages: + msg370573
2020-06-01 18:53:23vstinnersetkeywords: + patch
stage: patch review
pull_requests: + pull_request19819
2020-06-01 18:46:37vstinnercreate