classification
Title: [subinterpreters] Don't share Python objects between interpreters
Type: Stage: patch review
Components: Subinterpreters Versions: Python 3.9
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: eric.snow, shihai1991, vstinner
Priority: normal Keywords: patch

Created on 2020-05-06 15:11 by vstinner, last changed 2022-01-13 17:32 by vstinner.

Pull Requests
URL Status Linked Edit
PR 19958 closed vstinner, 2020-05-06 15:38
PR 19961 merged vstinner, 2020-05-06 15:57
Messages (8)
msg368262 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2020-05-06 15:11
To get one GIL per interpreter (bpo-40512), either PyObject.ob_refcnt member must become an atomic variable, or subinterpreters must not share any object.

Right now, subinterpreters share Python objects. For example, PyModule_Type is declared statically and so shared by all interpreters and so PyModule_Type.tp_mro tuple is accessed in parallel by multiple interpreters. If PyObject.ob_refcnt is not atomic, Py_INCREF() and Py_DECREF() are unsafe and tp_mro tuple can be destroyed whereas it is still used.

I propose to make PyObject.ob_refcnt atomic for now, when Python is built with EXPERIMENTAL_ISOLATED_SUBINTERPRETERS macro defined. It's a temporary workaround until subinterpreters stop sharing objects.
msg368274 - (view) Author: Eric Snow (eric.snow) * (Python committer) Date: 2020-05-06 16:08
Yep, before per-interpreter GIL is official we must get to the point where *no* PyObject objects are shared.

Making PyObject.ob_refcnt atomic until then (only as part of the experiment) should be fine.
msg368276 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2020-05-06 16:20
> Yep, before per-interpreter GIL is official we must get to the point where *no* PyObject objects are shared.

I would like to add: "no PyObject objects are shared in the stdlib" ;-)
msg368279 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2020-05-06 16:25
New changeset d8135e913ab7c694db247c86d0a84c450c32d86e by Victor Stinner in branch 'master':
bpo-40533: Disable GC in subinterpreters (GH-19961)
https://github.com/python/cpython/commit/d8135e913ab7c694db247c86d0a84c450c32d86e
msg380105 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2020-10-31 23:05
> For example, PyModule_Type is declared statically and so shared by all interpreters and so PyModule_Type.tp_mro tuple is accessed in parallel by multiple interpreters.

Another example of this issue are parallel Py_INCREF/Py_DECREF calls on the tp_bases member of a type.
msg388966 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2021-03-17 20:51
> To get one GIL per interpreter (bpo-40512), either PyObject.ob_refcnt member must become an atomic variable, or subinterpreters must not share any object.

The current plan is to not share any object between two interpreters, so this PR is not needed. I close my PR 19958 which marked PyObject.ob_refcnt as atomic.
msg408606 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2021-12-15 14:49
Until all Python stdlib C extensions and all third-party C extensions will be modified to no longer use and define static types and will stop shared Python objects between two interpreters, we can no longer micro-optimize the comparison of two interned strings by only comparing their memory addresse. See bpo-46006.
msg410499 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2022-01-13 17:32
Sharing objects between multiple interpreters can cause complex bugs leading to crashes: https://bugs.python.org/issue46070#msg410493 For this specific bug, I wrote a workaround (GH-30577).
History
Date User Action Args
2022-01-13 17:32:56vstinnersetmessages: + msg410499
2021-12-15 14:49:20vstinnersetmessages: + msg408606
2021-03-17 20:51:04vstinnersetmessages: + msg388966
2020-10-31 23:05:40vstinnersetmessages: + msg380105
2020-05-15 00:36:24vstinnersetcomponents: + Subinterpreters, - Interpreter Core
title: Subinterpreters: don't share Python objects between interpreters -> [subinterpreters] Don't share Python objects between interpreters
2020-05-06 16:25:10vstinnersetmessages: + msg368279
2020-05-06 16:20:33vstinnersetmessages: + msg368276
2020-05-06 16:08:25eric.snowsetmessages: + msg368274
2020-05-06 15:59:11vstinnersetnosy: + eric.snow
2020-05-06 15:57:00vstinnersetpull_requests: + pull_request19277
2020-05-06 15:38:52vstinnersetkeywords: + patch
stage: patch review
pull_requests: + pull_request19274
2020-05-06 15:31:45shihai1991setnosy: + shihai1991
2020-05-06 15:11:35vstinnercreate