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: Crash on Py_DecRef'ing builtin object from previous run
Type: crash Stage: resolved
Components: C API Versions: Python 3.10, Python 3.9
process
Status: open Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Victor Milovanov, pablogsal, ronaldoussoren, shihai1991
Priority: normal Keywords:

Created on 2021-11-08 23:07 by Victor Milovanov, last changed 2022-04-11 14:59 by admin.

Messages (4)
msg405987 - (view) Author: Victor Milovanov (Victor Milovanov) Date: 2021-11-08 23:07
Trying to Py_DecRef owned reference to builtin "iter" crashes if the reference was alive when runtime was reinitialized.

Py_Initialize();
PyObject* builtins = PyEval_GetBuiltins();
PyObject* iter = PyDict_GetItemString(builtins, "iter");

Py_IncRef(iter);

Py_Finalize();

// ----- new run starts, iter should still be alive

Py_Initialize();

Py_DecRef(iter);

Py_Finalize(); // fails inside PyGC_Collect -> validate_list

Related on StackOverflow: https://stackoverflow.com/questions/69890182/is-it-safe-to-call-py-decref-on-an-object-created-before-the-last-py-initializ
msg406075 - (view) Author: Hai Shi (shihai1991) * (Python triager) Date: 2021-11-10 07:01
Hi, Victor Milovanov. The memory allocated by interpreter will be freed after calling Py_Finalize().
msg406204 - (view) Author: Ronald Oussoren (ronaldoussoren) * (Python committer) Date: 2021-11-12 10:45
IMHO your code is buggy, as Hai Shi also indicates.

After Py_Finalize all objects in all (sub-)interpreters are deallocated unless there are bugs in the interpreter. Because of this "..., item should still be alive" in your sample code is incorrect: the reference should be considered to be invalid and cannot be used again.
msg406228 - (view) Author: Victor Milovanov (Victor Milovanov) Date: 2021-11-12 18:03
I think documentation should clarify that. Right now this line in the docs got me thinking that anything with an external strong reference won't be deallocated: "Memory tied up in circular references between objects is not freed."
History
Date User Action Args
2022-04-11 14:59:52adminsetgithub: 89919
2021-11-12 18:03:36Victor Milovanovsetstatus: pending -> open

messages: + msg406228
2021-11-12 10:45:52ronaldoussorensetstatus: open -> pending

nosy: + ronaldoussoren
messages: + msg406204

resolution: not a bug
stage: resolved
2021-11-10 07:01:02shihai1991setnosy: + shihai1991, pablogsal
messages: + msg406075
2021-11-08 23:07:35Victor Milovanovcreate