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.

Title: PyInterpreterState_Head(), PyThreadState_Next() etc can't be sanely used
Type: Stage: resolved
Components: Versions:
Status: closed Resolution: duplicate
Dependencies: Superseder: PyThreadState_Next not thread safe
View: 1021318
Assigned To: Nosy List: fijall, pitrou, serhiy.storchaka, vstinner
Priority: normal Keywords:

Created on 2016-02-29 23:19 by fijall, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (4)
msg261030 - (view) Author: Maciej Fijalkowski (fijall) * (Python committer) Date: 2016-02-29 23:19
All the internal uses of this API guard everything with HEAD_LOCK/HEAD_UNLOCK that's not exposed. It's not safe to traverse the whole API without holding those locks and those locks are static and module local
msg261031 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2016-02-29 23:27
Are you asking to expose the lock?
msg261032 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2016-02-29 23:31
fijal explained on IRC his use case: implement vmprof on Windows, a statistical profiler, using a C thread running in the Python process.

The _PyThread_CurrentFrames() API requires to be called with the GIL hold which is not acceptable for his use case. Having to wait for the GIL would serialize statistics plots and so would have a poor quality, probably similar to cProfile.

The use case is to implement something like _PyThread_CurrentFrames() without holding the GIL.


fijal mentioned pystate.c/.h functions which come with this comment:

/* Routines for advanced debuggers, requested by David Beazley.
   Don't use unless you know what you are doing! */

These functions are not safe if you don't hold head_mutex lock, but this lock is private in pystate.c (declared with "static").

I guess that they are safe to use if the whole process is stopped by a debugger like gdb.
msg368894 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2020-05-15 01:09
Duplicate of bpo-1021318.
Date User Action Args
2022-04-11 14:58:28adminsetgithub: 70648
2020-05-15 01:09:24vstinnersetstatus: open -> closed
resolution: duplicate
messages: + msg368894

superseder: PyThreadState_Next not thread safe
stage: resolved
2016-02-29 23:31:52vstinnersetmessages: + msg261032
2016-02-29 23:27:06vstinnersetnosy: + vstinner, serhiy.storchaka, pitrou
messages: + msg261031
2016-02-29 23:19:10fijallcreate