Comments (6)
I'm not sure any of this is related to the free-threaded build, but there are a few bugs in your code example:
Py_NewInterpreterFromConfig
sets the active thread-state. The subsequentPyEval_RestoreThread()
crashes- You are missing a
Py_EndInterpreter()
Here is a similar, fixed example:
PyThreadState *prev = PyThreadState_Swap(NULL);
PyInterpreterConfig conf = { 0 };
conf.check_multi_interp_extensions = 1;
conf.allow_threads = 1;
conf.gil = PyInterpreterConfig_OWN_GIL;
PyThreadState* tstate;
Py_NewInterpreterFromConfig(&tstate, &conf); // creating new interpreter in new thread
ExecuteScript(name, console_id, argc, argv); //PyRun_String-like func
Py_EndInterpreter(tstate);
PyThreadState_Swap(prev);
from cpython.
Oh yes, this is the rough code, the PyEval_RestoreThread() call has now been removed. However, I don't consider the lack of calling Py_EndInterpreter() to be the cause of the error, as it is just a memory leak. But in the error we can see that there seems to be object confusion in cpython.
from cpython.
I don't consider the lack of calling Py_EndInterpreter() to be the cause of the error, as it is just a memory leak
The lack of Py_EndInterpreter()
is not just a memory leak. It also swaps the active thread state. Your original code example calls PyThreadState_Clear()
with the wrong active interpreter. That can lead to all sorts of problems, including the ones you've described above.
I don't think there's anything more we can do with this issue without additional details that are sufficient to reproduce a bug in CPython.
from cpython.
Your fix just throws an exception (C0000005, EXCEPTION_ACCESS_VIOLATION)! in progress. The code I left above should be enough to reproduce the problem.
EXPORT VOID RunScript(char* name, int console_id, int argc, char* argv[]) {
// auto new_thread = PyThreadState_New(g_state->interp); //creating sub-main thread
auto prev = PyThreadState_Swap(0); //swap
PyInterpreterConfig conf = { 0 };
conf.check_multi_interp_extensions = 1;
conf.allow_threads = 1;
conf.gil = PyInterpreterConfig_OWN_GIL;
PyThreadState* tstate;
Py_NewInterpreterFromConfig(&tstate, &conf); // creating new interpreter in new thread
ExecuteScript(name, console_id, argc, argv); //PyRun_String-like func
Py_EndInterpreter(tstate);
PyThreadState_Swap(prev);
// PyThreadState_Clear(new_thread);
// PyThreadState_Delete(new_thread);
}
from cpython.
It's not enough to reproduce the issue because the code run by ExecuteScript
is not shown.
Here is the code I've tested that runs without issues:
Code Snippet
static PyObject *
issue117864(PyObject *module, PyObject *arg)
{
PyThreadState *prev = PyThreadState_Swap(NULL);
PyInterpreterConfig conf = { 0 };
conf.check_multi_interp_extensions = 1;
conf.allow_threads = 1;
conf.gil = PyInterpreterConfig_OWN_GIL;
PyThreadState* tstate;
Py_NewInterpreterFromConfig(&tstate, &conf); // creating new interpreter in new thread
PyObject *globals = PyDict_New();
PyObject *locals = PyDict_New();
PyObject *r = PyRun_String("print('Hello, World!')", Py_file_input, globals, locals);
if (r == NULL) {
PyErr_Print();
}
Py_XDECREF(r);
Py_EndInterpreter(tstate);
PyThreadState_Swap(prev);
Py_RETURN_NONE;
}
from cpython.
My program calls this code several times, so that inside the first ExecuteScript call there is another ExecuteScript call (for example, the autorun.py file runs other scripts through this code, which, by the way, is part of a custom cpython module). The first call (including the nested one) succeeds, but the second does not.
Callstack:
from cpython.
Related Issues (20)
- ``test_compile`` leaks references HOT 1
- `import keyword; help(keyword)` has some issues HOT 1
- _pydecimal.Decimal.__pow__ tries to compute 10**1000000000000000000 and thus doesn't terminate HOT 17
- Unittest not being found HOT 1
- When using inspect package's Parameter as dictionary keys, performance is lower compared to basic types, especially in large loops. HOT 1
- Don't needlessly repeat Sphinx directives HOT 17
- `dataclasses`: 3.12.3 regression with `weakref_slot` HOT 4
- CPython requires stdatomic.h HOT 2
- Call stats are incorrect for tier 2 and maybe for tier 1 as well HOT 1
- Problem with config cache of WASI HOT 11
- subclasses of telnetlib.Telnet may crash when used as context manager HOT 7
- Performance difference of C calling conventions HOT 1
- float('inf') faster ways HOT 10
- Python 3.x fails to load a .so file in TCL through tkinter HOT 1
- Python 3.x crashes when you load a .so file in TCL through tkinter HOT 6
- Add symlink to generalize macOS library pathname HOT 2
- The "finder" Glossary Entry May Need Updating
- Add option for hidden command line arguments to argparse HOT 1
- test_posixpath: test_expanduser_pwd2() fails on AMD64 Ubuntu Shared 3.x HOT 3
- [feature request] pathlib.Path().asyncopen HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cpython.