This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: multi subinterpreters use _PyStructSequence_InitType failed.
Type: crash Stage: patch review
Components: Subinterpreters Versions:
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: JunyiXie, shihai1991
Priority: normal Keywords: patch

Created on 2021-06-29 06:53 by JunyiXie, last changed 2022-04-11 14:59 by admin.

Pull Requests
URL Status Linked Edit
PR 26949 open JunyiXie, 2021-06-29 07:04
Messages (4)
msg396703 - (view) Author: junyixie (JunyiXie) * Date: 2021-06-29 06:53
In _PyStructSequence_InitType, it will check type is initialized.
but when we have multi subinterpreters, type may be initialized expected.

when type already been initialized, should return 0 rather than throw exception.
```c
/* PyTypeObject has already been initialized */
if (Py_REFCNT(type) != 0) {
    return 0;
}
```


```c
int
_PyStructSequence_InitType(PyTypeObject *type, PyStructSequence_Desc *desc,
                           unsigned long tp_flags)
{
    PyMemberDef *members;
    Py_ssize_t n_members, n_unnamed_members;

#ifdef Py_TRACE_REFS
    /* if the type object was chained, unchain it first
       before overwriting its storage */
    if (type->ob_base.ob_base._ob_next) {
        _Py_ForgetReference((PyObject *)type);
    }
#endif

    /* PyTypeObject has already been initialized */
    if (Py_REFCNT(type) != 0) {
        PyErr_BadInternalCall();
        return -1;
    }
```
msg404490 - (view) Author: Hai Shi (shihai1991) * (Python triager) Date: 2021-10-20 16:24
> In _PyStructSequence_InitType, it will check type is initialized.
  It's a internal C API :)

> when type already been initialized, should return 0 rather than throw exception.
  The another solution is to check the type status before calling _PyStructSequence_InitType().
msg406068 - (view) Author: junyixie (JunyiXie) * Date: 2021-11-10 03:33
Include/internal/pycore_structseq.h:
   10  
   11  
   12: PyAPI_FUNC(int) _PyStructSequence_InitType(
   13      PyTypeObject *type,
   14      PyStructSequence_Desc *desc,

Modules/_cursesmodule.c:
 4794      /* ncurses_version */
 4795      if (NcursesVersionType.tp_name == NULL) {
 4796:         if (_PyStructSequence_InitType(&NcursesVersionType,
 4797                                         &ncurses_version_desc,
 4798                                         Py_TPFLAGS_DISALLOW_INSTANTIATION) < 0) {

Objects/structseq.c:
  463  
  464  int
  465: _PyStructSequence_InitType(PyTypeObject *type, PyStructSequence_Desc *desc,
  466                             unsigned long tp_flags)
  467  {
  ...
  527  PyStructSequence_InitType2(PyTypeObject *type, PyStructSequence_Desc *desc)
  528  {
  529:     return _PyStructSequence_InitType(type, desc, 0);
  530  }
  531  

Python/sysmodule.c:
 2813      /* version_info */
 2814      if (VersionInfoType.tp_name == NULL) {
 2815:         if (_PyStructSequence_InitType(&VersionInfoType,
 2816                                         &version_info_desc,
 2817                                         Py_TPFLAGS_DISALLOW_INSTANTIATION) < 0) {
 ....
 2827      // sys.flags: updated in-place later by _PySys_UpdateConfig()
 2828      if (FlagsType.tp_name == 0) {
 2829:         if (_PyStructSequence_InitType(&FlagsType, &flags_desc,
 2830                                         Py_TPFLAGS_DISALLOW_INSTANTIATION) < 0) {
 2831              goto type_init_failed;
 ....
 2837      /* getwindowsversion */
 2838      if (WindowsVersionType.tp_name == 0) {
 2839:         if (_PyStructSequence_InitType(&WindowsVersionType,
 2840                                         &windows_version_desc,
 2841                                         Py_TPFLAGS_DISALLOW_INSTANTIATION) < 0) {
msg406325 - (view) Author: Hai Shi (shihai1991) * (Python triager) Date: 2021-11-14 17:05
OK,thanks. If it's just only interal calling, I don't think throwing a exception would break anything. As your pasted code shows, the modules have judeged the tp_name is inited or not.
History
Date User Action Args
2022-04-11 14:59:47adminsetgithub: 88698
2021-11-14 17:05:16shihai1991setmessages: + msg406325
2021-11-10 03:33:12JunyiXiesetmessages: + msg406068
2021-10-20 16:24:43shihai1991setnosy: + shihai1991
messages: + msg404490
2021-06-29 07:04:58JunyiXiesetkeywords: + patch
stage: patch review
pull_requests: + pull_request25516
2021-06-29 06:53:42JunyiXiecreate